【发布时间】:2016-11-16 10:14:11
【问题描述】:
在这个伟大的平台上阅读和学习多年后,现在我的第一篇文章。
我的问题:
在 C++ 中,我尝试创建一个动态链接库(32 位),用作 AQMP 通信客户端(基于 SimpleAmqpClient)。 dll 文件将在第三方应用程序(32 位)中使用。
在我在自定义可执行文件中调用 dll 的测试期间,一切正常。但是当我尝试在第三方应用程序中使用 dll 时,我收到了访问冲突错误 (0x00000000)。我发现问题可能出在函数调用约定上。
通过下面显示的几行代码,可以重现该错误。如果我删除 mytest.dll 中的 __stdcall 表达式,它就会消失。通常我希望代码能够工作,因为它在 custom_test.exe 和 mytest.dll 中使用相同的调用约定。
(旁注:第三方应用程序需要 __stdcall 函数,这就是我依赖它的原因)
我想了解这种行为。提前致谢!
我的设置:
- 操作系统:Windows 7
- 32 位编译器:gcc 5.3 (Cygwin)
我的代码 (custom_test.exe):
#include <stdio.h>
#include <windows.h>
int main(void) {
HINSTANCE hInstance;
hInstance=LoadLibrary("mytest.dll");
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hInstance), "test");
// Function prototype
typedef void (__stdcall *myFunction)(void);
myFunction test;
test = myFunction(lpfnGetProcessID);
// Call Function
test();
FreeLibrary(hInstance);
}
我的代码(mytest.dll):
extern "C" __declspec(dllexport) void __stdcall test(void) {
printf("Inside Function \n");
}
我通过编译代码
- dll:
g++ mytest.cpp -o mytest.dll -shared -std=gnu++11 - exe:
g++ custom_test.cpp -o custom_test.exe -std=gnu++11
【问题讨论】: