Implement function between Delphi and C ++ sharing

zhaozj2021-02-11  238

Implement function between Delphi and C sharing

Implement function between Delphi and C sharing

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 how Delphi and C program integration, including:

Sharing of Delphi and C ;

Code static link and dynamic link;

Sharing of objects.

Sharing of functions

In Delphi, the C function is called Delphi function, which is quite straightforward. It should be noted that Delphi 1 default function call mode is Pascal mode, Delphi 2, Delphi 3's default mode is an optimized CDECL call mode, the 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 a variable type supported by two 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 link

Implementing a code link between Delphi and C can use a static link or dynamic link.

1. Static link mode

If the code amount of the C program itself is small, it is not necessary to worry about it with the C run library, which generally selects a static link mode, that is, link Delphi and C target file (* .Obj) to the final executable file. The specific method is to use the {$ L} compile instruction to automatically read the specified target file using the Delphi compiler, 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 field is used, in which case the dynamic link library (DLL) is used. At this point, in the source code of the two languages, you should do the following description:

In C :

INT stdcall export testfunc ();

In Delphi:

Function testfunc: integer; stdcall;

External 'Testfunc.dll';

Object sharing

Object sharing between C and 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 achieve object-level sharing, programming language requires two prerequisites: It is possible to define a pointer to objects that are created by another language;

The method in the object determined by the pointer can be accessed.

To realize the sharing of the class, you need to consider:

Ability to create an instance of a class defined by another language;

You can release the space occupied by an instance from the heap;

Delicious new classes.

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 of the object you need to share in the interface portion of the Delphi unit and the C header file, define which properties and methods in the object interface, and explain 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 illustrated as abstract, and virtual (Virtual);

In the C program, you must use the keyword "Virtual" and "= 0" suffix to explain the method shared from the Delphi as "Pure Virtual";

The shared object method must be explained in both languages ​​to the same call mode, usually using standard system call mode (stdcall).

Next, these rules are given, assuming such a Delphi object:

TTestObject = Class

Procedure proc1 (x: integer);

Function func1 (x: integer): PCHAR;

PROCEDURE PROC2;

Function func2: integer;

END;

If the C program needs to share the methods PROC1, FUNC1, the above description can be modified below:

StestObject = Class

Procedure 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;

Do the following object prototype instructions in the C program:

Class switchject {

Virtual void proc1 (int x) = 0;

Virtual char * func1 (int x) = 0;

}

In order to successfully access Delphi defined in C , the Delphi interface must contain a shared "Factory function" CreateTestObject, which can be defined in dynamic link library or target file (.Obj) In, for example:

Library testlib;

Exports CREATETESTOBJECT;

Function CreateTestObject: StestObject; stdcall;

Begin

Result: = TTESTOBJECT.CREATE;

END;

...

End.

After such processing, this can now be used in the C program, the object defined by Delphi, the call mode is as follows: Extern "c" stestobject stdcall * createtestObject ();

Void useestObject (void) {

STESTOBJECT * THETESTOBJECT = CREATESTESTOBJECT ();

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, and the specific implementation can define a class in Delphi. , Such as the sharing method of PROC1 described above, to accomplish this task:

StestObject = Class

Procedure proc1 (x: integer); Virtual; Abstract; stdcall;

Function func1 (x: integer); virtual; abstract; stdcall;

. ;;;;;;;;;;;;;;;;

END;

...

IMPLEMentation

...

Procedure ttestObject.free;

Begin

...

END;

...

End.

2.Delphi shared C object

Typically, the programmer will consider using Delphi to prepare the 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 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 ();

}

These methods are then implemented. Similarly, C objects require a manufacturing function that corresponds to it, here is a DLL as an example.

STESTOBJECT stdcall export * createtestObject () {

Return (StestObject *) New TTestObject.create;

}

Delphi code can be created by calling the manufacturing function CreateTestObject, which is easy to create an instance in C to obtain a 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-4211.html

New Post(0)