2

在 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");

有用!因此,下面的许多答案都与在运行时发现这个损坏的名称有关。

4

2 回答 2

3

我不会开始寻找这个被破坏的名字。它依赖于编译器(这也意味着依赖于版本),即使它有效,它也将是一个脆弱的解决方案。

我建议以另一种方式获取您的 RegisterFactoriesType 的地址。

假设您的插件中有一个 C 样式的 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,可以使用(&)的地址

于 2015-06-24T06:21:12.317 回答
1

我前段时间专门为此目的创建了一个库。这里有一个使用示例,希望对您有所帮助。

于 2015-06-24T06:35:55.713 回答