《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.