.dll 的搜尋順序

來源:互聯網
上載者:User

《Windows 核心編程(第5版)》P522 

19.2.3 運行可執行模組:

由於匯入段只包含 DLL 的名稱,不包含 DLL 的路徑,因此載入程式必須在使用者的磁碟上搜尋 DLL。下面是載入程式的搜尋順序:

(1) 包含可執行檔的路徑;

(2) Windows 的系統目錄,該目錄通過 GetSystemDirectory 得到; 

(3) 16 位的系統目錄,即 Windows 目錄中的 system 子目錄; 

(4) Windows 目錄,該目錄可通過 GetWindowsDirectory 得到;

(5) 進程的目前的目錄;

(6) PATH 環境變數中所列出的目錄。

 

測試環境: Win7U + VS2008SP1 

上面順序中的路徑分別理解為:

【1】 VS2008產生的測試用 .exe 檔所在目錄;

【2】 C:/Windows/system32; 

【3】 C:/Windows/system; 

【4】 C:/Windows; 

【5】 VS2008 中工程檔案所在目錄(.vcproj 檔案所在目錄,即工程預設路徑); 

【6】 PATH 環境變數中所列出任一目錄。 

 

經過測試,搜尋順序是從【1】到【6】的優先順序。

 

P.S.: 以前聽說環境變數設定後無須重啟系統就能生效,在測試的過程中發現並不是這樣的。


----[2011.08.12 更新]----

SetDllDirectory Function

The SetDllDirectory function affects all subsequent calls to the LoadLibrary and LoadLibraryEx functions. It also effectively disables safe DLL search mode while the specified directory is in the search path.

After calling SetDllDirectory, the standard DLL search path is:

1. The directory from which the application loaded.

2. The directory specified by the lpPathName parameter.(註:lpPathName是SetDllDirectory的參數。)

3. The system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32.

4. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.

5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.

6. The directories that are listed in the PATH environment variable.

Each time the SetDllDirectory function is called, it replaces the directory specified in the previous SetDllDirectory call. To specify more than one directory, use the AddDllDirectory function and call LoadLibraryEx with LOAD_LIBRARY_SEARCH_USER_DIRS.

To revert to the standard search path used by LoadLibrary and LoadLibraryEx, call SetDllDirectory with NULL. This also restores safe DLL search mode based on the SafeDllSearchMode registry value.

To compile an application that uses this function, define _WIN32_WINNT as 0x0502 or later. For more information, see Using the Windows Headers.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.