c++ - GetProcAddress 找不到函数

标签 c++ dll

<分区>

我尝试使用 LoadLibraryGetProcAddress 加载库以使用该库中的函数。然而,dll 加载正常,但我没有得到该功能。

dll.h

#ifndef TEST_DLL_H
#define TEST_DLL_H

void __declspec(dllexport) __stdcall tests();

#endif // TEST_DLL_H

dll.cpp

#include <iostream>
#include "test_dll.h"

void __declspec(dllexport) __stdcall tests() {
    std::cout << "tests" << std::endl;
}

main.cpp

typedef void(*f_funci)();

int main() {
    HMODULE hMod = LoadLibrary(L"plugins/test_dll.dll");
    if (!hMod)
        throw(std::runtime_error("failed to load dll"));

    f_funci f = (f_funci)GetProcAddress(hMod, "tests");
    if (!f) {
        std::cout << GetLastError() << "\n"; // code is 127 "The specified procedure could not be found."
        throw(std::runtime_error("could not locate function"));
    }

    f();
}

所以我尝试转储 dll (dumpbin/exports) 并看到测试函数定义为 ?tests@@YGXXZ

00000000 characteristics
52DD9D2B time date stamp Mon Jan 20 23:03:23 2014
    0.00 version
       1 ordinal base
       1 number of functions
       1 number of names

ordinal hint RVA      name

      1    0 00011037 ?tests@@YGXXZ = @ILT+50(?tests@@YGXXZ)

Summary

    1000 .data
    1000 .idata
    3000 .rdata
    1000 .reloc
    1000 .rsrc
    B000 .text
   10000 .textbss

如果我现在尝试

f_funci f = (f_funci)GetProcAddress(hMod, "?tests@@YGXXZ");

有效。

所以我的问题是,为什么函数解析错误?

编辑:

我正在使用 visual studio 2013 Express。

字符集未设置

最佳答案

除了“重复”帖子中给出的选项之外,还有另一个选项。此选项涉及为您的 dll 创建一个 .def 文件。

来自 http://msdn.microsoft.com/en-us/library/d91k01sh.aspx :

If you are exporting functions in a C++ file, you have to either place the decorated names in the .def file or define your exported functions with standard C linkage by using extern "C".

关于c++ - GetProcAddress 找不到函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21245326/

相关文章:

c++ - Qt窗口调整大小问题

c++ - 请帮助我理解 C++ 中的 delete 运算符?

c# - 无法在 C# 中加载 DLL 'tesseract.dll'

.net - Wix 安装程序将 DLL 放入 GAC 和程序文件夹中

C# 使用 Assembly 调用 DLL 中的方法

windows - 在远程 Windows 机器上动态运行 DLL?

c++ - 库的字符串编码应该符合 Unicode 还是灵活的?

c++ - 我应该将 RAII 应用于我分配的所有数组吗?

c++ - 指针 vs 引用并清理它

c++ - Visual C++ 中的容错 DLL 使用