I. Principles
1. dll is a file containing multipleProgramTheCodeAnd data library.
2. dll advantages
A. Use fewer resources
When multiple programs use the same function library, the DLL can reduce the amount of code duplicated in the disk and physical memory. This not only can greatly affect the programs running on the foreground, but also can greatly affect other programs running on the Windows operating system.
B. Promote modular architecture
DLL facilitates the development of modular programs. This can help develop large programs that require multiple language versions or that require a modular architecture.
C. Simplified deployment and Installation
When the functions in the DLL need to be updated or repaired, you do not need to re-establish the link between the program and the DLL to deploy and install the DLL. In addition, if multiple programs use the same DLL, the soy milk of multiple programs will benefit from the update or repair. This issue may occur more frequently when a third-party DLL is updated or repaired on a regular basis.
3. dll connection method
When loading a DLL in an application, you can use two link methods to call the exported DLL function. These two link methods are dynamic links during loading and dynamic links during runtime.
In the dynamic link during loading, the application explicitly calls the exported DLL function just like calling a local function. To use dynamic links during loading, when compiling and linking an application, the connector provides the header file and imported/imported files, and provides the system with the information required to load the DLL, parse the location of the exported DLL function during loading.
In the dynamic link at runtime, the application calls the loadlibrary function to load the DLL at runtime. After successfully loading the DLL, you can use the getprocaddress function to obtain the address of the exported DLL function to be called. When using dynamic links during running, you do not need to use import/export files.
The following list describes the conditions for when to use the dynamic link at loading and when to use the dynamic link at runtime:
- Startup Performance
If the Initial Startup Performance of the application is important, use the dynamic link at runtime.
- Ease of use
In the dynamic link during loading, the exported DLL function is similar to a local function. This allows you to conveniently call these functions.
- Application logic
In the dynamic link during runtime, the application can be split to load different modules as needed. This is important when developing multilingual versions.
Ii. Create DLL
Export DLL Functions
To export a DLL function, you can add a function keyword to the exported DLL function, or create a block definition file (. Def) to list the exported DLL function.
1. Use Function keywords
Declare the exported functions: _ declspec (dllexport)
Declare to use the exported DLL function: _ declspec (dllimport)
Generally, a header file containing the define statement and ifdef statement is used to separate the export and import statements, which are used in the DLL source program and the client source program respectively.
Create a DLL (empty project) and add the header file and. cpp file respectively.
Dll_sen.h File
# Ifndef _ xx
# DEFINE _ xx
Extern "C" _ declspec (dllimport) int add (int x, int y );
# Endif
Dll_sen.cpp File
Extern "C" _ declspec (dllexport) int add (int x, int y)
{
Return X + Y;
}
Generate a solution, obtain the DLL, import the database, and the header file, and submit the file to the user.
Note: because it is a dynamic link during loading, the header file must be declared as a dllexport; otherwise, it is used as a local function for symbolic parsing.
2. Use the module definition file (. Def)
When using a module definition file, you do not need to add function keywords to the exported DLL function. In the module definition file, you can declareLibraryStatement andExportsStatement.
Dll_def.def File
Library "dll_def"
Exports
Add
Dll_def.cpp File
Int add (int x, int y)
{
Return X + Y;
}
Dll_def.h File
Int add (int x, int y );