SDK programming notes - DLL [ZZ]

xiaoxiao2021-03-06  103

Author: method n5 (original) 1 dynamic linking of meaning often use so-called "static link" link in the application, is about each object file (.obj), run-time library (.lib) and compiled resource file ( .res) link to together, form an executable file (.exe). When using static links, the various functions and resources that can be used in executable files are included in the file. The disadvantage of this is that the same function and resource to be used for multiple programs to repeat the chain to the exe file, making the program to increase the memory. "Dynamic Link" is a dynamic link library file (.dll) to dynamic link library files (.dll), when a function or resource program is started (accurately said to be initialized), the system will The DLL maps to the virtual address space of the calling process, increasing the DLL reference count, and then loads the DLL into memory when the DLL actually uses the DLL; if all the programs of the DLL are end, the system will This library is removed from memory. Use the various processes of the same DLL to share the DLL code at runtime, but for the data in the DLL, each copy (of course, there is also a method of sharing data in the DLL). Two functions can be defined in the dynamic link library: output functions and internal functions. The output function can be called by other modules, and the internal function can only be called by the dynamic link library itself. Dynamic link libraries can also output data, but this data is usually only used by its own functions. 2. Advantages of dynamic links → Save memory; → Make the application "thin"; → can modify the dynamic link library separately without having to re-link the application; → easy to implement multi-language joint programming (such as writing a DLL with VC , Then call in VB); → You can pack resource packages; → can share memory between applications → ... 3. The standard extension of the extension dynamic link library is DLL, others such as EXE, DRV, FON It can also be used as an extension, but only dynamic link libraries with DLL can be automatically loaded by Windows. If you use another extension dynamic link library, you must use LoadLibrary or LoadLibraryEx to load dynamic link library modules in the program that calls the dynamic link library. 4. Create a simple DLL file with SDK in VC Select New Win32 Dynamic-Link Library. A C / C Head file and a C / C Source File are required and join the project. The content of the content is the declaration of the output function, and the content is the definition of the DLLMAIN function and the output function. Here is the simplest example.

//dlldemo.h #ifdef __cplusplus # define EXPORT extern "C" __declspec (dllexport) # else # define EXPORT __declspec (dllexport) #endifEXPORT void CALLBACK DllFoo (void); // dlldemo.c # include # include "dlldemo.h" int WINAPI DllMain (hINSTANCE 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;}

The __Declspec in the header file pre-processing is Microsoft's incremented "C Extended Storage-Class Attribute, which indicates that a given instance is stored as a Microsoft-specific storage properties, which can be THREAD. , naked, dllimport or dllexport [MSDN original:. 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 a Callback. DLLMAIN is the entry point function of the DLL. You can also write it. DllMain must return True, otherwise the system will terminate the program and pop up a "startup error" dialog. After the compilation link, get the dynamic link library file DLLDEMO.DLL and enter the library file DLLDEMO.LIB. 5. Two ways to use the DLL 1: Load-time Dynamic Linking When you want to call the DLL application link, include the DLL input library file (IMPORT LIBRARY, FIB file) into it. The specific method is to add a sentence #include in the source file, then call the output file in dlldemo.dll in the source file. Method 2: Run-Time Dynamic Linking does not have to contain input library files when links, but use LoadLibrary or LoadLibraryEx dynamic load DLL in the source program. The main steps are (take Demodll.dll as an example): 1) TypeDef function prototype and definition 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 = loadingLibrary (Text ("DLLDEMO.DLL");

3) Use getProcAddress to get the pointer of the function in the DLL Pfndllfoo = (DLLHANDLE, Text ("DLLFOO"); Note The pointer returned from getProcAddress must be transformed into a specific type of function pointer. 4) Inspection function pointer, if it is not possible to call this function if (pfndllfoo! = Null) DLLFOO (); 5) Use freeElibrary to uninstall DLL Freelibrary (DLLHANDLE);

It is more troublesome to use Run-Time Dynamic Link, but has its benefits (discussed below). An article in MSDN DLLS The Dynamic Way discussion uses CC created a base class PDLL to complete the above complex operation, just define a class inheritance self-class PDLL and use macros for class and functions. The above two methods require the application to find the DLL file, and Windows looks for DLL files in the following order:

1) The application where the application is located. 2) The current directory. 3) Windows 9x: system directory; Windows 2000 / NT: System32 Directory 4) The system directory of Windows 2000 / NT. 5) The directory where Windows is located. 6) The path in the environment variable PATH.

If the system does not find a DLL file, the process of calling the DLL and popping up a "startup" dialog box, telling you "If the required DLL file -Xxx.dll" is not found. 6 What are the benefits of using running dynamic links (Run-Time Dynamic Linking)? If you use the dynamic link (LOAD-TIME DYNAMIC LINKING), when the DLL file is lost, the program that calls this DLL will not be able to run, and you will see a "startup" dialog box. And if you load a DLL at runtime, you have the opportunity to handle this error, at least the "gentle" end program. If the DLL changes, the programs that use load-time Dynamic Linking may terminate, and the program using Run-Time Dynamic Linking is only wrong when the calling function does not exist in the new DLL. 7 Using a pure resource DLL typically only writes an empty DLLMAIN in its C file, then insert the resource to the project, and finally compile into a DLL file. The pure resource DLL does not have any output functions, so it will not generate the .lib file, so you must use LoadLibrary to load when running. 8 using the shared data in the 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 narrative establishment data segment, here is named Shared. You can name this section to any of your favorite names. All initialized variables after #pragma here are placed in the Shared data segment. The second #pragma narrative indicator segment ends. It is important to make special initialization for variables, otherwise the compiler will put them in a normal unin-initialization data section instead of placing them. The connection must know if there is a "shared" shared data segment. Select the "LINK" page label in the "Project Settings" dialog box. When selecting "Strlib", in the "Project Options" field (all in Release and Debug settings), including the following link narrative: / section: Shared, the RWS letter RWS representation is read, write, and shared properties. Or, you can also specify link options directly with the original code of DLL, just like we are in strlib.c: #pragma comment (Linker, "/ Section: Shared, RWS") The end

转载请注明原文地址:https://www.9cbs.com/read-103658.html

New Post(0)