Call the DLL written in DLPHI

xiaoxiao2021-03-06  201

1, write and link DLL using Visual C 6.0

Open the Visual C 6.0 integrated development environment, create a new Win32 Dynamic-Link library type engineering CDLL, create a new C language source file CDLL.c in the project. The content in the source file is as follows:

__DECLSPEC (DLLEXPORT) INT MAX (INT X, INT Y) / * Compare two integer variable size functions MAX * /

{

IF (x> y)

Return X;

Else

Return Y;

}

After the input is completed, press the F7 key to compile and link CDLL.dll, and then find a DLL file called CDLL in the debug subfolder stored in the project, which is generated by the C language source program. DLL.

2, write the application to call the DLL using Delphi 7

Open the Delphi 7 integrated development environment, drag and drop 3 Edit control Edit1, Edit2, Edit3, and 1 Button Control Button1 on the default form form Form1, and clear the Text properties of 3 Edit controls in the Object Inspector. Then enter the importation of the Unit1.Pas file generated by default:

Function Max (x, y: integer): integer; stdcall external 'cdll.dll';

Returns Form1, double-click the Button1 control, and enter: in the generated event handler:

Edit3.Text: = INTSTR (Max (STRTOINT (EDIT1.TEXT), STRTOINT (Edit2.Text));

After the input is completed, save this project. Finally, the cdll.dll file copy to the folder saved in the Project.

3, test

In the Delphi integrated development environment, press F9 to run the Project just written. Enter 2 in Edit1, enter 4 in Edit 2, and then click Button1, you can see that 4 will appear in EDIT3, and the test is successful.

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

New Post(0)