Create an instance of an exported C ++ class from Delphi

I followed a great article by Rudy Velthuis about using C ++ classes in DLLs. Everything was golden, except that I need access to some classes that do not have corresponding factories in the C ++ DLL. How can I build an instance of a class in a DLL? The classes in question are defined as

class __declspec(dllexport) exampleClass
{
public:
  void foo();
};

Now, without a factory, I don’t have a clear way to instantiate a class, but I know that this can be done since I saw SWIG scripts (.i files) that make these classes available to Python. If Python & SWIG can do this, I guess / hope there is a way to do this in Delphi too.

Now I know little about SWIG, but it looks like it is generating some kind of map for garbled C ++ names? Is it somewhere nearby? If you look at the export from a DLL, I believe that I could directly access the functions and constructor / destructor by index or with a malformed name, but that would be unpleasant; and will it work? Even if I can call the constructor, how can I make the equivalent of "new CClass ();" in Delphi?

+3
source share
4 answers

The right way to do this is to write a DLL wrapper that provides a factory for the class you want.

I'm not sure if SWIG works, but anything that relies on reverse engineering called mangling seems dubious.

, ++ ++-. ++.

, COM . , .

COM-, Delphi, python #

+5

swig: 1) API factory/destroyer. :

class C { 
      public : 
           C() {...}
           int M1(int P1) {...}
        } 

:

       C* New_C();
       Destroy_C(C*self);
       int C_M1(C*self,int P1) {}

api DLL

2) api , flat api pascal 3) api Pascal, :

 type TC = class


 private

      FObj : pointer;
 public

      constructor Create();
      destructor Destroy(); override;
      function m1(p1:integer: integer;
    ...

  constructor TC.Create();
  begin
     inherited;
     FObj := New_C();
  end;

  destructor TC.Destroy();
  begin
      Destroy_C(FObj);
      inherited;
  end;

  function TC.M1(P1:integer) : integer;
  begin
      Result := C_M1(FObj, P1);
 end;
+7

SWIG. , . SWIG, ObjectPascal, . GEOS GDAL/OGR. SWIG, , .

- ?

stefano.moratto@gmail.com www.csiat.it

+5

python , , .

Python ( ) ++ , .

, python , (, python DLL, msvc gcc)

, - -. , , , , , ( , ) , cdecl asm Delphi.

, , pascal, , , - HLL - ( ) ).

+2
source

Source: https://habr.com/ru/post/1739453/


All Articles