Implement function and object sharing between Delphi and C + +

Source: Internet
Author: User
Tags abstract implement

In Delphi call C + + function and C + + call Delphi function is quite direct, it should be noted that Delphi 1 The default function call mode is Pascal, Delphi 4, Delphi 5 Default Way is optimized cdecl call way, That is the Register method. To implement function sharing between C + + and Delphi programs, you should use a standard system call method, or StdCall method, unless there is a good reason. In order for the C + + compiler not to mark the function as "mangled", so that the Delphi compiler mistook the function for a cdecl invocation, in C + + code, an extern "C" is used to describe the shared function, as shown in the following example: Prototype Description: in C + +: extern "C" int _stdcall testfunc (); in Delphi: function Testfunc:integer;

stdcall; Calling syntax: in C + +: int i=testfunc ();

In Delphi: Var i:integer ....

Begin ...

I:=testfunc;

End; The parameter of the shared function must be a variable type supported by both languages, which is the precondition for the correct passing of parameters. Such as Delphi currency, string, set, and other variable types, in C + + has no corresponding variable type, can not be used as the parameters of the shared function. You can pass a string pointer with a Pchar type as a value parameter, at which point the user must pay attention to the collection of string spaces.

The variable parameters in Delphi 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 + +

Medium: int testfunc (int &i); Code links implement code links between Delphi and C + + by using static or dynamic links.

1. Static link If the code of the C + + program itself is very small, and no need to worry with the C Run-time Library will have interactive process, the general choice of static link, that is, Delphi and C + +

Target file (*. OBJ) are linked into the final executable file. The specific method is to use {$L} to compile instructions, so that the Delphi compiler automatically read the specified target file, described below: function testfunc:integer;stdcall; {$L Testfunc.obj} 2. Dynamic link if the C + + code is fairly comprehensive or self-contained, the code is large, or the C Run-time Library is used, in which case the dynamic link library (DLL) should be used. At this point, the source code in both languages should be described as follows: in C + +: int stdcall export TestFunc (); in Elphi: function Testfunc:integer; Stdcall;external ' TestFunc.DLL '; object sharing between C + + and Delphi is mainly embodied in the sharing of object methods (method), which can be divided into two levels: object-level Sharing and class ( Class) Level share. To achieve object-level sharing, a programming language requires two prerequisites: the ability to define pointers to objects created by another language, and to access methods in objects determined by the pointer.

To implement class-level sharing, it is also necessary to consider the ability to create an instance of a class defined by another language, to free up the space occupied by an instance from the heap, and to derive a new class.

Here's how to implement object sharing between Delphi and Borland C + +.

1.c++ share Delphi object to implement Call Delphi object from C + +, first of all, in the interface part of Delphi Unit and C + + header file to describe the interface of the object that needs to be shared, define which properties and methods are included in the object interface, and explain the parts that can be shared.

The key to sharing objects is the sharing of methods. In the Delphi language, to enable an object to be shared, it can be described as a two-part interface, tentatively known as "Shared interface" and "Implementation Interface". Where the shared interface indicates which methods in the object can be shared by another language, the implementation interface inherits the shared interface and defines the implementation in the Unit implementation section for the method in the implementation interface. To define a Delphi object that can be shared by C + +, the description of the shared interface should be noted: In the Delphi program, the method to be shared must be described as abstract and virtual, and in C + + programs, the keyword "virtual" and "=0" must be used. Suffix, the method of sharing from Delphi is described as "pure virtual"; The shared object method must be described in both languages as the same invocation, typically using standard system invocation (stdcall).

Here's an example of these rules, assuming there's a Delphi object like this: Ttestobject=classprocedure

Proc1 (X:integer); function Func1 (x:integer):P char;procedure

Proc2;function Func2:integer;end; If a C + + program needs to share methods Proc1, FUNC1, you can modify the above description to 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):P char;

Procedure Proc2;fuction

Func2:integer;end in C + + program to do the following object prototype description: Class Stestobject

{virtual void Proc1 (int x) =0;virtual char *func1 (int x) = 0;};

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.