動(dòng)態(tài)庫(kù)

微軟關(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:

  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.

動(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)該就是指這種。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,985評(píng)論 0 0
  • 說(shuō)明本次redis集群安裝在rhel6.8 64位機(jī)器上,redis版本為3.2.8,redis的gem文件版本為...
    讀或?qū)?/span>閱讀 15,598評(píng)論 3 9
  • Pathname lookup in Linux. This write-up is based on three...
    朔飛閱讀 871評(píng)論 0 0
  • 從茶卡返回西寧,我們走的是高速。同是草原上,卻有些人工點(diǎn)綴,使得畫面不再單一。 沒(méi)錯(cuò),就是那些風(fēng)力發(fā)電的大風(fēng)車了。...
    傳馨閱讀 458評(píng)論 0 1
  • 仲夏的雨淋濕了誰(shuí)的眼睛一個(gè)人的夜憂郁而安靜清涼的竹林是誰(shuí)還在等雨停 循著小徑 踏入小小軒亭倚著窗細(xì)聽(tīng)燈暈外風(fēng)吟雨...
    柳塵微閱讀 774評(píng)論 0 8

友情鏈接更多精彩內(nèi)容