我有两个项目,一个是AddExec,另一个是DllAdd。 AddExec通过GetProcAddress检索到地址后加载DllAdd并调用Add函数..
这是我的 AddExec 的 main.cpp
#include <iostream>
#include <windows.h>
typedef int(__cdecl*AddPtrType) (int x, int y);
int main()
{
HMODULE DllAddModule = LoadLibraryA("DllAdd.dll");
if (DllAddModule == NULL)
{
DWORD error = GetLastError();
std::cerr << "Failed to get address : " << error;
return -1;
}
AddPtrType AddLocal = (AddPtrType)GetProcAddress(DllAddModule, "Add");
if (AddLocal == NULL)
{
DWORD error = GetLastError();
std::cerr << "Failed to get address : " << error;
return -1;
}
int x = AddLocal(10, 15);
std::cout << x;
return 0;
}
这是我的 DllAdd.dll 的 main.cpp
__declspec(dllexport) int __cdecl Add(int x, int y)
{
return x + y;
}
DllAdd 内部的函数称为 Add。但是当我尝试检索其地址时,我收到错误代码 127
ERROR_PROC_NOT_FOUND
127 (0x7F)
The specified procedure could not be found.
我不知道为什么它找不到地址。我正在导出它并使用确切的函数名称
最佳答案
您的编译器正在管理 DllAdd 中的 Add 函数的名称。因为您正在使用允许函数名称重载的 C++ 编译器来编译它。解决此问题的方法正如我提到的,要么将其编译为 C 项目,要么阻止编译器重命名或名称修改函数,如下所示。
extern "C" __declspec(dllexport) int __cdecl Add(int x, int y)
{
return x + y;
}
extern "C"
将阻止编译器更改它。
关于c++ - 无法在我自己的 dll 中使用 GetProcAddress 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76698999/