Large applications Program These modules are composed of many modules that complete relatively independent functions and work together to complete the work of the entire software system. Some modules may have common functions and will still be used when constructing other software systems. When constructing a software system Source code If all files are statically compiled into the entire application EXE file, some problems will occur: one drawback is that the application size is increased, which will occupy more disk space, when the program runs, it also consumes a large amount of memory space, resulting in a waste of system resources. Another drawback is that when writing a large exe program, you must adjust all the source code during each modification and reconstruction. Code It increases the complexity of the compilation process and is not conducive to the staged unit testing.
On the Windows system platform, a completely different effective programming and running environment is provided. You can create an independent program module as a small DLL file, they can be compiled and tested separately. During runtime, the system will load the EXE program to the memory space only when it does need to call these DLL modules. This method not only reduces the size of the EXE file and the need for memory space, but also enables these DLL modules to be used by multiple applications at the same time. Windows itself implements some major system functions in the form of DLL modules.
In general, a DLL is a disk file. dll,. DRV,. Fon,. sys, and many system files with. EXE extension can be DLL. It consists of global data, service functions, and resources, and is loaded into the virtual space of the calling process by the system at runtime, becoming part of the calling process. If there is no conflict with other DLL, the file is usually mapped to the same address of the virtual space of the process. The DLL module contains various export functions to provide external services. A dll can have its own data segment but does not have its own stack. It uses the same stack mode as the application that calls it. a dll has only one instance in the memory; DLL implements code encapsulation; DLL compilation and specificProgramming LanguageAnd the compiler.
In the Win32 environment, each process copies its own read/write global variables. To share memory with other processes, you must use a memory ing file or declare a shared data segment. The stack memory required by the DLL module is allocated from the stack of the running process. When Windows loads the DLL module, it matches the process function call with the export function of the DLL file. In Windows, the DLL operation only maps the DLL to the virtual address space of the process that requires it. Any object (including variables) created by the code in the DLL function is owned by the thread or process that calls it.
Call method:
1. Static call method: the compilation system loads the DLL and the encoding of the DLL uninstallation when the application ends (if other programs use the DLL, in Windows, the Application Record of the DLL is reduced by 1 until all related programs end to use the DLL. It is simple and practical, but not flexible enough to meet general requirements.
Implicit call: You need to add the. Lib file generated when a dynamic Connection Library is generated to the project of the application. To use a function in the DLL, you only need to describe it. You do not need to call loadlibrary () and freelibrary () for implicit calls (). When a programmer creates a DLL file, the link program automatically generates a corresponding lib import file. This file contains the symbolic name and optional Identification Number of each DLL export function, but does not contain the actual code. The LIB file is compiled into the application project as an alternative DLL file.
When programmers compile and generate an application through static links, the calling functions in the application match the exported symbols in the Lib file. These symbols or identifiers enter the generated EXE file. The LIB file also contains the corresponding DL l file name (but not the full path name). The link program stores the name in the EXE file.
When a DLL file needs to be loaded while the application is running, Windows will find and load the DLL based on the information, and then use the symbolic name or identification number to achieve dynamic links to the DLL function. All DLL files called by the application will be loaded into the memory when the application EXE file is loaded. The executable program is linked to an input library file (. Lib file) that contains DLL output function information ). The operating system loads the DLL when using an executable program. The executable program calls the DLL output function directly through the function name. The Calling method is the same as its function in the program.
2. Dynamic calling method: the programmer uses API functions to load and uninstall the DLL to call the DLL. It is complicated to use, but can use the memory more effectively, is an important way to compile large-scale applications.
Explicit call:
It refers to explicitly calling the dynamic Connection Library provided by loadlibrary or MFC in the application. The file name of the dynamic Connection Library is the parameters of the above two functions, use getprocaddress () to obtain the function to be introduced. Since then, you can call the introduced function like a function defined by the application. Before exiting the application, use the afxfreelibrary provided by freelibrary or MFC to release the dynamic Connection Library. Directly call the loadlibary function of Win32 and specify the dll path as the parameter. Loadlibary returns the hinstance parameter, which is used by the application to call the getprocaddress function. The getprocaddress function converts the symbolic name or identification number to the internal address of the DLL. The programmer can decide when to load or not load the DLL file, and the explicit link decides which DLL file to load at runtime. Before using the DLL program, you must load (loadlibrary) the DLL to obtain the handle of a DLL module. Then, call the getprocaddress function to obtain the pointer of the output function, you must uninstall the DLL (freelibrary) before exiting ).
It is precisely because DLL occupies a small amount of memory and is easy to edit. Many computer viruses are DLL files. However, it cannot be run independently.
Generally, Dynamic Link Libraries cannot run directly or receive messages. They are independent files that contain functions that can be called by executable programs or other DLL to complete a job. It works only when other modules call functions in the dynamic link library.