DLL(ENG)
DLL (Dynamic Link Library) Detailed Explanation
Definition and Abbreviation • DLL is the abbreviation of Dynamic Link Library, which is a mechanism used to implement shared function libraries in Microsoft Windows operating systems. It contains code, data, and resources that can be used simultaneously by multiple programs.
Historical Background • Origin: The concept of DLL first appeared in Windows 1.0 and subsequent versions in the 1990s, aiming to solve the problems of code redundancy and resource waste in early software. With the popularity of Windows 3.1 and Windows 95, DLL became a core component of the system. • Driving Factors: ◦ Resource Saving: Avoid each program from repeatedly including the static library code with the same functionality. ◦ Modular Development: Support splitting functions into independent modules, facilitating maintenance and updates. ◦ Operating System Standardization: The Windows system itself provides APIs through DLLs (such as kernel32.dll), unifying the application programming interface.
Core Connotations • Shareability: Multiple programs can call the same DLL file simultaneously, reducing memory and disk occupancy. • Modularity: Functions exist in the form of independent files, supporting "hot updates" (replacing the DLL without recompiling the main program). • Resource Integration: It can contain resources such as icons, strings, and dialog box templates for reuse by multiple programs. • Cross-Language Support: As long as the calling convention (such as stdcall) is followed, different languages (such as C++, C#, VB, etc.) can all call it.
Why DLLs are Needed? • Space and Memory Saving: Shared code reduces redundancy and lowers the program size and memory occupancy during runtime. • Flexible Updates: When fixing bugs or upgrading functions, only the specific DLL needs to be replaced without republishing the entire program. • Function Expansion: Through dynamic loading (such as LoadLibrary()), a plugin mechanism can be implemented (such as Photoshop filters). • System Standardization: Windows provides unified APIs through system DLLs, simplifying application development.
Comparison between DLL and Static Library | Feature | DLL (Dynamic Linking) | Static Library (.lib/.a) | |--|--|--| | Linking Time | Dynamically loaded at runtime | Directly embedded into the executable file during compilation | | File Size | The main program is smaller and depends on external DLLs | The main program is larger (including all library code) | | Update and Maintenance | Replacing the DLL takes effect immediately | The entire program needs to be recompiled | | Memory Occupancy | Shared code, low memory occupancy | Each program occupies memory independently |
Loading Methods of DLLs • Implicit Loading (Static Binding): Automatically loaded when the program starts, and dependencies are declared through the import table. • Explicit Loading (Dynamic Binding): Manually loaded at runtime through APIs (such as LoadLibrary() and GetProcAddress()), flexibly controlling resource usage.
Challenges and Solutions • DLL Hell: Version conflicts lead to program crashes (such as an older version of the DLL overwriting a newer version). ◦ Solution: Windows XP introduced Side-by-Side Assembly (SxS), allowing different versions of DLLs to coexist. • Security: Malicious DLL injection may pose system risks. ◦ Mitigation Measures: Digital signature verification, restricting trusted directories (such as System32).
Cross-Platform Analogy • Linux/Unix: Shared objects (.so files, such as libc.so). • macOS: Dynamic libraries (.dylib or.framework). • Functional Equivalence: All achieve code sharing and dynamic loading, but the file formats and loading mechanisms are different.
Practical Application Examples • System-level DLLs: user32.dll (user interface functions), gdi32.dll (graphic drawing). • Software Plugins: Game mods and browser extensions dynamically load functional modules through DLLs. • Multilingual Collaboration: A DLL written in C++ is called by Python through ctypes.
Extension: COM and DLL • COM (Component Object Model): A cross-language binary interface standard, often implemented as components in the form of DLLs (such as mscoree.dll supporting.NET). • Relationship: DLL is one of the carriers of COM components, but COM places more emphasis on interface specifications and inter-process communication (it can also be implemented through EXE).
Last updated