SDK programming notes-DLL
1. Meaning of Dynamic Links
). When using static links, all the functions and resources required for the executable file are included in the file. The disadvantage is that the same functions and resources used by multiple programs must be repeatedly linked to the EXE file to increase the program size and increase the memory usage. "Dynamic Link" organizes some common functions or resources into dynamic link library files (. DLL), when a program that needs to use the functions or resources in the DLL is started (or initialized accurately ), the system maps the DLL to the virtual address space of the calling process, increases the reference count value of the DLL, and then loads the DLL into the memory when the DLL is actually used; if all programs using the DLL have ended, the system will remove the library from the memory. Each process using the same DLL shares the DLL code at run time, but there is a copy of the data in the DLL (of course, there is also a way to share data in the DLL ). Two types of functions can be defined in the Dynamic Link Library: Output Functions and internal functions. The output function can be called by other modules. The internal function can only be called by the dynamic link library itself. The dynamic link library can also output data, but the data is usually used only by its own functions.
2. Advantages of Dynamic Links
→ Memory saving;
→ Make the application "thin ";
→ The dynamic link library can be modified separately without re-linking with the application;
→ Multi-language joint programming is convenient (for example, using VC ++ to write a DLL and then calling it in VB );
→ Package resources;
→ Shared memory between applications
→ ......
3. About the extension
The standard extension of the dynamic link library is DLL. Other dynamic link libraries such as EXE, DRV, and Fon can also be used as extensions. However, only dynamic link libraries with DLL extensions can be automatically loaded by windows. If you use a dynamic link library with another extension, you must use loadlibrary or LoadLibraryEx to load the dynamic link library module in the program that calls the dynamic link library.
4. Use the SDK to create a simple DLL file
Create a Win32 dynamic-Link Library in VC ++. A c/C ++ head file and a C/C ++ source file need to be created and added to the project. The header file contains the declaration of the output function, and the source file contains the definition of the dllmain function and the output function. The following is a simple example.
// Dlldemo. h # Ifdef _ cplusplus # Define export extern "C" _ declspec (dllexport) # Else # Define export _ declspec (dllexport) # EndifExport void callback dllfoo (void ); // Dlldemo. c # Include <windows. h> # Include "dlldemo. H" Int winapi dllmain (hinstance, DWORD fdwreason, pvoid pvreserved) { Return true; } Export void callback dllfoo (void) { MessageBox (null, text ("this function is exported from a DLL"), text ("dllfoo"), mb_ OK ); Return; } |
In header file preprocessing, _ declspec is the c extended storage-class attributes added by Microsoft ), it indicates that an instance is stored as a Microsoft-specific class storage attribute, which can be thread, naked, dllimport or dllexport. [original msdn text: the extended attribute syntax for specifying storage-class information uses the _ declspec keyword, which specifies that an instance of a given type is to be stored with a Microsoft-specific storage-class attribute (thread, naked, dllimport, or dllexport).] The output function must be specified as callback. Dllmain is the DLL entry point function. You can also leave it empty. Dllmain must return true; otherwise, the program will be terminated and a "error occurred while starting the program" dialog box will pop up. After the link is compiled, obtain the dynamic link library file dlldemo. dll and the input library file dlldemo. Lib.
5. Two DLL Methods
Method 1: Load-Time Dynamic Linking
When you want to call the DLL application link, include the DLL input library file (import library,. Lib file. The specific method is to add # include at the beginning of the source file, and then you can call the output file in dlldemo. dll in the source file.
Method 2: Run-Time Dynamic Linking
Instead of including the input library file during the link, you can use loadlibrary or LoadLibraryEx to dynamically load the DLL in the source program.
The main steps are as follows (demodll. dll is used as an example ):
1) typedef function prototype and define function pointer. Typedef void (callback * dllfootype) (void ); Dllfootype pfndllfoo = NULL; |
2) use loadlibrary to load the DLL and save the DLL instance handle Hinstance dllhandle = NULL; ... Dllhandle = loadlibrary (text ("dlldemo. dll ")); |
3) Use getprocaddress to get the pointer of the function in the DLL. Pfndllfoo = (dllfootype) getprocaddress (dllhandle, text ("dllfoo ")); Note that the pointer returned from getprocaddress must be converted to a specific type of function pointer. |
4) Check the function pointer. You can call this function if it is not null. If (pfndllfoo! = NULL) Dllfoo (); |
5) Use freelibrary to uninstall the DLL Freelibrary (dllhandle ); |
Running-time dynamic linking is troublesome, but it has advantages (discussed below ). One article in msdn, DLLs the dynamic way, discusses how to use a C macro to create a base class pdll to complete the above complex operations, you only need to define a class to inherit from the class pdll and use macros for classes and functions.
Both methods require the application to find the DLL file. In Windows, find the DLL file in the following order:
1) application directory. |
2) Current directory. |
3) Windows 9x: System Directory; Windows 2000/NT: system32 directory |
4) Windows 2000/NT System directory. |
5) Windows directory. |
6) path in the environment variable path. |
If the system cannot find the DLL file, it will end the process of calling the DLL and pop up a "error occurred when starting the program" dialog box, telling you that "the required DLL file-xxx. dll cannot be found ".
6. What are the advantages of running-time dynamic linking?
If load-time dynamic linking is used, the program that calls this DLL cannot run when the DLL file is lost, you will see a "error occurred when starting the program" dialog box. If you load the DLL at runtime, you have the opportunity to handle this error, at least you can end the program more gently.
If the DLL changes, the program that uses load-Time Dynamic Linking may terminate, however, the run-time dynamic linking program will only encounter errors when the called function does not exist in the new DLL.
7. Use pure resource DLL
Generally, you only need to write an empty dllmain in its c file, insert the resource into the project, and compile it as a DLL file. Pure resource DLL does not have any output function, so it does not generate the. Lib file, so it must be loaded with loadlibrary at runtime.
8. Use shared data in DLL (from programming Windows)
// Shared memory section (requires/section: shared, RWS in link options) # Pragma data_seg ("shared ") Int itotal = 0; Wchar szstrings [max_strings] [max_length + 1] = {'/0 '}; # Pragma data_seg ()# Pragma comment (linker, "/section: shared, RWS ") |
The first # pragma describes the establishment of a data segment, which is named shared. You can name this section as any name you like. Here, all initialized variables after the # pragma description are stored in the shared data segment. The second # End Of The Pragma description section. It is very important to initialize variables. Otherwise, the compiler will put them in common uninitialized data segments rather than in shared. The connector must know that there is a shared data segment. Select the link tab in the Project Settings dialog box. When "strlib" is selected, it is in the "Project Options" Column (both in the release and debug settings) and contains the following link Description:/section: shared, the RWS letter RWS indicates that the segment has read, write, and shared attributes. Alternatively, you can use the DLL original code to specify the link option, just as we did in strlib. C: # pragma comment (linker, "/section: shared, RWS ")
Source:
Http://www.chinaitpower.com/A/2004-01-21/132305.html
The buddy mentioned above is good, but there is something wrong:
// Dlldemo. h
# Ifdef _ cplusplus
# Define export extern "C" _ declspec (dllexport)
# Else
# Define export _ declspec (dllexport)
# Endif
Change # ifdef to # ifndef