A dynamic link library is a collection of procedures and functions that can be called by applications and other DLLs. It contains public code or resources. Since the DLL code uses memory sharing technology, Windows also gives the DLL some higher permissions in some places, so the DLL can implement some functions that cannot be achieved by ordinary programs, such as implementing Windows HOOK, ISAPI, etc. At the same time, DLL also provides a convenient way for code sharing between different languages. Therefore, DLL is widely used in programming. This article will introduce how to create and use DLL in Delphi.
one. DLL library memory sharing mechanism
From the perspective of usage, DLL and unit are very similar. They can both be called by other project modules, but there are differences in their internal implementation mechanisms. If a program module uses a uses statement to reference a unit, when the compiler compiles the module, it will compile it together with the unit and link the compiled executable code to the program module. This is how a program module can The reason for calling procedures and functions in the referenced unit. When the same unit is referenced by multiple projects, each project contains the executable code of the unit. When multiple projects containing the unit are executed at the same time, the executable code of the unit will be updated multiple times with different projects. is transferred into the memory, causing a waste of memory resources. DLL is different. Even if it is called by a certain project, it is still independent after compilation. That is to say, after compilation, a DLL library forms a separate executable file and is not connected with any other executable file. Therefore, The DLL library is not subordinate to a specific project. When multiple projects call the same DLL library, only the first project transfers the DLL library into the memory. The other projects do not repeatedly transfer the same DLL library into the memory, but Read from the same shared memory area. Moreover, the execution code of the DLL is dynamically transferred during the running of the program, rather than being transferred into the memory together with the entire project when the program is running. This can eliminate the disadvantage of the same code occupying memory in multiple places caused by the unit.
2. Creation of DLL library in Delphi
In the Delphi environment, writing a DLL is not much different from writing a general application. In fact, the writing of DLL functions as the main body of DLL does not require any other special means except for differences in memory and resource management.
The format of general project files is:
PROgram project title;
uses clause;
Program body
The format of DLLs project files is:
library project title;
uses clause;
exprots clause;
Program body
There are two main differences between them:
1. Generally, the header of project files uses the program keyword, while the header of DLL project files uses the library keyword. Different keywords tell the compiler to generate different executable files. The program keyword is used to generate an .exe file, and the library keyword is used to generate a .dll file;
2. If the DLL wants to export functions or procedures for use by other applications, these functions or procedures must be listed in the exports clause. These functions or procedures themselves must be compiled using the export compilation directive.
Select the new... item in the Delphi main menu file, double-click the DLL icon in the pop-up window, and the DLL source module framework will be automatically given, as follows:
Libraryproject1;
{...annotation...}
uses
SysUtils,Classes;
begin
end.
Next, you can add the definitions of the procedures and functions you want to implement in the DLL between USES and begin, and use export and exports to export them so that other modules can reference them. Add initialization code between begin and end. Initialization code is used to initialize DLL variables. It should be noted that even if there is no initialization code, begin and end cannot be omitted, as in the following example:
libraryminmax;
functionMin(X,Y:Integer):Integer;export;
begin
ifX<YthenMin:=XelseMin:=Y;
end;
functionMax(X,Y:Integer):Integer;export;
begin
ifX>YthenMax:=XelseMax:=Y;
end;
exports
Minindex1,
Maxindex2;
begin
end.
After compilation and saving as minmax.DLL, a DLL library file is formed.
Access to three DLL libraries
There are two ways to access the DLL library, one is static reference and the other is dynamic reference.
Loading a DLL using a static reference method requires two things: creating an input unit for the DLL library, and using USES to connect the input unit to the program module where the DLL function is to be used. The only difference between the input unit created for the DLL library and the ordinary unit is that the procedures and functions declared at its interface do not give the real implementation code in its implementation part, but use the external keyword to declare the procedures and functions. The implementation details are delegated to external DLL modules.
The syntax for using the external command is as follows:
procedure/function procedure/function name; externalDLL module name;
The following is the input unit source file testdll.pas written for the minmax.DLL library created above. From it, we can see some differences between the input unit and the general unit. The code is as follows:
unittestdll;
interface
uses
functionMin(X,Y:Integer):Integer;
functionMax(X,Y:Integer):Integer;
implementation
functionMin;external'minmax.DLL';
functionMax;external'minmax.DLL';
end.
If an application wants to call a function in minmax.DLL, it only needs to add the testdll unit to its uses statement.
Dynamically loading a DLL requires three API functions of Windows. Loadlibrary, Freelibrary and GetprocAddress. The loadlibrary function is used to load the DLL library. Its calling format is as follows:
functionloadlobrary(DLLfileName:Pchar):THandle:
When a DLL library is no longer needed, the FreeLibrary function should be called to release it to free up valuable memory resources. The calling format is as follows:
procedureFreeLibrary(Libmodule:THandle)
Libmodule is the DLL library handle obtained by the LoadLibrary call. In the program segment between loading a DLL library with the loadlobrary function and calling FreeLibrary to release the DLL library, you can use the procedures and functions in the DLL library. The specific method of use is: use the GetprocAddress function to get the address of the function in the DLL library. Pass it to a function variable in the program, and then use the variable to call the DLL function. The GetprocAddress function is declared as follows,
functionGetprocAddress(Libmodule:THandle:procname:pchar):TFarProc:
As shown in the following example:
type
TTimeRec=record
Second:Integer;
Minute:Integer;
Hour:Integer;
end;
TGetTime=procedure(varTime:TTimeRec);
THandle=Integer;
var
Time:TTimeRec;
Handle:THandle;
GetTime:TGetTime;
...
begin
Handle:=LoadLibrary('DATETIME.DLL');
ifHandle<>0then
begin
@GetTime:=GetProcAddress(Handle,'GetTime');
if@GetTime<>nilthen
begin
GetTime(Time);
withTimedo
WriteLn('Thetimeis',Hour,':',Minute,':',Second);
end;
FreeLibrary(Handle);
end;
end;
When calling the dynamic link library, you should note that the required dynamic link library must be in the same directory as the application or in the WindowsSystem directory.
Dynamic link libraries are an important way to organize programs under Windows. Using dynamic link libraries can greatly protect the work done by users in different development tools and at different times and improve programming efficiency.