A dynamic-link library is a collection of procedures and functions that can be called by applications and other DLLs, which contain public code or resources. Because the DLL code uses the memory sharing technology, in some places Windows also gives the DLL some higher permissions, so the DLL can implement some of the functions that the general program cannot achieve, such as the implementation of Windows hooks, ISAPI and so on. At the same time, DLLs provide a convenient way to share code between different languages. Thus DLL is widely used in programming, this article will describe how to build and use DLLs in Delphi.
I. DLL library memory sharing mechanism
From the view of use, DLL and unit are similar, they can be called by other engineering modules, but there are differences in the internal implementation mechanism. If a unit is referenced in a program module with a uses statement, the compiler compiles the module together with the unit and links the compiled executable code to the program module, which is why a program module can invoke procedures and functions in the referenced unit. When the same unit is referenced by multiple projects, each project contains the unit's executable code, and when multiple projects containing the unit execute simultaneously, the unit's executable code will be transferred to memory multiple times with different projects, resulting in a waste of memory resources. DLL is different, even if it is called by a project, compiled is still independent, that is, after compiling, a DLL library to form a separate executable file, and not with any other executable file, so DLL library does not belong to a specific project, When multiple projects invoke the same DLL library, only the first project calls the DLL library into memory, and the rest of the project does not repeatedly call into the same DLL library to memory, but to the same shared memory area to read. Also, the execution code of the DLL is dynamically transferred during the run of the program, rather than being transferred into memory with the entire project when the unit is running. This eliminates the disadvantage that the same code that the unit brings in memory is used in many places.
The establishment of DLL library in two Delphi
In the Delphi environment, writing a DLL is not much different from writing a generic application. In fact, as a DLL body DLL functions, in addition to memory, resource management is different, there is no need for other special means.
The format of the general engineering document is:
Program Project title;
Uses clause;
Program Body
The DLLs project file format is:
Title of the Library project;
Uses clause;
Exprots clause;
Program Body
Their main differences are two points:
1. General engineering document Header with the program keyword, and DLL project file header with the library key word. Different keywords tell the compiler to generate a different executable file. The. exe file is generated with the program keyword, and the. dll file is generated with the Library keyword;
2. If the DLL is going to output functions or procedures for use by other applications, you must include these functions or procedures in the exports clause. These functions or procedures themselves must be compiled with export compilation instructions.
In the Delphi main menu file, select New ... Item, double-clicking the DLL icon in the popup window will automatically give the DLL source module framework as follows:
The following is a reference fragment: Library Project1; {... Comments ...} Uses Sysutils, Classes; Begin End |
Next, you can add the definitions of the procedures and functions that you want to implement in the DLL between uses and begin, and draw them out with the export and exprots letters, so that other module references can be added to the initialization code between begin and end, and the initialization code is used to initialize the DLL variables. It should be noted that even if no initialization code begin and end can be omitted, the following example:
The following is a reference fragment: Library Minmax; function Min (X, Y:integer): Integer; Export Begin If x < y then min: = 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; Egin
End. |
After compiling, and with Minmax. DLL, a DLL library file is formed.
The establishment and use of dynamic link library (DLL) in Delphi from: Free paper net
Access to the three DLL libraries
There are two ways to access a DLL library, one for static references and one for dynamic references.
Loading a DLL with a static reference is two things to do: Create an input unit for the DLL library, and
Use uses to connect the input unit to the program module where you want to use the DLL function. The difference between an input unit created for a DLL library and a normal cell is that the procedure and function declared at its interface do not give the actual implementation code in its implementation, but instead delegate the implementation details of the procedure and function to the external DLL module using the external keyword.
The syntax for using the external command is as follows:
Procedure/function process/function name; external DLL module name;
The Minmax created for the above is given below. DLL library writes the input unit source file Testdll. Pas, from which you can see some differences between the input unit and the general unit, as shown in the code below:
The following is a reference fragment: 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 that wants to invoke Minmax. DLL, you only need to add the Testdll unit to its uses statement.
Dynamically loading DLLs, using Windows three API functions. Loadlibrary, FreeLibrary and GetProcAddress. The LoadLibrary function is used to load the DLL library in the following format:
function Loadlobrary (Dllfilename:pchar): Thandle:
When you no longer need a DLL library, you should call the FreeLibrary function to release it to empty out valuable memory resources in the following format:
Procedure FreeLibrary (Libmodule:thandle)
The Libmodule is the DLL library handle that was obtained by the LoadLibrary call. You can use procedures and functions in the DLL library when you load a DLL library with the Loadlobrary function and call FreeLibrary to release the DLL library. The concrete use method is: Use the GetProcAddress function to pass the address of the function in the DLL library to a function variable in the program, and then use the variable to implement the DLL function call. The GetProcAddress function has the following reputation,
The following is a reference fragment: 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 Then Begin GetTime (time); With Time do Writeln (' The time is ', Hour, ': ', Minute, ': ', Second); End FreeLibrary (Handle); End End |
When invoking a dynamic-link library, be aware that the required dynamic-link library must be in the same directory as the application or under the Windows System directory.
Dynamic link library is an important way to organize the program under Windows, and using dynamic link library can greatly protect users ' work in different development tools and different periods, and improve the efficiency of programming.
Go Establishment and use of dynamic link library (DLL) in Delphi