# 动态链接库

动态链接库（DLL）是 [Microsoft Windows](https://www.edgepedia.org/zh/microsoft-windows) 系列操作系统中的一种模块，其中包含可供另一模块（应用程序或另一个 DLL）使用的函数和数据。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-libraries)</sup> DLL 是共享库的一种形式：与静态链接不同——在静态链接中，链接器会把库函数的代码复制到每一个调用它的模块中——动态链接只包含在加载时或运行时定位并加载含有该函数或数据项的 DLL 所需的信息。<sup>[2](https://learn.microsoft.com/lb-lu/windows/win32/dlls/about-dynamic-link-libraries)</sup><sup> • </sup><sup>[3](https://learn.microsoft.com/lb-lu/cpp/build/dlls-in-visual-cpp?view=msvc-150)</sup> Windows 自身的许多功能都由 DLL 提供，[Windows API](https://www.edgepedia.org/zh/windows-api) 也是作为一组 DLL 实现的，因此任何使用 Windows API 的进程都在使用动态链接。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-libraries)</sup><sup> • </sup><sup>[4](https://learn.microsoft.com/troubleshoot/windows-client/deployment/dynamic-link-library)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 一种包含可供另一模块（应用程序或 DLL）使用的函数和数据的模块；导出函数可被其他模块调用，内部函数仅在 DLL 内部调用。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-libraries)</sup> |
| 链接方式 | 加载时动态链接使用头文件（.h）和导入库（.lib）；运行时动态链接使用 LoadLibrary 或 LoadLibraryEx 加 GetProcAddress，不需要导入库。<sup>[5](https://learn.microsoft.com/en-us/troubleshoot/windows-client/setup-upgrade-and-drivers/dynamic-link-library)</sup> |
| 内存共享 | 每个应用程序获得自己的 DLL 数据副本，但应用程序共享 DLL 的代码；在相同基址加载同一 DLL 的多个进程共享单个物理副本。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-libraries)</sup><sup> • </sup><sup>[6](https://learn.microsoft.com/en-us/windows/win32/dlls/advantages-of-dynamic-linking)</sup> |
| 缺失 DLL 的行为 | 使用加载时链接时，如果启动时找不到所需的 DLL，系统会终止进程；使用运行时链接时，进程会继续运行，并可采用替代方法。<sup>[6](https://learn.microsoft.com/en-us/windows/win32/dlls/advantages-of-dynamic-linking)</sup> |
| 搜索顺序 | 启用安全 DLL 搜索模式（默认）时，非打包应用的搜索顺序依次为 DLL 重定向、API 集、已知 DLL，然后是应用程序文件夹、系统文件夹、Windows 文件夹、当前文件夹和 PATH 目录。<sup>[7](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order)</sup> |
| 入口点 | 可选的 DllMain 入口点在 DLL_PROCESS_ATTACH、DLL_THREAD_ATTACH、DLL_THREAD_DETACH 和 DLL_PROCESS_DETACH 时被调用，运行时持有加载器锁。<sup>[8](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-entry-point-function)</sup><sup> • </sup><sup>[9](https://learn.microsoft.com/en-us/windows/win32/Dlls/dynamic-link-library-best-practices)</sup> |
| 安全风险 | 如果攻击者控制了 DLL 搜索路径上的某个目录，就可能加载恶意副本（DLL 预加载或二进制植入）；MITRE ATT&CK 跟踪搜索顺序劫持（T1574.001）和侧加载（T1574.002）。<sup>[10](https://learn.microsoft.com/en-us/windows/desktop/dlls/dynamic-link-library-security)</sup><sup> • </sup><sup>[11](https://attack.mitre.org/techniques/T1574/001/)</sup> |

## DLL 包含什么

DLL 可以定义两类函数。导出函数既供 DLL 内部调用，也供其他模块调用；内部函数通常只在定义它们的 DLL 内部调用。DLL 也可以导出数据，不过其数据一般只供自身的函数使用。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-libraries)</sup>

有几种常见的 Windows 文件类型就是 DLL。ActiveX 控件使用 .ocx 扩展名，控制面板项是 .cpl 文件，设备驱动程序使用 .drv；例如，每个控制面板项都是一个专用的 DLL。<sup>[4](https://learn.microsoft.com/troubleshoot/windows-client/deployment/dynamic-link-library)</sup> Comdlg32 DLL 执行打开对话框等常见对话框功能，是许多程序同时复用同一代码的标准示例。<sup>[4](https://learn.microsoft.com/troubleshoot/windows-client/deployment/dynamic-link-library)</sup>

## 加载时链接与运行时链接

**加载时动态链接。** 应用程序像调用本地函数一样调用导出的 DLL 函数。构建时，开发人员在编译和链接时提供头文件（.h）和导入库（.lib）文件；链接器记录系统在加载时加载 DLL 并解析导出函数位置所需的信息。<sup>[5](https://learn.microsoft.com/en-us/troubleshoot/windows-client/setup-upgrade-and-drivers/dynamic-link-library)</sup> 导入库包含链接器解析对导出 DLL 函数的外部引用所需的信息；例如，要调用 CreateWindow，代码需与 User32.lib 链接，后者是系统 DLL User32.dll 的导入库。<sup>[12](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-creation)</sup> 启动时，系统利用链接器记录的信息定位 DLL，将每个 DLL 映射到进程的虚拟地址空间，递增其引用计数，调用入口点，并用导入函数的起始地址填充函数地址表。<sup>[13](https://learn.microsoft.com/en-us/windows/win32/dlls/load-time-dynamic-linking)</sup>

**运行时动态链接。** 应用程序调用 LoadLibrary 或 LoadLibraryEx 加载 DLL，然后调用 GetProcAddress 获取导出函数的地址，最后在完成后调用 FreeLibrary；不需要导入库。<sup>[5](https://learn.microsoft.com/en-us/troubleshoot/windows-client/setup-upgrade-and-drivers/dynamic-link-library)</sup><sup> • </sup><sup>[14](https://learn.microsoft.com/en-us/windows/win32/dlls/using-run-time-dynamic-linking)</sup> 如果 DLL 的代码已映射到调用进程中，LoadLibrary 只返回一个句柄并递增引用计数；不同目录中文件名相同的两个 DLL 不被视为同一个 DLL。<sup>[15](https://learn.microsoft.com/en-us/windows/win32/dlls/run-time-dynamic-linking)</sup> FreeLibrary 和 FreeLibraryAndExitThread 递减引用计数，计数归零时模块被取消映射。<sup>[16](https://learn.microsoft.com/en-us/windows/win32/Dlls/dynamic-link-library-functions)</sup>

两者之间的选择是一种权衡。加载时链接更易于编写，因为导出函数的行为与本地函数相同。运行时链接能改善启动性能，因为模块只在需要其功能时才加载，并且它允许应用程序按需分支加载不同的模块，这对多语言版本很重要。<sup>[5](https://learn.microsoft.com/en-us/troubleshoot/windows-client/setup-upgrade-and-drivers/dynamic-link-library)</sup> 失败行为也不同：如果找不到所需的 DLL，加载时链接的进程会被系统终止并弹出错误对话框，而运行时链接的进程会继续运行，可以尝试替代方案、通知用户，或从用户提供的完整路径加载 DLL。<sup>[6](https://learn.microsoft.com/en-us/windows/win32/dlls/advantages-of-dynamic-linking)</sup><sup> • </sup><sup>[15](https://learn.microsoft.com/en-us/windows/win32/dlls/run-time-dynamic-linking)</sup>

## 优势与依赖

与静态链接相比，动态链接提供了若干实际好处：<sup>[6](https://learn.microsoft.com/en-us/windows/win32/dlls/advantages-of-dynamic-linking)</sup><sup> • </sup><sup>[4](https://learn.microsoft.com/troubleshoot/windows-client/deployment/dynamic-link-library)</sup>

- 在相同基址加载同一 DLL 的多个进程共享物理内存中的单个副本，节省内存并减少交换。
- 当 DLL 中的函数发生变化时，使用它们的应用程序无需重新编译或重新链接，只要函数参数、调用约定和返回值不变；静态链接的代码则必须重新链接。
- 用不同语言编写的程序只要遵循相同的调用约定（如 C、Pascal 或标准调用），就可以调用同一个 DLL 函数。
- 模块化让程序可以分别发布和更新组件；一个税率每年变化的工资单程序，只需替换一个 DLL 即可更新，无需重建整个程序；而且各模块加载更快，因为每个模块只在需要时才加载。

相应的弱点是依赖。当程序使用 DLL 时，就创建了依赖关系；如果另一个程序覆盖或破坏了该依赖，原程序可能无法运行。Microsoft 指出，随着 .NET Framework 的引入，通过使用程序集，大多数依赖问题已被消除。<sup>[4](https://learn.microsoft.com/troubleshoot/windows-client/deployment/dynamic-link-library)</sup>

## 内存与数据

代码和数据的处理方式不同。每个应用程序获得自己的 DLL 数据副本，但应用程序共享 DLL 的代码，这在多个应用程序同时使用相同功能时减少了内存开销。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-libraries)</sup> 在 DLL 源代码中声明为全局的变量，会被编译器和链接器视为全局变量，但每个加载该 DLL 的进程都会获得这些全局变量自己的副本，因此默认情况下每个进程都有自己的 DLL 全局变量和静态变量实例。<sup>[17](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-data)</sup> DLL 用 GlobalAlloc、LocalAlloc、HeapAlloc 或 VirtualAlloc 分配的内存属于调用进程的地址空间，使用同一 DLL 的其他进程无法访问；要在进程间共享内存，DLL 必须使用文件映射。<sup>[17](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-data)</sup><sup> • </sup><sup>[18](https://learn.microsoft.com/en-us/windows/win32/memory/scope-of-allocated-memory)</sup>

对于每线程状态，线程本地存储（TLS）函数允许 DLL 分配一个索引，为多线程进程的每个线程存储和检索不同的值，例如每个打开的电子表格的当前行和列。<sup>[17](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-data)</sup>

## DLL 入口点

DLL 可以选择指定一个入口点函数，习惯上称为 DllMain。每当进程或线程加载或卸载 DLL 时，系统就会调用它，原因代码为 DLL_PROCESS_ATTACH、DLL_THREAD_ATTACH、DLL_THREAD_DETACH 或 DLL_PROCESS_DETACH；它可用于简单的初始化和清理，例如设置线程本地存储。<sup>[8](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-entry-point-function)</sup> 一次只能有一个线程调用入口点，DisableThreadLibraryCalls 函数可以关闭线程附加和分离通知。<sup>[8](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-entry-point-function)</sup><sup> • </sup><sup>[16](https://learn.microsoft.com/en-us/windows/win32/Dlls/dynamic-link-library-functions)</sup>

入口点在严格的约束下运行，因为它在持有加载器锁时被调用。调用任何直接或间接试图获取加载器锁的函数都可能导致死锁或使应用程序崩溃，而 DllMain 中的错误可能危及整个进程及其所有线程。<sup>[9](https://learn.microsoft.com/en-us/windows/win32/Dlls/dynamic-link-library-best-practices)</sup> Microsoft 的指导是尽可能推迟初始化，并且绝不要在 DllMain 中调用 LoadLibrary 或 LoadLibraryEx、同步函数、注册表函数、CreateProcess、CreateThread、User32.dll 或 Gdi32.dll 函数或托管代码。<sup>[9](https://learn.microsoft.com/en-us/windows/win32/Dlls/dynamic-link-library-best-practices)</sup> 由于入口点运行时保证已加载 Kernel32.dll，入口点可以安全地创建临界区和互斥体等同步对象，并可以使用 TLS；调用位于 Advapi32.dll 中的注册表函数则不安全。<sup>[8](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-entry-point-function)</sup> 返回值只对进程附加有意义：返回 FALSE 会使加载时链接的程序进程初始化失败并终止进程，对于运行时链接的程序则使 LoadLibrary 返回 NULL。<sup>[8](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-entry-point-function)</sup>

## 搜索顺序与安全

当应用程序加载 DLL 而未提供完全限定路径时，系统会按顺序搜索一组定义的目录。对于启用了安全 DLL 搜索模式（这是默认设置）的非打包应用，顺序为：DLL 重定向、API 集、并行（SxS）清单重定向、已加载模块列表、已知 DLL、包依赖图（[Windows 11](https://www.edgepedia.org/zh/windows-11) 版本 21H2 及更高版本）、应用程序从中加载的文件夹、系统文件夹、16 位系统文件夹、Windows 文件夹、当前文件夹以及 [PATH](https://www.edgepedia.org/zh/path-variable) 环境变量中的目录。安全 DLL 搜索模式将用户的当前文件夹移到顺序中较后的位置；禁用它则会把当前文件夹从第 11 位移到第 8 位。<sup>[7](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order)</sup> 开发人员可以用 LoadLibraryEx 和 LOAD_WITH_ALTERED_SEARCH_PATH、用 SetDllDirectory，或用 LOAD_LIBRARY_SEARCH 标志来改变顺序，这些标志按该顺序搜索 DLL 自身文件夹、应用程序文件夹、用户添加的目录和 System32 文件夹。<sup>[7](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order)</sup>

搜索顺序也是一个攻击面。如果攻击者控制了被搜索目录之一，他们就可以在那里放置 DLL 的恶意副本，这种技术称为 DLL 预加载攻击或二进制植入；如果应用程序以管理员权限运行，这可能导致本地权限提升。<sup>[10](https://learn.microsoft.com/en-us/windows/desktop/dlls/dynamic-link-library-security)</sup> Microsoft 的缓解措施是：用 LoadLibrary、LoadLibraryEx、CreateProcess 或 ShellExecute 指定完全限定路径；将 LOAD_LIBRARY_SEARCH 标志与 SetDefaultDllDirectories 和 AddDllDirectory 一起使用；并保持启用安全 DLL 搜索模式。<sup>[10](https://learn.microsoft.com/en-us/windows/desktop/dlls/dynamic-link-library-security)</sup> [MITRE ATT&CK](https://www.edgepedia.org/zh/mitre-att-and-ck) 在“劫持执行流”下收录了两种相关的对手技术：DLL 搜索顺序劫持（T1574.001）和 DLL 侧加载（T1574.002），后者是指将一个合法的已签名应用程序与一个恶意 DLL 放在一起，该应用程序会加载这个恶意 DLL 而不是合法库；有记录的滥用案例从 APT3 用有效版本的 Chrome 侧加载 DLL，到 Sidewinder 劫持合法的 rekeywiz.exe 应用程序。<sup>[11](https://attack.mitre.org/techniques/T1574/001/)</sup>

## 创建 DLL

构建 DLL 需要一个或多个源文件，对于加载时链接还需要一个导入库。在 Visual C++ 中，有两种导出函数的方法：在函数声明中使用 __declspec(dllexport) 修饰符，或向链接器传递模块定义（.def）文件；使用 __declspec(dllexport) 就不需要 .def 文件。<sup>[12](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-creation)</sup> 如果 DLL 将由多线程应用程序使用，则必须同步对其所有全局数据的访问，并且只能与线程安全的库链接；Visual C++ 附带多个版本的 C 运行时库，一个不是线程安全的，两个是线程安全的。<sup>[12](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-creation)</sup> 在使用方，对于显式运行时链接，应用程序用 LoadLibraryEx 加载 DLL 并用 GetProcAddress 获取函数指针，不需要 __declspec(dllimport) 声明。<sup>[19](https://learn.microsoft.com/en-us/cpp/build/linking-an-executable-to-a-dll?view=msvc-170)</sup>

## References

1. [Dynamic-Link Libraries, Microsoft Learn (Win32)](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-libraries)
2. [About Dynamic-Link Libraries, Microsoft Learn](https://learn.microsoft.com/lb-lu/windows/win32/dlls/about-dynamic-link-libraries)
3. [Create C/C++ DLLs in Visual Studio, Microsoft Learn](https://learn.microsoft.com/lb-lu/cpp/build/dlls-in-visual-cpp?view=msvc-150)
4. [What is a DLL, Microsoft Learn (Windows Client troubleshooting, KB 815065)](https://learn.microsoft.com/troubleshoot/windows-client/deployment/dynamic-link-library)
5. [Dynamic link library (DLL), Microsoft Learn (Windows Client troubleshooting)](https://learn.microsoft.com/en-us/troubleshoot/windows-client/setup-upgrade-and-drivers/dynamic-link-library)
6. [Advantages of Dynamic Linking, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/dlls/advantages-of-dynamic-linking)
7. [Dynamic-link library search order, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order)
8. [Dynamic-Link Library Entry-Point Function, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-entry-point-function)
9. [Dynamic-Link Library Best Practices, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/Dlls/dynamic-link-library-best-practices)
10. [Dynamic-Link Library Security, Microsoft Learn](https://learn.microsoft.com/en-us/windows/desktop/dlls/dynamic-link-library-security)
11. [Hijack Execution Flow: DLL, Sub-technique T1574.001, MITRE ATT&CK](https://attack.mitre.org/techniques/T1574/001/)
12. [Dynamic-link library creation, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-creation)
13. [Load-Time Dynamic Linking, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/dlls/load-time-dynamic-linking)
14. [Using Run-Time Dynamic Linking, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/dlls/using-run-time-dynamic-linking)
15. [Run-Time Dynamic Linking, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/dlls/run-time-dynamic-linking)
16. [Dynamic-Link Library Functions, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/Dlls/dynamic-link-library-functions)
17. [Dynamic-Link Library Data, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-data)
18. [Scope of Allocated Memory, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/memory/scope-of-allocated-memory)
19. [Link an executable to a DLL, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/build/linking-an-executable-to-a-dll?view=msvc-170)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026; Oct 21, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Microsoft Windows

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/dynamic-link-library

English original: https://www.edgepedia.org/dynamic-link-library
