问题描述
我试图从控制台编译DLL,而不使用任何IDE并面临下一个错误。
I'm trying to compile DLL from console, without using any IDE and faced with next error.
我写了这个代码:
test_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){
return a + b;
}
然后使用命令 icl / LD test_dll.cpp
。我试图从另一个程序调用 func
:
Then compiled with command icl /LD test_dll.cpp
. And I'm trying to call this func
from another program:
prog.cpp
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
。然后我运行它,它失败与标准窗口程序不工作。
Compile it with icl prog.cpp
. Then I run it, and it fails with standard window "Program isn't working". Maybe there is a segmentation fault error.
我做错了什么?
推荐答案
检查 LoadLibrary()
和 GetProcAddress()
成功这种情况下,它们绝对不会作为导出的函数调用 func
,而不是Function
GetProcAddress()
意味着当尝试调用它时,函数指针将是 NULL
。
Check that both LoadLibrary()
and GetProcAddress()
succeed, in this case they definitely will not as the exported function is called func
, not "Function"
as specified in the argument to GetProcAddress()
meaning the function pointer will be NULL
when the attempt to invoke it is made.
函数指针的签名也不匹配导出函数的签名,导出的函数返回一个 int
,函数指针正在等待 double
。
The signature of the function pointer also does not match the signature of the exported function, the exported function returns an int
and the function pointer is expecting a 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.
}
这篇关于使用intel编译器编译DLL时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!