Katsazuka
Katsazuka

Reputation: 1

Pass and receive raw byte data via an unsigned char* function parameters in C++

I have a 3rd party x.dll and an export function which is named as "GenerateKeyEx-in C". I do not have any additional info such as .lib, header etc. I have found the function parameters from x.dll supplier and they are already added in the code.

Here you can see the parameter definitions;

> [in] ipSeedArray: the seed queried by the ECU (as byte raw data)
> [in] iSeedArraySize: The size of the array
> [in] iSecurityLevel: the security level to be change to
> [in] ipVariant: the ECU variant’s qualifier
> [out] iopKeyArray: the calculated key on return (as byte raw data)
> [in] iMaxKeyArraySize: maximum number of key bytes available
> [out] oActualKeyArraySize: the number of key bytes calculated 

I can access and run the function "GenerateKeyEx" without any error. Functions returns always 4 which is unspecified error for the function. I think I can not pass the array values(or initialize the arrays correctly) between main and dll function. const unsigned char* ipSeedArray and unsigned char* iopKeyArray are specified as raw data bytes(above) and did I define the arrays wrong to pass raw byte datas via unsigned char* ?

//__stdcall replaced  //__cdecl *f_GenerateKey 
typedef int(*f_GenerateKey)(const unsigned char* ipSeedArray,unsigned int iSeedArraySize,const unsigned int iSecurityLevel,
    const char* ipVariant,unsigned char* iopKeyArray,unsigned int iMaxKeyArraySize,unsigned int& oActualKeyArraySize);

int main()
{
    HINSTANCE hGetProcIDDLL = LoadLibrary(L"C:\\Users\\thego\\source\\repos\\ConsoleApplication1cp\\ConsoleApplication1cp\\Debug\\SeednKey.dll");
    if (!hGetProcIDDLL) {
        std::cout << "could not load the dynamic library" << std::endl;
        return EXIT_FAILURE;
    }
    // resolve function address here
    f_GenerateKey GenerateKey = (f_GenerateKey)GetProcAddress(hGetProcIDDLL, "GenerateKeyEx");
    if (!GenerateKey) {
        std::cout << "could not locate the function" << std::endl;
        return EXIT_FAILURE;
    }
    const int sb = 4;  const int kb = 100;
    const BYTE seedbuffer[sb] = { 0x0B,0xCF,0xFE,0x10 };                            //function in                           
    unsigned int seedbufferSize = sizeof(seedbuffer) / sizeof(seedbuffer[0]);   //function in
    const unsigned int SecurityLevel = 0x01;                                    //function in
    const char Variant[1] = { '0' };                                            //function in
    BYTE keybuffer[kb];                                                         //function out
    for (int i = 0; i < kb; ++i) keybuffer[i] = 0x00;
    unsigned int MaxKeykeybuffer = sizeof(keybuffer) / sizeof(keybuffer[0]);    //function in
    unsigned int oSize;                                                     //function out

    int x = GenerateKey(seedbuffer, seedbufferSize, SecurityLevel, Variant, keybuffer, MaxKeykeybuffer, oSize);

    return 0;
}

Upvotes: 0

Views: 620

Answers (1)

Vlad Feinstein
Vlad Feinstein

Reputation: 11321

Here:

GenerateKey(seedbuffer, SeedArraySize, ...

you are using seedbuffer, while (I'm guessing) you need SeedArray.

The bottom line is: you are passing an array of 4 bytes, and tell them that there are 9.

Upvotes: 0

Related Questions