C++ 使用英特尔编译器编译DLL时出错
我试图从控制台编译DLL,不使用任何IDE,并且面临下一个错误 我写了这段代码: 测试dll.cppC++ 使用英特尔编译器编译DLL时出错,c++,winapi,dll,compiler-construction,intel,C++,Winapi,Dll,Compiler Construction,Intel,我试图从控制台编译DLL,不使用任何IDE,并且面临下一个错误 我写了这段代码: 测试dll.cpp #include <windows.h> #define DLL_EI __declspec(dllexport) BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved){ return 1; } extern "C" int DLL_EI func (int a, int b){
#include <windows.h>
#define DLL_EI __declspec(dllexport)
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved){
return 1;
}
extern "C" int DLL_EI func (int a, int b){
return a + b;
}
int main(){
HMODULE hLib;
hLib = LoadLibrary("test_dll.dll");
double (*pFunction)(int a, int b);
(FARPROC &)pFunction = GetProcAddress(hLib, "Function");
printf("begin\n");
Rss = pFunction(1, 2);
}
使用icl prog.cpp
编译它。然后我运行它,它在标准窗口“程序不工作”下失败。可能存在分段错误
我做错了什么?检查
LoadLibrary()
和GetProcAddress()
是否都成功,在这种情况下,它们肯定不会成功,因为导出的函数调用的是func
,而不是GetProcAddress()的参数中指定的“function”
意味着在尝试调用函数指针时,函数指针将为NULL
函数指针的签名也与导出函数的签名不匹配,导出函数返回一个int
,函数指针需要一个double
例如:
typedef int (*func_t)(int, int);
HMODULE hLib = LoadLibrary("test_dll.dll");
if (hLib)
{
func_t pFunction = (func_t)GetProcAddress(hLib, "func");
if (pFunction)
{
Rss = pFunction(1, 2);
}
else
{
// Check GetLastError() to determine
// reason for failure.
}
FreeLibrary(hLib);
}
else
{
// Check GetLastError() to determine
// reason for failure.
}
快速浏览一下,应该使用函数名而不是“函数”调用GetProcAddress。但是在每次调用之后添加一些正确的错误检测代码,这样您就可以看到它失败的地方