gpt4 book ai didi

c++ - 通过 GetProcAddress 提取的函数指针使应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 00:08:28 26 4
gpt4 key购买 nike

在我的例子中,我通过有效导出函数名称的 GetProcAddress 获得了一个函数指针,一旦我的应用程序调用导出函数,它就会在显示输出后崩溃。如果我从函数和 typdef 中删除参数并直接将 19342 传递给“getNumChildShapes(19342)”,一切正常,没有任何崩溃。

在我的申请中:

typedef INT (CALLBACK* LPFNDLLFUNC2)(INT modelid);
int codecall()
{
HINSTANCE hDLL;
LPFNDLLFUNC2 lpfnDllFunc2;
INT value = 0;
INT pass = 19342;

hDLL = LoadLibrary(L"myDLL");
if (hDLL != NULL)
{
lpfnDllFunc2 = (LPFNDLLFUNC2)GetProcAddress(hDLL, "get_children");
if (!lpfnDllFunc2)
{
// handle the error
FreeLibrary(hDLL);
return 0;
}
else
{
// call the function
value = lpfnDllFunc2(pass);
}
}
printf("Children: %i", value);
FreeLibrary(hDLL);
return 1;
}

在我的 dll 中:

extern "C"
{
__declspec(dllexport) int get_children(int modelid)
{
return getNumChildShapes(modelid);
}
}

最佳答案

从 DLL 导出的函数将使用 C 调用约定 (__cdecl) 进行编译,但是函数指针的 typedef 表明您希望将其视为 Windows 调用约定(CALLBACK 是 __stdcall 的宏)。将这两者混合起来会使堆栈失衡。

要么从函数指针声明中删除 CALLBACK,要么使用 __stdcall 声明导出的函数。

关于c++ - 通过 GetProcAddress 提取的函数指针使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34225253/

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