Home > Back-end >  Delphi call C export member function
Delphi call C export member function

Time:10-06

Situation is that the third party to provide a DLL and a c + + interface file, I need through the interface to develop programs, I want to directly translated into Delphi, c + + interface file and then use the Delphi development,
The question now is whether the third party provides a class member functions in c + + interface file with no clear statement as __cdecl and __stdcall, I check it on the website is if not specified, the default class member functions are __thiscall, this pointer by ECX register transfer parameters, and __thiscall is only effective for c + +, Delphi is no similar keywords, so the call to the function that resulted from the value of this pointer is wrong, he had to write a small example to simulate the situation, kneeling, ask ace to give advice or comments

C + + code
 
//1. H
Class A {
Public:
Virtual void hsshowmessage ()=0;//the structure with no clear statement as __stdcall, the default should be __thiscall
};
Extern "C" __declspec (dllexport) A * CreateA (int a1, int b1);

//1. CPP
# include "1 h
"#include
#include
Class B: public A {
Private:
Public:
Virtual void hsshowmessage () {//hope calls to this function in Delphi, due to the function in the Delphi stdcall by default, so call passed this pointer value is wrong, we can see from the print information
Char TMP [100].
//by Delphi call here this pointer value and the value of the objects created in the Delphi,
Sprintf (TMP, "c.d ll - & gt; Hsshowmessage: b=% d, I=% d, j=% d ", int (this), I, j);
MessageBox (NULL, TEXT (TMP), TEXT (" "), MB_OK);
}
Int I;
Int j;
};

__declspec (dllexport) A * CreateA (int a1, b1) int {
//A * A: : CreateA (int a1, b1) int {
B * B=new B;
B - & gt; I=a1.
B - & gt; J=b1;
Char TMP [100].
Sprintf (TMP, "c.d ll - & gt; CreateA: b=% d, I=% d, j=% d ", int, (b) b - & gt; I, b - & gt; j);
MessageBox (NULL, TEXT (TMP), TEXT (" "), MB_OK);
Return b;
}

Delphi code
 
Type
TForm1=class (TForm)
Btn1: TButton;
Procedure btn1Click (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;

A=class
Procedure hsshowmessage (); Virtual; The abstract;//Delphi default is stdcall
end;

Const QTDLL='c.d ll;

The function CreateA (a, b: Integer) : a; Cdecl;

Var
Form1: TForm1;

Implementation

{$R *. DFM}

The function CreateA; External QTDLL;

Procedure TForm1. Btn1Click (Sender: TObject);
Var
Ta: A;
The begin
Ta: CreateA=(1, 2);
Application. MessageBox (PChar (' project1. Ext - & gt; Btn1click: a='+ IntToStr (Integer (ta))),' ');
Ta. Hsshowmessage;
end;

CodePudding user response:

__thiscall agreed mainly assigned to virtual function in c + +, the third party will usually provide a corresponding com interface, ask them if they have any

Never make this kind of business, you can look at the following practice
http://stackoverflow.com/questions/2667925/pass-a-delphi-class-to-a-c-function-method-that-expects-a-class-with-thiscal

CodePudding user response:

Other Delphi the function calling convention is the default register (fastcall), not stdcall

CodePudding user response:

The default method is called Delphi will certainly support,
  • Related