在Visual C ++ 2013中,我试图从一个插件中导出一个函数'项目:
void registerFactories(FactoryRegister<BaseShape> & factoryRegister);
其中包含一个动态dll,它将在运行时由一个&#39;应用程序&#39;项目。首先我定义函数指针类型:
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"
)并使用GetProcAddress
从同一个DLL导入它们,但导入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");
有效!因此,下面的许多答案都与在运行时发现这个受损的名称有关。
答案 0 :(得分:3)
我不会开始寻找受损的名字。它依赖于编译器(这也意味着版本依赖),即使它工作也是一个脆弱的解决方案。
我建议以另一种方式获取RegisterFactoriesType的地址。
假设你的插件中有一个C-style init函数(其地址可通过GetProcAddress获得)我会这样做:
struct init_data_t
{
RegisterFactoriesType factory ;
... other members
} ;
然后在init(所以在DLL中)
void init(init_data_t *data)
{
init_data->factory = &dll_factory ;
}
基本上你要求DLL为你提供工厂功能的地址。 dll代码不需要GetProcAddr,它可以使用(&amp;)
的地址答案 1 :(得分:1)