Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use C++ Classes exported by a dll in Delphi

Tags:

c++

dll

delphi

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, ...)?

like image 630
Tobias Langner Avatar asked Sep 30 '09 06:09

Tobias Langner


2 Answers

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" 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.

  • 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.

like image 159
Rob Kennedy Avatar answered Sep 19 '22 23:09

Rob Kennedy


You can't use C++ classes exported from a DLL as far as I know in Delphi; you can use C functions and you can import COM classes into Delphi.

like image 28
imekon Avatar answered Sep 21 '22 23:09

imekon