如何使用在Delphi中由dll导出的C ++类 [英] How to use C++ Classes exported by a dll in Delphi
问题描述
有没有办法使用由win32 dll导出的C ++类在Delphi for win32?有没有其他方法来养成类似的东西(COM,.NET,...)?
is there a way to use C++ classes exported by a win32 dll in Delphi for win32? Are there other ways to archieve similar things (COM, .NET, ...)?
推荐答案
类。您只能导入功能。 Rudy Velthuis详细介绍了该主题。虽然您不能直接使用导出的C ++类,但他描述一对夫妇的技术来实现相同的效果:
You can't import a class. You can only import functions. Rudy Velthuis has written at length on the topic. Although you can't directly use an exported C++ class, he describes a couple of techniques to achieve the same effect:
-
Flatten对象,所以在调用端没有对象现在只是一个指针,它被传递给DLL和一系列包装对象方法的函数的参数。编写包装器非常简单,虽然很繁琐。
"Flatten" the object, so on the calling side there is no object anymore, just a pointer that gets passed to the DLL along with other parameters for a series of functions that wrap the object's methods. Writing the wrapper is very simple, although it can be tedious.
使用纯虚拟类。 Windows C ++编译器和Delphi通常具有相同的VMT布局,因此如果C ++类可以通过纯虚拟方法列表描述,您可以创建一个等效的Delphi声明,使用DLL返回的对象指针进行一些类型转换,
Use pure virtual classes. Windows C++ compilers and Delphi have generally the same VMT layouts, so if the C++ class can be described by a list of pure virtual methods, you can create an equivalent Delphi declaration, do some type-casting with the object pointer returned by the DLL, and proceed.
文章中提供了两种方式的完整示例。
Complete examples of both ways are given in the article.
这篇关于如何使用在Delphi中由dll导出的C ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!