c++ LoadLibrary and GetProcAddress from extern DLL

840 views Asked by At

I´ve got an extern DLL to work with. I try to load the DLL with LoadLibrary and get the function by calling GetProcAddress but everytime I debug the programm I got an exception, which I can ignore by windows but in the end my main method throws the exception also and never ends in calling exceptions:

typedef int(__cdecl* GETRANDOMNUM)(unsigned char*);

int main(int argc, char *argv[])
{
    HMODULE hinstLib = LoadLibrary(TEXT("some.dll"));
    GETRANDOMNUM getrandomnum = (GETRANDOMNUM)GetProcAddress(hinstLib, "getrandomnum");
    
    int state = 0;
    //getrandomnum
    if (ic_getrandomnum != NULL)
    {
        unsigned char password[8] = { 0 };
        state = (getrandomnum)(password);
    }
    
    if(!FreeLibrary(hinstLib))
        return -1;

    return state;
}

The definition of "getrandomnum" is:

#ifdef EXPORT
#define IM_OR_EXPORT    extern "C" __declspec(dllexport)
#else
#ifdef USE_IMPORT_LIBRARY
#define IM_OR_EXPORT    extern "C" __declspec(dllimport)
#else
...
#define CALLTYPE  __stdcall
...
IM_OR_EXPORT int CALLTYPE getrandomnum(unsigned char* randomnum);

The exceptions are:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

And:

Ausnahme ausgelöst bei 0x0044C828 in Cpp.exe: 0xC0000005: Zugriffsverletzung beim Ausführen an Position 0x0044C828.

And:

Ausnahmefehler bei 0x00442F59 in Cpp.exe: Vom Stapelcookie-Instrumentationscode wurde ein stapelbasierter Pufferüberlauf erkannt.

This exception repeats for ever.

May someone help me?

0

There are 0 answers