动态库

微软关于动态库的权威文档集

https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-libraries

动态库的链接

1. Load-time Dynamic Linking

When the system starts a program that uses load-time dynamic linking, it uses the information the linker placed in the file to locate the names of the DLLs that are used by the process. The system then searches for the DLLs.

2. Run-time Dynamic Linking

When the application calls the LoadLibrary or LoadLibraryEx functions, the system attempts to locate the DLL (for details, see Dynamic-Link Library Search Order). If the search succeeds, the system maps the DLL module into the virtual address space of the process and increments the reference count. If the call to LoadLibrary or LoadLibraryEx specifies a DLL whose code is already mapped into the virtual address space of the calling process, the function simply returns a handle to the DLL and increments the DLL reference count.

The system calls the entry-point function in the context of the thread that called LoadLibrary or LoadLibraryEx. The entry-point function is not called if the DLL was already loaded by the process through a call to LoadLibrary or LoadLibraryEx with no corresponding call to the FreeLibrary function.

The process can use GetProcAddress to get the address of an exported function in the DLL using a DLL module handle returned by LoadLibrary or LoadLibraryEx, GetModuleHandle.

Windows下的运行时加载动态库的API是 LoadLibrary(传入DLL绝对路径,获得动态库句柄)和GetProcAddress(传入函数名,获得函数地址)

动态库搜索路径顺序

If SafeDllSearchMode is enabled (enabled by default for Windows 7 & 10), the search order is as follows:

  1. The directory from which the application loaded.
  2. The system directory. Use the GetSystemDirectory function to get the path of this directory.
  3. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
  4. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
  5. The current directory.
  6. The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.

动态库的入口函数

  • 可选的
  • 在动态库被加载和卸载时被调用,被加载时做初始化,被卸载时做 clean up.
    A DLL can optionally specify an entry-point function. If present, the system calls the entry-point function whenever a process or thread loads or unloads the DLL. It can be used to perform simple initialization and cleanup tasks. For example, it can set up thread local storage when a new thread is created, and clean it up when the thread is terminated.

桩代码(stub code)

什么是桩代码 - 知乎
《程序员的自我修养》第一版第264页:当延迟载入的API第一次被调用时,由链接器添加的特殊的桩代码就会启动,这个桩代码负责对DLL的装载工作。然后这个桩代码通过调用GetProcAddress来找到被调用API的地

Stub在很多情况下含义有一些微妙的差别。这里指的应该是这样一个东西:首先你有一个dll或者so,你可能不想程序启动就加载,而是第一次调用dll的函数的时候再去加载那么对于dll里面的函数function,你可以实现另一个函数,也叫做function,功能类似这样:

if (function_ptr == NULL) {
   function_ptr = load_dll_and_find_symbol("xx.dll", "function")
}
 return function_ptr(...);

这样你的程序只需要链接到这个函数,而那些dll在用户不使用的情况下,都可以不用部署。既然是说延迟载入,应该就是指这种。

你可能感兴趣的:(动态库)