简体   繁体   中英

c++ callback from dll

I have the following c++ code for making a dll (only a part of it):

    #include <windows.h>
    #include <stdint.h>

    using namespace std;

    typedef int (__stdcall *event)(unsigned int id, int value);

    BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwFunction, LPVOID lpReserved);

    namespace Bla
    {
      class blubb
      {
        public:         
        static __declspec(dllexport) void setCallback(event evnHnd);
      };
    }

Now I want to link that dll in another c++ code. Therefore I use that code:

typedef int (__stdcall *event)(unsigned int id, int value);
typedef void (__stdcall *setCallback)(eventCallback evHnd);

int __stdcall valuesDll( unsigned int id, int value)
{
std::cout << "id::value == " << id << "::" << value << std::endl;
return 0;
}

int _tmain()
{

HINSTANCE hDLL = LoadLibrary(_T("test"));
if(hDLL == NULL)
{
    std::cout << "dll not loaded.\n";
}
else
{
    std::cout << "DLL loaded.\n";
    setCallback values = (setCallback)GetProcAddress(hDLL, "setCallback");

    if(NULL != values)
    {   
        values(&valuesDll);
    }

    FreeLibrary(hDLL);
}
return 0;
}

But now I got the error:

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.

Why? Thank you very much.

blubb::setCallback defaults to __cdecl and you call it as if it were __stdcall . Try declaring it in your DLL as:

static __declspec(dllexport) void __stdcall setCallback(event evnHnd);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM