Loading and unloading of dynamic libraries

Source: Internet
Author: User

The three related functions:

( loadlibrary,getprocaddress,freelibrary )

Dynamically loading DLLs
Dynamic loading means that you do not know before compiling which DLL functions will be called, it is exactly what functions should be called as needed during the run.
The method is to use the LoadLibrary function to load the dynamic link library into memory, and use the GetProcAddress function to obtain the DLL function's entry address dynamically. When a DLL file is explicitly loaded with LoadLibrary, it can be explicitly unloaded from memory at any moment by calling the FreeLibrary function.

Personally, for security reasons, the load process should be placed in the constructor, and releasing the dynamic library should be done in the destructor. This prevents memory wasting problems after multiple loads and less release in the release.

(1) LoadLibrary function
Note: The Safeloadlibrary function is also available in Delphi, which encapsulates the Loadlibrary function to load the Windowsdll or Linux shared object specified by the Filename parameter. It simplifies the loading of DLLs and makes loading more secure.
[Format]:

    1. function LoadLibrary (libfilename:P char): thandle;

Copy Code

[function]: Loads the DLL file specified by the parameter libfilename.
[Note]: parameter libfilename Specifies the DLL file name to be loaded, if Libfilename does not contain a path, the system will follow: current directory, Windows directory, Windows system directory, directory containing the current task executable, column in path Search for files in the order of the directories in the environment variable.
If the function operation succeeds, the instance handle of the loaded DLL library module is returned, otherwise an error code is returned, and the error code is defined as shown in the following table.

Error code

meaning

0

System memory is not enough, executable file is corrupted or invalid call

2

The file was not found

3

Path not found

5

Attempt to dynamically link a task error or have a sharing or network protection error

6

Libraries need to separate data segments for each task

8

There is not enough memory to start the application

10

The Windows version is incorrect

11

Executable files are illegal or not Windows applications, or in. EXE image has errors

12

The application is designed for a different operating system (e.g. OS/2)

13

The application is MS DOS 4. 0 Design

14

Type of executable file not known

15

Attempting to load a real-mode application (designed for earlier versions of Windows)

16

Attempting to load a second instance of an executable file that contains multiple writable data segments

19

An attempt was made to load a compressed executable file (the file must be unzipped before it can be loaded)

20

DLL file is illegal

21st

Applications require 32-bit extensions

If another application has loaded the DLL into memory before it is loaded with the LoadLibrary function in the application, the system will no longer mount another instance of the DLL, adding 1 to the DLL's reference count.
(2) GetProcAddress function
[Format]:

    1. function GetProcAddress (module:thandle; Procname:pchar): Tfarproc;

Copy Code

[function]: Returns the entry address of the procedure or function specified by parameter ProcName in the module specified by the parameter module.
[description]: The parameter Module contains the DLL handle of the called function, this value is returned by LoadLibrary, procname
is a pointer to a nil-terminated string that contains a function name, or it can be a sequence value for a function, but in most cases a function name is a more secure choice. If the function succeeds, returns the entry address of the procedure or function specified by the parameter procname in the DLL, otherwise returns nil.
(3) FreeLibrary function
[Format]:

    1. Procedure FreeLibrary (Module:thandle);

Copy Code

[description]: The DLL file specified by the parameter Module is unloaded 1 times from memory.
[Description]:module is a handle to the DLL library.] This value is returned by LoadLibrary. Because the DLL is loaded only once in memory, calling FreeLibrary first causes the DLL's reference count to be reduced by 1, and the DLL is unloaded if the count is reduced to 0.
[Note]: Each call to the LoadLibrary function should call a FreeLibrary function to ensure that no extra library modules remain in memory after the application ends, or a memory leak is caused.

Loading and unloading of dynamic libraries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.