c++ - 无法在我自己的 dll 中使用 GetProcAddress 调用函数

标签 c++ winapi

我有两个项目,一个是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/

相关文章:

c++ - 从网格渲染 RGB-D 图像

C++使用谓词在元组列表中查找元素

c++ - 按编号确定对象类型

c - 可以复制 CRITICAL_SECTION 吗?

ruby - 如何从 ruby​​ 应用程序将击键发送到窗口?

C++ 线程安全 - map 读取

c++ - 如何多次调用具有不同参数的函数成员?

windows - GetDC() 和 BeginPaint() 之间的区别

windows - 是否有 WM_QUERYHIDE/CLOSE 或类似信息?

c# - 为什么 CreateFile 需要永远?