C ++无法在DLL中调用回调函数

时间:2016-12-27 16:54:07

标签: c++ api dll callback

当我的C ++可执行文件读取一个dll时,无法调用dll中的回调函数,但可以使用普通函数。

以下是我的情况:

1有第三方DLL API:thirdparty.dll

2使用thirdparty.dll作为外部链接的win32 dll项目,然后这个dll项目是为mydll.dll构建的。 f()是mydll.dll中的函数,g()是mydll.dll中的回调函数。调用f()会触发g(),但实现由第三方API提供,因此不可见。

3 win32控制台应用程序使用mydll.dll作为外部链接。它的main()调用f(),但不会触发g()。但是,如果我将mydll.dll重写为可执行的win控制台应用程序,f()可以成功触发g()。

有人可以帮忙吗?

非常感谢,

学家张

**mydll.h:**
#define MYAPI extern "C" _declspec(dllexport)

MYAPI void mylogin();

**mydll.cpp:**
void mylogin()
{
    cout<<"called function mylogin()"<<endl;
    INSTANCE* instance = INSTANCE::CreateInstance(); //CreateInstance() implement in thirdparty API hence invisible
    cout<<"created instance"<<endl;     
    instance->join();
    cout<<"called function join()"<<endl;
}

**loginSPI.cpp:**
void OnFrontConnected()
{
    cout<<"connected"<<endl; //join() will call callback OnFrontConnected() in thirdparty dll
}

thirdparty.dll: invisible

**mydlltest.cpp**
int main()
{
    mylogin();
    return 0;
}

result of running mydlltest.cpp

called function mylogin()
created instance

if rewrite to 

**main.cpp:**
void main()
{
    cout<<"called function mylogin()"<<endl;
    INSTANCE* instance = INSTANCE::CreateInstance(); //CreateInstance() implement in thirdparty API hence invisible
    cout<<"created instance"<<endl;     
    instance->join();
    cout<<"called function join()"<<endl;
}

**loginSPI.cpp:**
void OnFrontConnected()
{
    cout<<"connected"<<endl; //join() will call callback OnFrontConnected() in thirdparty dll
}

result of running main.cpp

called function mylogin()
created instance
connected //"called function join()" is not printed as well

0 个答案:

没有答案