gpt4 book ai didi

c++ - 使用 intel 编译器编译 DLL 时出错

转载 作者:行者123 更新时间:2023-11-30 02:57:41 24 4
gpt4 key购买 nike

我正在尝试从控制台编译 DLL,不使用任何 IDE 并面临下一个错误。

我写了这段代码:

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:

程序.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编译。然后我运行它,它失败并显示标准窗口“程序不工作”。可能存在段错误错误。

我做错了什么?

最佳答案

检查 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.
}

关于c++ - 使用 intel 编译器编译 DLL 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14333788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com