在 Visual C++ 2013 中,我试图从“插件”项目中导出一个函数:
void registerFactories(FactoryRegister<BaseShape> & factoryRegister);
它被编译成一个动态 dll,它将在运行时由一个“应用程序”项目链接。首先我定义函数指针类型:
typedef void (*RegisterFactoriesType)(FactoryRegister<BaseShape> &);
用作:
auto registerFactories = (RegisterFactoriesType)GetProcAddress(dll, "registerFactories");
if (!registerFactories) {
if (verbose) {
ofLogWarning("ofxPlugin") << "No factories for FactoryRegister<" << typeid(ModuleBaseType).name() << "> found in DLL " << path;
}
FreeLibrary(dll);
return false;
}
但是,GetProcAddress
返回 NULL。
我可以确认我可以导出 C 函数(使用extern "C"
)并使用从同一个 DLL 导入它们GetProcAddress
,但是我导入 C++ 函数失败。例如这有效:
extern "C" {
OFXPLUGIN_EXPORT void testFunction(int shout);
}
然后
auto testFunction = (TestFunction)GetProcAddress(dll, "testFunction");
if (testFunction) {
testFunction(5);
}
所以我的假设是我需要以某种方式考虑为registerFactories
. 由于它需要处理 C++ 类型,理想情况下我想在没有export "C"
.
这是dumpbin.exe
看到的:
文件 examplePlugin.dll 的转储
文件类型:DLL
Section contains the following exports for examplePlugin.dll
00000000 characteristics
558A441E time date stamp Wed Jun 24 14:46:06 2015
0.00 version
1 ordinal base
2 number of functions
2 number of names
ordinal hint RVA name
1 0 001B54E0 ?registerFactories@@YAXAEAV?$FactoryRegister@VBaseShape@@@ofxPlugin@@@Z = ?registerFactories@@YAXAEAV?$FactoryRegister@VBaseShape@@@ofxPlugin@@@Z (void __cdecl registerFactories(class ofxPlugin::FactoryRegister<class BaseShape> &))
2 1 001B5520 testFunction = testFunction
Summary
86000 .data
8E000 .pdata
220000 .rdata
E000 .reloc
1000 .rsrc
65D000 .text
编辑 :
registerFactories
不是给的名字GetProcAddress
。通过从 bindump 手动复制损坏的名称,例如:
auto registerFactories = (RegisterFactoriesType)GetProcAddress(dll, "?registerFactories@@YAXPEAV?$FactoryRegister@VBaseShape@@@ofxPlugin@@@Z");
有用!因此,下面的许多答案都与在运行时发现这个损坏的名称有关。