Calling C++ functions in Delphi is quite direct and calling Delphi functions in C++. Note
What is meant that the default function call method of Delphi 1 is Pascal method, Delphi 4, Delphi
The default method of 5 is the optimized cdecl call method, that is, the register method. To be in C++
Implement function sharing with Delphi programs, unless there are sufficient reasons, the standard should be used.
The bare-system call method, that is, the stdcall method. In order for the C++ compiler to not mark the function as "mang"
led", causing the Delphi compiler to mistakenly believe that the function uses cdecl call method, it should be in C++
In the code, extern "C" describes the shared function, as shown in the following example: Prototype description: in C++
In:extern "C" int _stdcall TestFunc(); in Delphi:function TestFunc:integer;
stdcall;Call syntax: in C++: int i=TestFunc();
In Delphi:var i:integer;…
begin…
i:=TestFunc;…
end;The parameters of the shared function must be the variable type supported by both languages, which is the correct one.
Prerequisite for passing parameters. Variable classes such as Delphi's currency, string, set, etc.
Type, in C++, there is no corresponding variable type and cannot be used as a parameter of a shared function. Can
Pass string pointers with value parameters using PChar type. At this time, the user must pay attention to characters.
Recycling of string space.
The variable parameters in the Delphi language should be described as reference forms of the corresponding variable types in C++.
As follows: in Delphi: function TestFunc(var i:integer):integer; in C++
In: int TestFunc(int &i); code link implements code link between Delphi and C++
Connection can be done by static linking or dynamic linking.
1. Static linking method If the C++ program itself has a small amount of code, and there is no need to worry about C
The runtime library will have an interactive process, and static linking method can be used, that is, Delphi and C++
The object file (*.OBJ) is linked into the final executable file. The specific method is to use {$L}
Compile instructions to enable the Delphi compiler to automatically read the specified target file, as described below: function
TestFunc:integer;stdcall;{$L TestFunc.OBJ}2. Dynamic linking method is as follows
If C++ code is already quite comprehensive or a complete subsystem, the code volume is large, or
When using the C runtime library, in this case, the dynamic link library (DLL) method should be used. this
When, the following explanation should be made in the source code of two languages: In C++: int stdcall export
TestFunc(); in Delphi:function TestFunc:integer; stdcall; external
'TestFunc.DLL'; Object Sharing The object is shared between C++ and Delphi.
Now in terms of sharing object methods, this sharing can be divided into two levels: object (Object)
Class sharing and Class sharing. To implement object-level sharing, the programming language must have
Two prerequisites: able to define a pointer to an object created by another language; accessible
Methods in an object determined by a pointer.
To implement class-level sharing, you also need to consider: being able to create classes defined by another language
instance; space occupied by an instance can be freed from the heap; new classes are derived.
The following describes the method of implementing object sharing between Delphi and Borland C++.
1. To share Delphi objects in C++, to implement the call of Delphi objects from C++, you must first use Delphi
The interface part of the unit and the C++ header file indicate the interface of the object that needs to be shared, in the
The object interface defines what properties and methods the object contains, and explains the parts that can be shared.
The key to sharing of objects lies in sharing methods. In Delphi language, make an object capable
To be shared, it can be described as two interface parts, temporarily called "shared interface" and "implementation
interface ". Where a shared interface indicates which methods in the object can be shared by another language;
The current interface inherits the shared interface, and the method in the implementation part of the unit is used to implement the interface.
Define specific implementations. To define a Delphi object that can be shared by C++, the shared interface
Note: In the Delphi program, the method to be shared must be described as abstract (abstract).
And virtual (virtual); in C++ programs, the keywords "virtual" and "=0" must be used.
The shared object method must be explained as "pure virtual" by Delphi.
It must be described in both languages as the same calling method, usually using standard system calls
way (stdcall).
Below, let's give an example of these rules, assuming that there is such a Delphi object: TTestObject=classprocedure
Proc1(x:integer);function Func1(x:integer):PChar;procedure
Proc2;function Func2:integer;end;If C++ programs need to share their
Methods Proc1 and Func1 can be modified into the following form:
STestObject=classprocedure
Proc1(x:integer); virtual; abstract;
stdcall;function Func1(x:integer);
virtual; abstract; stdcall;end;TTestObject=class(STestObject)procedure
Proc1(x:integer);fuction Func1(x:integer):PChar;
procedure Proc2;function
Func2:integer;end; Make the following object prototype description in a C++ program: class STestObject
{virtual void Proc1(int x)=0;virtual char *Func1(int x)=0;};
In order to successfully access Delphi-defined classes in C++, the Delphi interface must be specified.
Contains a shareable "Factory Function" CreateTestObject,
The manufacturing function can be defined in a dynamic link library or object file (.OBJ), for example: Library
TestLib;exports CreateTestObject;function CreateTestObject:STestObject;
stdcall; beginResult:=TTestObject.Create;end;…
end. After such processing, this can now be used in C++ programs defined by Delphi
The object of the call is as follows: extern "C" STestObject stdcall
*CreateTestObject();void
UseTestObject(void) {STestObject *theTestObject=CreateTestObject(); theTestObject->Proc1(10);Char
*str=theTestObject->Func1(0);}When calling the manufacturing function CreateTestObject
When the Delphi side actually occupied the space of an object instance, the C++ program is in
After all processing for this object is completed, the space must be freed. The specific implementation can be
Define a class in Delphi, such as the shared method Free of the Proc1 mentioned above, to complete this
One task:STestObject=classprocedure Proc1(x:integer); virtual;
abstract; stdcall;function Func1(x:integer); virtual; abstract;
stdcall;procedure Free; virtual; abstract; stdcall;end;…
implementation…
procedure TTestObject.Free; begin…
end;…
end.2.Delphi share C++ objects usually, programmers will consider using Delphi to compile.
The user interface, so the Delphi code calls C++ code seems to be more practical. Actually, Delphi
The implementation method of sharing C++ objects is very similar to the above C++ shared Delphi objects. Use the same
Shared interface and implementation interface description method to define C++ classes: class STestObject
{virtual void Proc1(int x)=0;virtual char *Func1(int x)=0;};class
TTestObject :public STestObject {void Proc1(int x);char *Func1(int
x);void Proc2();int Func2();void Free();}; and then implement these methods
. Similarly, C++ objects require a corresponding manufacturing function. Here, DLL is used as an example STestObject
stdcall export *CreateTestObject() {return (STestObject *)
new TTestObject.Create;}Delphi code can be used to create a function CreateTestObject by calling the createTestObject,
It is easy to create an instance in C++, get the pointer value to that instance, and use this pointer
Value to call shared methods in the object. Of course, after completing the relevant processing of the object,
Don't forget to call Free to free up the space occupied.