The establishment and use of dyncty link library (DLL) in Delphi
The dynamic link library is a set of processes and functions that can be called by applications and other DLL, which contains public code or resources. Since the DLL code uses memory sharing technology, some local Windows also gives some higher privileges, so that some general procedures can be implemented, such as the Hook, ISAPI, etc. of Windows. At the same time, the DLL also provides a convenient way for different language code sharing. Therefore, the DLL is more widely used in programming, and this article will show how to establish and use the DLL in Delphi.
One. DLL library memory sharing mechanism
From the use of the effect, DLL and Unit are very similar, they can be called by other engineering modules, but they have different differences in the internal implementation mechanism. If a program module is used to reference a UNIT, the compiler is compiled with the unit, and the compiled executable code link is connected to this program module, which is a program module. Call the cause of the process and function of the referenced Unit. When the same unit is quoted by multiple projects, each project includes an executable code of the Unit. When multiple projects containing the Unit, the Unit executable code will be used multiple times with different projects. Templaced in memory, causing waste of memory resources. Dll is different, even if it is called by a project, it is still independent after compiling, which means that a DLL library forms a separate executable, and does not connect with any other executable, thus The DLL library does not belong to a particular project. When multiple project calls the same DLL library, only the first project is transferred to the memory, and the rest of the project does not repeat the same DLL library to memory, but Go to the same shared memory area. Also, the execution code of the DLL is dynamically transferred during the program run, rather than the entire project to memory as the unit is running at the program. This eliminates the disadvantage of the same code from the same code brought by Unit.
Establishment of DLL library in two Delphi
In the Delphi environment, writing a DLL is not much different from writing a general application. In fact, as the writing of the DLL function of the DLL body, in addition to the management of the memory, the resources are managed, other special means are not required.
The format of the general engineering file is:
Program project title;
Uses clause;
Program body
And the format of the DLLS project file is:
Library project title;
Uses clause;
EXPROTS clause;
Program body
Two main differences:
1. The header of the general engineering file with the program keyword, while the DLL project file header uses library keywords. Different keyword notification compilers generate different executables. The use of the Program keyword is the .exe file, and use the library keyword is the .dll file;
2. If the DLL wants to output functions or processes for other applications, these functions must be listed in the Exports clause. These functions or process itself must compile with Export compilation instructions.
Select the new ... item in the Delphi main menu file, double-click the DLL icon in the pop-up window, which will automatically give the DLL source module framework, as follows:
Library Project1;
{... comment ...}
Uses
Sysutils, Classes;
Begin
End.
Next, you can join the definitions of the procedures and functions implemented in this DLL between Uses and Begin, and will be introduced with Export and Exprots so that other modules are referenced, and the initialization code is added between Begin and End. The initialization code is used to initialize the DLL variable. It should be noted that even if there is no initialization code, BeGin and END are not omitted, as in the following example:
Library minmax;
Function min (x, y: integer): integer; export;
Begin
= x else min: = y;
END;
Function Max (X, Y: Integer): Integer; Export;
Begin
IF x> y Then Max: = x else max: = y;
END;
Exports
MIN Index 1,
Max Index 2;
Begin
End.
After compiling, after the MinMax.dll store, a DLL library file is formed. Access to the three DLL library
There are two ways to access the DLL library, one is a static reference, and the other is a dynamic reference.
Use static reference to this method to load the DLL to do two things: Create an input unit for the DLL library, and connect the input unit to the program module to use the DLL function with Usees. The difference between the input unit created for the DLL library is only: the process, function, which is declared at its interface, does not give real implementation code, but use the External keyword to process the process, function Implementation details Entrust to the external DLL module.
The use syntax of the external command is as follows:
Procedure / function process / function name; External DLL module name;
The input unit source file Testdll .pas written for the MinMax.dll library created above, it can be seen from which some differences in the input unit and the general unit are as follows:
UNIT TESTDLL;
Interface
Uses
Function min (x, y: integer): integer;
Function Max (X, Y: Integer): Integer;
IMPLEMENTATION
Function min; external 'minmax.dll';
Function max; external 'minmax.dll';
End.
An application wants to call the function in MinMax.dll, you only need to add a TestDLL unit in its USES statement.
Dynamically load DLL, use three API functions for Windows. LoadLibrary, Freeelibrary and GetProcaddress. The LoadLibrary function is used to load the DLL library, and its call format is as follows:
Function LoadLobrary (DLLFileName: Pchar): Thandle:
When you no longer need a DLL library, you should call the Freelibrary function to empty the valuable memory resources, and its call format is as follows:
Procedure Freelibrary (libmodule: thandle)
Libmodule is a DLL library handle that is called by LoadLibrary. In a block with the loadlobrary function, a DLL library is loaded and called FreeELBRAR, the process and function in the DLL library can be used, and the specific usage method is: use the getProcAddress function to put the address of the function in the DLL library Pass a function variable in the program and use this variable to implement the call of the DLL function. The GetProcaddress function is called,
Function GetProcaddress (libmodule: thandle: procname: pchar): Tfarproc:
As shown in the following example:
Type
TTIMEREC = Record
Second: Integer;
Minute: integer;
Hour: integer;
END;
Tgettime = procedure (var time: TTIMEREC);
Thandle = integer;
VAR
TIME: TTIMEREC;
Handle: thandle;
GetTime: tgettime;
...
Begin
Handle: = loadLibrary ('datetime.dll');
IF Handle <> 0 THEN
Begin
@Gettime: = getProcadDress (Handle, 'getTime');
IF @gettime <> nil dam
Begin
GetTime (TIME);
WITH TIME DO
Writeln ('Time IS', HOUR, ':', Minute, ':', Second);
END;
Freelibrary (Handle);
END;
END;
Be careful when calling the dynamic link library, the required dynamic link library must be with the application in the same directory or Windows System directory. The dynamic link library is an important way for Windows's programming organizations. Using the dynamic link library can greatly protect users in different development tools, different periods, and improve programming efficiency.