c++winapidlldllexportgetprocaddress

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)

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
}

Solution

  • 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);