简体   繁体   English

DLL的C ++回调

[英]c++ callback from dll

I have the following c++ code for making a dll (only a part of it): 我有以下用于制作dll的c ++代码(仅一部分):

    #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. 现在,我想在另一个C ++代码中链接该dll。 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. ESP的值未在函数调用中正确保存。 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 . blubb::setCallback默认为__cdecl ,您可以像调用__stdcall一样对其进行调用。 Try declaring it in your DLL as: 尝试在您的DLL中声明为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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