微軟關(guān)于動(dòng)態(tài)庫(kù)的權(quán)威文檔集
https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-libraries
動(dòng)態(tài)庫(kù)的鏈接
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下的運(yùn)行時(shí)加載動(dòng)態(tài)庫(kù)的API是 LoadLibrary(傳入DLL絕對(duì)路徑,獲得動(dòng)態(tài)庫(kù)句柄)和GetProcAddress(傳入函數(shù)名,獲得函數(shù)地址)
動(dòng)態(tài)庫(kù)搜索路徑順序
If SafeDllSearchMode is enabled (enabled by default for Windows 7 & 10), the search order is as follows:
- The directory from which the application loaded.
- The system directory. Use the GetSystemDirectory function to get the path of this directory.
- The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
- The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
- The current directory.
- 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.
動(dòng)態(tài)庫(kù)的入口函數(shù)
- 可選的
- 在動(dòng)態(tài)庫(kù)被加載和卸載時(shí)被調(diào)用,被加載時(shí)做初始化,被卸載時(shí)做 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)
什么是樁代碼 - 知乎
《程序員的自我修養(yǎng)》第一版第264頁(yè):當(dāng)延遲載入的API第一次被調(diào)用時(shí),由鏈接器添加的特殊的樁代碼就會(huì)啟動(dòng),這個(gè)樁代碼負(fù)責(zé)對(duì)DLL的裝載工作。然后這個(gè)樁代碼通過(guò)調(diào)用GetProcAddress來(lái)找到被調(diào)用API的地
Stub在很多情況下含義有一些微妙的差別。這里指的應(yīng)該是這樣一個(gè)東西:首先你有一個(gè)dll或者so,你可能不想程序啟動(dòng)就加載,而是第一次調(diào)用dll的函數(shù)的時(shí)候再去加載那么對(duì)于dll里面的函數(shù)function,你可以實(shí)現(xiàn)另一個(gè)函數(shù),也叫做function,功能類似這樣:
if (function_ptr == NULL) {
function_ptr = load_dll_and_find_symbol("xx.dll", "function")
}
return function_ptr(...);
這樣你的程序只需要鏈接到這個(gè)函數(shù),而那些dll在用戶不使用的情況下,都可以不用部署。既然是說(shuō)延遲載入,應(yīng)該就是指這種。