Use DLL in the Delphi application

xiaoxiao2021-03-06  130

1. Create a DLL with Delphi

Delphi's DLL creation is not complex. Here you introduce you to Delphi's DLL creation method.

1] First create a new DLL project because DLL is compiled separately from the main program called it. If the DLL module has been built in the item called it (Project), it is removed from Project, and a new project is built. Simply select the NewProject item from the File menu, then add the DLL's PAS module file to the project, then the FORM1 that is automatically created is deleted.

2] In the DPR file in the DLL, the Program key is changed to library, which is declared as a dynamic link library, and add the exports statement after the Uses statement, indicating the function name of the DLL.

3] If the DPR file of the main program already has the statement of the DLL form Createform, it will be removed.

4] In the DLL's PAS file, the DLL is added to the DLL function or the declaration, the form, such as:

FunctionName (argment): boolean; export;

This function or process should be added to the form of CREATE and FREE (production and release) methods.

5] Compile the project.

2, DLL call

There are two ways to call the DLL, one is called when the application is loaded, and the other is called when the application is running. First introduce the call of DLL when loading:

(1) Call the DLL when loading

In the PAS file calling the DLL, the DLL function is submitted to the external statement, and the declaration should be located in the Uses statement of the Implementation, the form is as follows:

IMPLEMENTATION

Uses dialogs;

Function name (argment): boolean; far; external'callname ';

......

The string in the quotation marks behind the External keyword is the file name of the DLL. The DLL function can be referenced anywhere in the PAS file later.

The advantage of calling the DLL during loading is faster, and the code can also share the code.

(2) Call DLL at runtime

Another calling method of the DLL is to call at runtime. To call to Windows API functions: loadLibrary, getProcAddress, etc. Mainly used to call DELPHI and other languages, especially C compiled DLLs.

Suppose your DLL includes a function:

FunctionMyfunc (Aparam: Word): String; Export;

First add a sentence at the program Type type statement:

Type

TMYFUNC = Function (Aparam: Word): String;

The function of this sentence is like a function pointer declared in C .

Then define the following variables:

VAR

APTR: TFARPROC;

LHND: THANDLE;

String;

Where APTR, LHND two variable declares must have, S is the DLL function return value, depending on the situation.

Add the following statement to the DLL load:

LHND: loadlibrary ('path / DLL file name);

{Such as lhnd: loadlibrary ('c: /aa/bb.dll');

Aptr: = getProcaddress (lhnd, 'myfunc');

The DLL can be called directly below: S: = TMYFUNC (BPTR) (60); {Fill in the corresponding variable parameter} according to the function

After the call is completed, release the DLL occupied memory with FreeELIBRARY:

Freelibrary (LHND);

The following is given below, and an example of runtime call. This DLL is primarily used to check if the input password is correct, and the form contains an Edit edit box, two buttons Button, a tag Label, enter the password in the edit box. Returns true holiday based on comparison results.

{main.pas main program (runtime call DLL)}

Unit main;

Interface

Uses Wintypes, WinProcs, Classes, Graphics, Forms, Controls, Stdctrls, ExtCtrls

Type

TFORM1 = Class (TFORM)

EDIT1: TEDIT;

Label1: TLABEL;

Button1: tbutton;

Bevel1: TBevel;

Groupbox1: TgroupBox;

STATUSLBL: TLABEL;

Procedure Button1Click (Sender: TOBJECT);

END;

Tgetpass = function (aa: string): boolean;

VAR

FORM1: TFORM1;

GetPass: Tgetpass;

LHND: THANDLE;

APTR: TFARPROC;

IMPLEMENTATION

Uses dialogs;

{$ R * .dfm}

{Import routine from dll.takes password to matchand returns boolean.}

{Function GetPassword (Password: String): Boolean; Far; External'chkpWord ';

{Call Password Check Routine, Show Status Informtion.

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

if Edit1.Text = '' THEN

Begin

Messagedlg ('EntersamplePasswordfirst', MTInformation, [Mbok], 0);

Edit1.setfocus;

end

Else

Begin

LHND: = LoadLibrary ('chkpword.dll');

Aptr: = getProcaddress (lhnd, 'getpassword');

IF TgetPass (adt1.text).

Statuslbl.caption: = 'verifiedpassword'

Else

Statuslbl.caption: = 'invalid password';

Freelibrary (LHND);

END;

End.

{DLLFORM.PASDLL module}

Unit DLLForm;

Interface

Uses Wintypes, WinProcs, Classes, Graphics, Forms, Controls, Buttons, Sysutils, stdctrls;

Type

TPASSWORDFORM = Class (TFORM)

EDIT1: TEDIT;

Label1: TLABEL;

Bitbtn2: tbitbtn;

Bitbtn1: tbitbtn;

END;

Function getPassword (Password: string): boolean; export;

IMPLEMENTATION

Uses dialogs;

{$ R * .dfm}

Function getPassword (Password: String): Boolean

VAR

Passwordform: tpasswordform;

Begin

Result: = FALSE;

Passwordform: = tpasswordform.create (application);

Try

With PasswordForm Do

If ShowModal = MROK THEN

If Uppercase (Edit1.Text) <> Uppercase (Password) THEN

Messagedlg ('INVALID Password', MtWarning, [Mbok], 0)

Else

RESULT: = TRUE;

Finally

Passwordform.free;

END;

END;

End.http://tech.sina.com.cn 2000/12/12 11:40 Software World Lin Ling

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

New Post(0)