<分区>
我尝试使用 LoadLibrary
和 GetProcAddress
加载库以使用该库中的函数。然而,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。
字符集未设置。