Implement function between Delphi and C ++ sharing

zhaozj2021-02-08  236

Delphi has its own unique controlled development method, powerful database function, and fast compilation technology, making it quite attractive. As Delphi 3 offers a wealth of Internet applications, Delphi is increasingly one of the most important software development tools, which attracts many of the original Visual Basic, FoxPro, DBase or even C programmers, and one of these programmers need to solve it with Delphi. The important issue is how to take advantage of their original code. This article will introduce the method of integration of Delphi and C program, including: S Delphi and C sharing; S code static link and dynamic link; S object's sharing. The sharing of functions is quite straightforward in Delphi call with C call Delphi functions, it is necessary to note that the default function called Delphi 1 is Pascal mode, Delphi 2, Delphi 3's default mode is an optimized CDECL call mode, ie Register mode. To implement function sharing between C and Delphi programs unless there is a sufficient reason, the Standard system call mode, that is, the stdcall method. In order to make the C compiler not to mark the function as "MANG LED", the Delphi compiler mistakes that the function is used to use the CDECL call mode. In the C code, the shared function is explained in extern "c", as shown in the following example: Prototype Description: In C : 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 type of variable supported by both languages, which is the premise of correct transmission parameters. Currency, String, SET and other variable types such as Delphi, there is no corresponding variable type in C , and cannot be used as a parameter of the shared function. String pointers can be transmitted in a value-changer by a PCHAR type. At this time, the user must pay attention to the recycling of the string space. The change in paragraph in the Delphi language is described in the reference form of the corresponding variable type in C , in Delphi: Function Testfunc (VAR i: Integer): Integer; in C : int TestFunc (Int & I); code link Delphi and C implement code links can be used in a static link or dynamic link. 1. Static link mode If the code amount of the C program itself is small, there is no need to worry about the interaction with the C run library, which generally selects a static link mode, that is, link Delphi and C target file (* .Obj) link into the final Executable file. The specific method is to use the {$ L} compile instruction to automatically read the specified target file, which is as follows: function testfunc: integer; stdcall; {$ l testfunc.obj} 2. Dynamic link mode is quite Comprehensive or self-made a complete subsystem, the amount of code is large, or the C run library is used. In this case, the dynamic link library (DLL) should be used. At this point, in the source code of the two languages, the following description should be described in C : int stdcall export testfunc (); in Delphi: Function Testfunc: integer; stdcall; external 'testfunc.dll'; shared in C Object sharing between Delphi is mainly reflected in the sharing of Method, which can be divided into two levels: object (Object) Level Sharing and class (Class) Shared. To implement object-level sharing, programming language requires two prerequisites: s can define a pointer to objects created by another language; S can access methods in objects determined by pointer.

To achieve the sharing of the class, you should also consider: S can create an instance of a class defined by another language; S can release the space occupied by an instance from the heap; S derived a new class. The following describes how to implement an object share between Delphi and Borland C . 1.c Shared Delphi object To implement the Delphi object from C , first describe the interface that needs to be shared in the interface portion of the Delphi unit and the C header file, defining which attributes and methods in the object interface, and Description of the part of the shared. The sharing of objects is shared by the method. In the Delphi language, to make an object can be shared, you can explain it as two interface portions, called "shared interface" and "implementation interface". Where the shared interface indicates which methods in the object can be shared by another language; implement the interface, inherit the shared interface, and define a specific implementation for the method in the unit implementation. To define a Delphi object that can be shared by C , you should pay attention to: s In the Delphi program, the method to share must be illustrated as abstract, and virtual; s in the C program, must With keyword "virtual" and "= 0" suffix, explain the method from Delphi shared into "Pure Virtual"; s shared object method must be explained in two languages ​​to the same call mode, usually using standard systems Call mode (stdcall).

Next, these rules are given, assuming such a delphi object: TTestObject = ClassProcedure Proc1 (x: integer); function func1 (x: integer): pchar; procedure proc2; function func2: integer; end; if C program needs to share wherein the method Proc1, Func1, may be the above-described methods modified 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): PChar; procedure Proc2; fuction Func2: integer; end; follows object prototype described in the C program: class STestObject {virtual void Proc1 (int X) = 0; Virtual char * func1 (int x) = 0;}; In order to successfully access the Delphi defined in C , the Delphi interface must contain a shared "manufacturing function" CreateTObject "CreateTestObject This manufacturing function can be defined in a dynamic link library or target file (.Obj), for example: library testlib; exports createtestObject; function CreateTestObject: StestObject; stdcall; beginResult: = TTESTOBJECT.CREATE; END; ... end. After such processing, can now use the objects defined by Delphi in a C program, called by the following: extern "C" STestObject stdcall * CreateTestObject (); void UseTestObject (void) {STestObject * theTestObject = CreateTestObject (); theTestObject-> Proc1 ( 10); char * str = THETESTO Bject-> Func1 (0);} When the manufacturing function CreateTestObject is called, the space that has been occupied on the Delphi side, and the C program must consider the release of this space after all the processing of the object is completed. The implementation can define a class in Delphi, such as the shared method of PROC1 described above, to accomplish this task: StestObject = ClassProcedure PROC1 (x: integer); Virtual; Abstract; stdcall; function func1 (x: integer); Virtual; procad; procAl1; virtual; abstract; stdcall; end; ... importation ... procedure ttestObject.free; begin ... End; ... End.2.delphi shared C objects Usually, programmer considers using Delphi to program user interface So the Delphi code calls C code seems to be more actually. In fact, the implementation of Delphi shared C objects is very similar to the above C shared Delphi object.

Use the same shared interface to define C classes: class switchjedt {Virtual void proc1 (int x) = 0; Virtual char * func1 (int x) = 0;}; Class TTestObjedt: Public STESTOBJECT {void Proc1 (int X); char * func1 (int x); void proc2 (); int func2 (); void free ();}; then implement these methods. Similarly, C objects require a corresponding manufacturing function, here as an example of STESTOBJECT STDCALL EXPORT * CREATESTOBJECT () {Return (STESTOBJECT *) New TTestObject.create;} Delphi code can be used to call the manufacturing function CreateTObject, it is easy to Create an instance in C , get the pointer value to this instance, and call the sharing method in the object with this pointer value. Of course, after the relevant processing of the object is performed, don't forget to call free release space. Shao Hui, Real Estate Management Office, Fenghua City, Zhejiang Province

转载请注明原文地址:https://www.9cbs.com/read-1536.html

New Post(0)