DLL (Dynamic Link Library) Programming

Source: Internet
Author: User

DLL is a common file, which integrates many functions of the program. In general, it cannot be executed directly. A common method of use is to use another *. EXE to call its execution to display its internal functions. Also, the *. ocx file is similar to this file, that is, the common COM file.
1. Brief
All functions of Windows APIs are included in the DLL, of which three are the most important.
(1) kernel32.dll
It contains functions used to manage memory, processes, and threads, such as the createthread function;
(2) user32.dll
It contains functions used to execute User Interface tasks (such as window creation and message transmission), such as the createwindow function;
(3) gdi32.dll
It contains functions for drawing and displaying text.
2. static and dynamic libraries
(1) static library
Functions and data are compiled into a binary file (usually with the extension. Lib ). When a static library is used, the linker copies these functions and data from the library and combines them with other modules of the application to create the final executable file (. EXE file ). when releasing a product, you only need to publish this executable file and do not need to publish the used static library. (2) dynamic library
When using a dynamic library, we usually provide two files: one imported Library (. Lib) file and one DLL (. dll) file. Although the suffix of the import library is also "lib", the import library file of the dynamic library is essentially different from the static library file. For a DLL, the import library file (. lib) contains the name of the function and variable exported by the DLL, and. the DLL file contains the actual functions and data of the DLL. When a dynamic library is used, you only need to link the imported DLL file when compiling the executable file. The function code and data in the DLL are not copied to the executable file, the required DLL is loaded only when the executable program runs. The DLL is mapped out of the address space of the process and then the exported function in the DLL is accessed. In this case, in addition to the executable file, the dynamic link library to be called by the program is also released.
3. Standard writing in import/export header files:
# Ifdef libdaq_exports
# Define libdaq_api _ declspec (dllexport)
# Else
# Define libdaq_api _ declspec (dllimport)
# Endif
This header file is automatically expanded when it is added to a customer's code. If the client code does not define libdaq_exports, libdaq_exports will be defined as _ declspec (dllimport), indicating that all functions with libdaq_exports header are imported from this DLL.
4. Name adaptation and "extern" C ""
When the C ++ compiler generates a DLL, it will modify the name of the exported function, and different compilers use different rules, so the modified name will be different. In this way, if different compilers are used to generate the DLL and the client code program accessing the DLL, the latter may encounter problems when accessing the export function of the DLL. To achieve universality, a qualifier extern "C" must be added ".
However, the use of the qualifier extern "C" can solve the problem of function naming when C ++ and C call each other. However, this method has a defect that it cannot be used to export a member function of a class, you can only export global functions.
5. Show the Loading Method to load the DLL
The loadlibrary function is required when dynamic library is loaded. This function maps the specified executable module to the address space of the calling process. The call prototype is:
Hmodule loadlibrary (lpctstr lpfilename );
The loadlibrary function can load not only DLL but also executable modules (exe ). When an executable module is loaded, it is mainly used to access some resources in the module, such as dialog box resources, bitmap resources, or ICON resources. The loadlibrary function has a string type (lpctstr) parameter, which specifies the name of the executable module, either a DLL file or an EXE file. If the call is successful, the loadlibrary function returns the handle of the loaded module. The return types hmodule and hinstance can be generic.
After loading the handle to the dynamic link library module, you need to find a way to obtain the address of the export function in the dynamic link library, which can be achieved by calling the getprocaddress function. This function is used to obtain the DLL export function address. Its prototype declaration is as follows:
Farproc getprocaddress (hmodule, lpcstr lpprocname );
Hmodule: Specifies the handle of the Dynamic Link Library module, that is, the return value of the loadlibrary function.
Lpprocname: A character pointer to a constant, specifying the name or serial number of the function to be exported by the DLL. If it is a sequence number, the sequence number must be in the low byte, and the high byte must be 0.
If the call is successful, the getprocaddress function returns the address of the specified export function; otherwise, null is returned.
For example:
Hinstance hinst;
Hinst = loadlibrary ("dlltest. dll ");
Typedef int (* addproc) (int A, int B );
Addproc add = (addproc) getprocaddress (hinst, "add ");
If (! Add)
Print ("failure ");
Else
Process next events
Freelibrary (hinst );
Call Syntax:
Bool freelibrary (hmodule );
6. Advantages and Disadvantages of loading dll:
When dynamic loading is adopted, the DLL can be loaded only when needed, while the implicit link method is relatively simple to implement. The link can be done well when writing client code, you can call the DLL export function at any time in the program. However, if the program needs to access more than 10 DLL files, if they are loaded using the implicit link method, these DLL files must be loaded into the memory when the program starts, and map to the address space of the calling process, which will increase the startup time of the program. In general, a function in a DLL needs to be accessed only when a certain condition is met during the program running. In other cases, the functions in these DLL do not need to be accessed. However, when all the DLL files are loaded into the memory, the resource waste is serious. In this case, you need to access the DLL by means of display loading, and load the required DLL only when necessary. That is to say, it is loaded to the memory only when necessary and mapped to the address control of the calling process. It should be noted that, when accessing the DLL through the implicit link method, the dynamic link library required by the process is also loaded through the loadlibrary function at the startup of the program.
7. dllmain Function
If the dllmain function is provided (this function can be selected), do not make too complex calls in this function. Because some core Dynamic Link Libraries may not be loaded when the dynamic link library is loaded. For example, use32.dll or gdi32.dll. The DLL compiled by ourselves will be loaded first.

 

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.