MircoProgram MircoProgram - 2 months ago 18
C++ Question

c++ WINAPI call exported function via GetProcAddress

Im trying to call an exported function from a DLL by grabbing its function pointer through

GetProcAddress
but upon calling the function the application crashes.

I used dependencywalker to see if the exported functions have the correct name. The address returned from
GetProcAddress
is not null. I'm almost certain that it has something to do with the calling convention, i used both
__cdecl
and
__stdcall
but no success. However i do would like to use
GetProcAdress
instead of
__declspec(dllimport)
.

DLL #1 (Caller)


  • Linked DLL#2.lib to this DLL

    typedef void(__stdcall *ptr_init)(DWORD size);

    ctx.hModule = LoadLibraryA("someDLL.dll");
    ptr_init init = (ptr_init)GetProcAddress(ctx.hModule, "init");

    if (init == NULL) {
    out = out + " | init function is null";
    } else {
    out = out + " | init function found!";//It is found
    }

    DWORD test = 10;
    (*init)(test);//<-- makes application crash



DLL #2 (DLL containing exported function)

//header.h
extern "C" __declspec(dllexport) void init(DWORD size);

//source.cpp
extern "C" __declspec(dllexport) void init(DWORD size) {
//code
}

Answer

You should be consistent. If you retrieve pointer as a pointer to stdcall function - it must be declared as stdcall in implementation:

//header.h
extern "C" __declspec(dllexport) void __stdcall init(DWORD size);
Comments