Delphi and C ++ shared by implementing functions

xiaoxiao2021-03-06  56

Tell, I don't have a deep understanding of Delphi. I have written some small procedures, but given the legend of Borland, I still have seen the inside by Li Weina VCL, and the end feeling is not how fatful to Delphi, but the same In-depth understanding of the advantages and disadvantages of C and the deepening of some concepts, but still have respect for Borland, and also envy those who use delphi, there are many, :) This is the early days of collected from BBS Article, temporarily posted here, is an open door

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 , including: Delphi and C functions sharing; code static link and dynamic link; object 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 variable parameters in the Delphi language are described in the form of reference to the corresponding variable type in C , as follows:

In Delphi: Function Testfunc (VAR i: Integer): Integer; in C : int TestFunc (INT & I); Code Links to implement code links between Delphi and C can use static links or dynamic links.

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 If the C code is quite comprehensive or self-made, the amount of code is large, or the C run library, in which 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: It is possible to define a pointer to objects created by another language; the method in which the object determined by the pointer can be accessed.

To achieve a class sharing, you need to consider: You can create an instance of a class defined by another language; you can release the space occupied by an instance from the heap; 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: In the Delphi program, the method to share must be explained as an abstract, and virtual (Virtual); in the C program, you must use the key Word "Virtual" and "= 0" suffix, explain the method from Delphi shared into "Pure Virtual"; shared object method must be explained in two languages ​​to the same call mode, usually using standard system 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 description must contain a shared "manufacturing function" CreateTOBJECT, which manufacturing function Defined in 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, now C programs using the objects defined by Delphi, is called as follows: extern "C" STestObject stdcall * CreateTestObject (); void UseTestObject (void) {STestObject * theTestObject = CreateTestObject (); theTestObject-> Proc1 (10); Char * Str = THETESTOBJECT-> 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. The specific implementation can define a class in Delphi, such as the sharing method of PROC1 described above, to accomplish this task: StestObject = ClassProcedure Proc1 (x: integer); virtual; abstract; stdcall; function func1 (x: integer Virtual; abstract; stdcall; procedure free; virtual; abstract; stdcall; end; ... importation ... procedure ttestObject.free; begin ... End; ... End.2.delphi shared C object usually, programmers will consider using Delphi to compile The user interface, so the Delphi code calls C code seems to be more practical. 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 with the same shared interface: 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.

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

New Post(0)