C ++ DLL插件接口 [英] C++ DLL plugin interface

查看:289
本文介绍了C ++ DLL插件接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算做一个C + +插件界面ala 如何从dll(dll中的构造函数)?(с++)
创建一些类,但是有人提出,如果接口用于通过MinGW或Borland创建DLL, DLL加载器是用MSVC ++编译的,可能会有问题。因为唯一导出的函数声明为externC,我不知道为什么它不工作?

I'm planning on doing a C++ plugin interface ala How to create some class from dll(constructor in dll)?(с++) but concerns have been raised that if the interface is used to create DLLs via MinGW or Borland and the DLL loader is compiled with MSVC++, there might be problems. Since the only exported function is declared extern "C" I fail to see why it would not work ?

想法?

推荐答案

如果要跨编译器(和Release / Debug)和使用C ++,你需要更多的努力。

If you want to be compatible across compilers (and Release / Debug) and use C++, you need a little more effort.

基本上 - 你可以传递基本的数据类型,指向纯虚拟类。这些类不能包含任何数据成员,它们的析构函数不能是公共的,它们不应该有重载函数。

Basically - you are allowed to pass basic datatypes, and pointer to pure virtual classes. These classes must not contain any data member, their destructor must not be public and they should not have overloaded functions.

内存不能在一个dll中分配, 。这意味着没有异常,你需要某种类型的引用计数或返回机制。

Memory must not be allocated in one dll and released in another. This means no exceptions and you need some kind of reference counting or returning mechanism.

纯虚拟类(也称为接口)中的所有方法必须标记一个调用约定(我更喜欢stdcall)。

All methods inside pure virtual class (aka "Interface") must be marked with a call convention (I'd prefer stdcall).

动态强制转换也不可能,所以你可能需要一些功能在所有的接口来做的窍门(如COM中的QueryInterface )。

Dynamic casts are not possible as well, so you might need some functionality in all your interfaces to do the trick (like QueryInterface in COM).

这是因为win32上的大多数编译器都尝试兼容COM,并以COM兼容的方式解决相同的问题。对于获得第一个接口,你需要一个从dll导出的纯C函数。

This works because most compiler on win32 try to be COM compatible and solve the same problems in a COM compatible way. For getting the first interface, you need a plain C function that is exported from the dll.

如果你只是使用C函数和C数据类型,一切都会工作。但是你只限于C没有类&继承。

If you just use C functions and C data types, everything will work as well. But then you are limited to C without classes & inheritance.

我希望有帮助。

是不是一个问题:

第一:如果你使用C函数与C数据类型,一切都被定义,没有名称mangling(例外:在VS与STDCALL,通过链接器指令将名称重新映射为正常C名称)

1st: if you use C functions with C data types, everything is defined, there's no name mangling (exception: in VS with STDCALL, you need to remap the name to the "normal" C name via Linker directive)

第二:类中的方法不导出,因此不会被转换。你通过指向纯虚拟类(又名接口)的指针调用方法。这使用偏移量,没有名称。你仍然不能使用析构函数,因为vtbl内析构函数的位置不是固定的,只要我知道。

2nd: Methods inside classes are not exported and thus not mangled. You call methods via pointer to pure virtual classes (aka "Interfaces"). This uses an offset and no name. You still can't use the destructor, as the position of the destructor inside the vtbl is not fixed as far as I know.

如果将结构传递给函数/方法,请务必修复对齐方式。它不是在不同的编译器之间定义的。

If you pass structs to functions / methods, be sure to fix the alignment. It is not defined across different compilers.

这篇关于C ++ DLL插件接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆