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

查看:137
本文介绍了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 ?

想法?

推荐答案

如果要跨编译器兼容(和释放/调试)并使用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中,而是在另一个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数据类型,一切都被定义,没有名称变化(例外:在VS与STDCALL,你需要通过Linker指令将名称重新映射到正常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天全站免登陆