【发布时间】:2020-08-06 12:27:16
【问题描述】:
我正在尝试创建一个库 Lib.dll 以从控制台应用程序动态调用,但找不到我要调用的函数 funci()。
Lib.dll 是在 Visual Studio 2019 中创建的项目(控制台应用程序,但设置为配置类型:.dll)的结果。
Lib.cpp 是该项目中唯一的文件,并且只包含代码:
__declspec(dllexport) int funci()
{
return 50;
}
我认为我正确地导出了该函数,因为我使用 DLL Export Viewer v1.66 找到了该函数。
但是,我很难通过控制台应用程序 (.exe) 找到该功能:
#include <windows.h>
#include <iostream>
typedef int(__cdecl* o_funci)(void);
o_funci funci;
int main()
{
HINSTANCE hGetProcIDDLL = LoadLibraryA("C:\\Lib.dll");
if (!hGetProcIDDLL) {
std::cout << "could not load the dynamic library" << std::endl;
return EXIT_FAILURE;
}
// resolve function address here
funci = (o_funci) GetProcAddress(hGetProcIDDLL, "funci");
if (!funci) {
std::cout << "could not locate the function" << std::endl;
return EXIT_FAILURE;
}
std::cout << "funci() returned " << funci() << std::endl;
FreeLibrary(hGetProcIDDLL);
}
GetProcAddress 出了点问题,但不知道为什么。我哪里做错了?
输出:
我一直在看这个旧帖子:Dynamically load a function from a DLL
编辑:感谢 tenfour 解决
我使用了 DependencyWalker。
没有extern "C" 我可以看到未修饰的funci 有名称?funci@@YGHXZ,
所以funci = (o_funci)GetProcAddress(hGetProcIDDLL, "?funci@@YGHXZ"); 工作了。
使用extern "C",未修饰的funci 的名称为_funci@0 - 更干净一些。
另一个注释;使用序数 0x0001 在这两种情况下都有效。像这样:funci = (o_funci)GetProcAddress(hGetProcIDDLL, (PCSTR)0x0001);
【问题讨论】:
标签: c++ windows winapi dllexport