I. Concepts
DLL: Dynamic Link Library, that isDynamic Link LibraryThis library contains code and data that can be used by multiple programs at the same time.
It is an implementation method for microsoft to implement the concept of shared function libraries in windows operating systems. Some files implemented as DLL in windows include:
- ActiveX Control (. ocx) file: for example, a calendar control on windows.
- Control Panel (. cpl) file: each item in the control panel is a dedicated DLL.
- Device Drivers (. drv) files: such as printer drivers that control printing to printers.
Ii. Origin
DLL is initially used to save the disk and memory space required by the application. Previously, some code in a traditional non-shared library was simply appended to the called program. If two programs call the same subroutine at the same time, two pieces of code will appear. On the contrary, the Code shared by many applications can be split into a DLL, stored as a document on the hard disk, and only one instance is needed in the memory.
Iii. Advantages and Disadvantages of DLL
Advantages:
(1) saving memory and code reuse: When multiple programs use the same function library, DLL can reduce the amount of code repeatedly loaded in disk and physical memory and facilitate code reuse.
(2) Modularization: DLL helps promote modular program development. Modularization allows you to modify the code and data in a DLL shared by only several applications without changing the application itself. The basic form of this module allows applications such as Microsoft Office, Microsoft Visual Studio, and even windows to use compact patches and service packages.
Disadvantages:
DLL Hell:DLL hellWhich indicates that several applications use the same shared DLL library in a version conflict.
The reason is:Shared, and shared. Because DLL hell is caused by the fact that the dynamic link library can share functions and resources with other programs.
There are two main scenarios::
Suppose that program a uses the dynamic link library X of version 1.0. When program a is installed on the system, the dynamic link library X of version 1.0 is installed at the same time. Assuming that another program B will also use dynamic link library X, then program B can directly copy to the hard disk to run normally, because the dynamic link library already exists in the system. However, one day, another program C will also use the dynamic link library X. However, due to the late development time of program C, it needs a later version than the dynamic link library X of version 2.0. When program C is installed to the system, version 2.0 dynamic link library X must also be installed to the system, in this case, the dynamic link library of version 1.0 in the system will be replaced (replaced) by version 2.0 ).
Case 1: The new version of the dynamic link library is not compatible with the old version. For example, if a and B require the functions provided by X, after the upgrade to 2.0, the new version of X actually canceled this function (it is hard to imagine, oh, but sometimes it is ....). At this time, although C can run normally, A and B cannot work.
Case 2: The New Dynamic Link Library is compatible with the old version, but there is a bug.
See the following example (to illustrate the problem only ):
// X1.0 versionvoid func (INT count) {If (count <0) Count = 0 ;....} // x2.0 versionvoid func (INT count) {// negative number processing is removed! ...}
Once the Count value is negative, program a may encounter problems in the processing of the new version.
Solution: Side-by-side assembly is a technology used by Windows XP and later systems to solve version conflicts of dynamic link libraries. The key point is that when compiling a program, vs generates a manifest file, specifies the version number of the dynamic link library used by the current application. The manifest file must be released simultaneously for the DLL on the client's computer.
Loader loads the appropriate version of DLL Based on the manifest. If this manifest item is not released, the client loads the DLL Based on the default version. Typical scenarios:
Iv. Relationship between DLL and lib
At first glance, Lib is a static Link Library, DLL is a dynamic link library, which is provided during compilation, and runtime.
Actually not that simple! Lib also has static lib and dynamic lib.
Static lib: It puts the export declaration and implementation in Lib, and all the code after compilation is embedded in the Host Program.
Dynamic lib: It is equivalent to an H file, which is a declaration of the export part of the Implementation part (. dll. After compilation, only part of the export declaration is compiled into the host Program. The corresponding DLL file must be supported during runtime; otherwise, it cannot work. When a new DLL is generated, a supporting lib will also be generated (that is, the two must be distributed together). At this time, the Lib is dynamic lib (there will be experiments later ).
5. How to generate a DLL
In the VC ++ 6.0 development environment, open the file \ new \ project option, you can select Win32 dynamic-link library or MFC Appwizard [DLL] to create different types of dynamic link libraries such as non-mfc dll, regular DLL, and extension DLL in different ways. The following describes how to create a DLL using Win32 dynamic-Link Library.AdditionCalculation ):
1. Create an empty project named myDLL in Win32 Dynamic-Link Library mode.
2. Add the header file (. h) and the source file (. cpp) respectively)
// Mydll. h fileextern "C" _ declspec (dllexport) int add (int A, int B); // mydll. CPP file # include "mydll. H "int add (int A, int B) // function to be exported by the DLL: Addition {return a + B ;}
Note:
(1) The previous extern "C" tells the compiler function to be used in this module or other modules. "C" indicates that it needs to be compiled and connected in C language, c ++ modifies the function name during compilation to implement function overloading. This function is not available in C, therefore, you need to use extern "c" to distinguish it when declaring the header file so that the function name can be searched correctly during the link.
(2) _ declspec (dllexport) is the keyword of the export function, meaning that the function needs to be exported from the DLL for use.
3. Compile the connection
After compiling the connection, find the DLL file and the corresponding lib file in the Debug directory.
6. How to call a DLL
The following two call methods are implemented: Separate. dll and. h +. lib +. dll
Note:Copy the corresponding. dll files,. lib files, and. H files (when combined) to the called program directory.
(1) Use. dll only
# Include <wtypes. h> # include <winbase. h ># include <iostream> _ declspec (dllimport) int Add (int a, int B); // import declaration, or you can leave it empty, if the program runs typedef int (* pAdd) (int a, int B); int main () {HINSTANCE hDLL; pAdd Add; hDLL = LoadLibrary ("mydll. dll "); // load the DLL file if (hDLL = NULL) std: cout <" Error !!! \ N "; Add = (pAdd) GetProcAddress (hDLL," add "); // obtain the function address in the DLL for calling int a = Add (5, 8); std :: cout <"a:" <a <std: endl; FreeLibrary (hDLL); return 0 ;}
Output result:
(2). h +. lib +. dll
# Include <wtypes. h> # include <winbase. h> # include <iostream> # include "mydll. h "# pragma comment (lib," mydll. lib ") // set mydll. the lib library file is connected to the target file (that is, this project) extern "C" _ declspec (dllimport) int add (int a, int B); int main () {int a = add (5, 8); std: cout <"a:" <a <std: endl; return 0 ;}
Output:
Reverse example: If the. dll file is removed (only. lib and. H files are available), an error occurs:
References:
(1) Dynamic Link Library of Wikipedia
(2) What is a DLL in msdn?
(3) DLL hell in Wikipedia
(4) Don't fall into the DLL Hell trap again (DLL Hell )....
(5) What is Side-by-side Assembly?
(6) differences between lib and dll
(7) C ++ DLL