About the sequence of DLL search paths

Source: Internet
Author: User

Author: Zhu Jincan

Source: http://www.cnblogs.com/clever101

 

DLLThere are two methods for dynamic link. One is load_time dynamic linking ). Windows searches for the DLL to be loaded in the following order: ApplicationProgramDirectory → current directory → Windows System directory → Windows Directory → path specified by PATH environment variable.

 

When I saw these words the day before yesterday, I suddenly designed a pen question that I thought was brilliant:

"If dynamic links are used during loading, what sequence does Windows Search for DLL to be loaded ?"

This is a basic question. It is easy to answer (the answer is the above ). Well, I still have the following:

"How do you prove that the DLL to be loaded in Windows Search follows this order and tell your proof steps"

 

You can think about it. The following is a solution I imagined, but I tested it myself. The results are surprising.

My proof steps are as follows:

1.First, create an MFC General DLL project outputmodulepath and add an API function in it to print the path of the DLL and then export it,CodeAs follows:

Void Paintmodulepath ()
{
Extern Coutputmodulepathapp theapp;
Tchar szmodulepath [max_path];
: Getmodulefilename (theapp. m_hinstance, szmodulepath, max_path );
Afxmessagebox (szmodulepath );
}

2.Compile the DLL project and copy the generated DLL file outputmodulepath. DLL to the current directory, Windows System directory, Windows directory, and path specified by the PATH environment variable. NOTE: If debugging is run, the current directory should be the path of the project file in the current directory. If you click run directly, the current directory should be the path of the EXE program, you can use the following code to obtain the current directory:

Tchar szbuf [max_path];
: Zeromemory (szbuf, max_path );
If (: Getcurrentdirectory (max_path, szbuf) >   0 )
{
// The process directory is obtained successfully.
Afxmessagebox (szbuf );
}

 

PathThe path specified by the environment variable may have multiple paths. Here, you only need to copy outputmodulepath. DLL to one of them. For example, if you copy:D: \ Program Files \ Lua \ 5.1.

 

3.Create a test project (either a dialog box program or a single-document Program) and callPaintmodulepathFunction.

My test steps and results are as follows: Press F5 to debug and run the test program. First, the program outputs the path of the EXE program, and then I delete the DLL file under the EXE program path, however, the output is C: \ WINDOWS \ SYSTEM \Outputmodulepath. dll, I willC: \ WINDOWS \ SYSTEM \Outputmodulepath. dllDelete, and the output isC: \ windows \ outputmodulepath. dllDelete c: \ windows \ outputmodulepath. dll. The output path is the file path in the current directory, and the output path is the path specified by the PATH environment variable.

 

This result is inconsistent with the theory in the book. Is there anything wrong with my test plan?

 

Go to the Forum and get an answer from Microsoft.Article:

 

Dynamic-link library search order

 

Translate it now:

 

Search Order of Dynamic Link Library

 

A system can contain multiple versions of the same dynamic link library (DLL ). Applications can useDynamic Link Library redirectionOrListFileSpecifies the full path of the DLL to be loaded. If these methods are not used, this article describes the DLL search sequence when the DLL is loaded.

 

Standard search order

 

DLLThe search order of depends on whether the DLL search mode is enabled or disabled.

The secure DLL search mode is enabled by default. CreateHKLM \ System \ CurrentControlSet \ Control \ Session Manager \ SafeDllSearchModeYou can disable the registry key and set its value to 0. By calling the setdlldirectory function, you can effectively disable the secure DLL search mode. In this article, the path specified by this function is included in the search range and the search order is changed.

 

Windows XP and Windows 2000 with SP4:The security DLL search mode is disabled by default. CreateHKLM \ System \ CurrentControlSet \ Control \ Session Manager \ SafeDllSearchModeSet the registry key to 1 to enable this attribute. Secure DLL search mode is enabled by default in Windows XP with SP2.

 

Windows 2000:Not SupportedSecure DLL search mode. In this case, the DLL search sequence is the same as that in the secure DLL search mode. The security DLL search mode is supported.Windows 2000.

 

If the security DLL search mode is enabled, the search order is as follows:

1.Application Path

2. Windows SystemDirectory. You can call the getsystemdirectory function to obtain the directory path.

3. 16Directory of the BIT system. No function can obtain the path of this directory, but it will be searched.

4. WindowsDirectory. You can call the getwindowsdirectory function to obtain the directory path.

5.Current Directory

6. PathThe path specified by the environment variable. Note that this does not include the application path specified in the Registry key for each application. The key value of the registry entry in the Application Path is not used as the DLL search path.

 

 

If the security DLL search mode is disabled, the search order is as follows:

1.Application Path

2.Current Directory

3. Windows SystemDirectory. You can call the getsystemdirectory function to obtain the directory path.

4. 16Directory of the BIT system. No function can obtain the path of this directory, but it will be searched.

5. WindowsDirectory. You can call the getwindowsdirectory function to obtain the directory path.

6. PathThe path specified by the environment variable. Note that this does not include the application path specified in the Registry key for each application. The key value of the registry entry in the Application Path is not used as the DLL search path.

 

Search Order

 

The standard search sequence specified by the system can be changed by calling the LoadLibraryEx function and adding the load_with_altered_search_path parameter. The standard search order can also be changed by calling the setdlldirectory function.

 

Windows XP:PassCalling the setdlldirectory function to change the standard search sequence is not supported until Windows XP SP1 appears.

Windows 2000:Not SupportedPassCall the setdlldirectory function to change the standard search order.

 

 

If you specify a standby search order, the program searches in the standby search order until all relevant executable modules are found. After the system starts, the DLL initialization routine is processed, and the system will return to the standard search order.

 

LoadLibraryExBy specifyingLoad_with_altered_search_pathAttributes andLpfilenameThe parameter specifies an absolute path to support a prepared search order.

 

Note: Specify the standard search order and by callingLoad_with_altered_search_pathAttributeLoadLibraryExFunction to setThe pre-search sequence is just a little different: the standard search sequence begins with the search1.The application is located in the path and the pre-search sequence starts from the directory of the executable module to be loaded by the LoadLibraryEx function.

 

If the security DLL search mode is enabled, the search order is as follows:

1.LpfilenameParametersDirectory specified by the value

2. Windows SystemDirectory. You can call the getsystemdirectory function to obtain the directory path.

3. 16Directory of the BIT system. No function can obtain the path of this directory, but it will be searched.

4. WindowsDirectory. You can call the getwindowsdirectory function to obtain the directory path.

5.Current Directory

6. PathThe path specified by the environment variable. Note that this does not include the application path specified in the Registry key for each application. The key value of the registry entry in the Application Path is not used as the DLL search path.

 

 

If the security DLL search mode is disabled, the search order is as follows:

1.LpfilenameParametersDirectory specified by the value

2.Current Directory

3. Windows SystemDirectory. You can call the getsystemdirectory function to obtain the directory path.

4. 16Directory of the BIT system. No function can obtain the path of this directory, but it will be searched.

5. WindowsDirectory. You can call the getwindowsdirectory function to obtain the directory path.

6. PathThe path specified by the environment variable. Note that this does not include the application path specified in the Registry key for each application. The key value of the registry entry in the Application Path is not used as the DLL search path.

 

Assume thatLppathnameThe parameter specifies a path,SetdlldirectoryThe function supports a prepared search order. The search order for this preparation is as follows:

1.Application Path

2.LppathnameDirectory specified by the Parameter

3. Windows SystemDirectory. You can call the getsystemdirectory function to obtain the directory path.

4. 16Directory of the BIT system. No function can obtain the path of this directory, but it will be searched.

5. WindowsDirectory. You can call the getwindowsdirectory function to obtain the directory path.

6. PathThe path specified by the environment variable. Note that this does not include the application path specified in the Registry key for each application. The key value of the registry entry in the Application Path is not used as the DLL search path.

 

IfLppathnameThe parameter is an empty string, and the current directory will be deleted from the search order.

Setdlldirectory Disable security effectively DLL Search mode, while searching for the specified directory path. Restore security DLL Search Mode SafeDllSearchMode Base the registry value and restore the current directory to the search order, call Lppathname The parameter value is Null Of Setdlldirectory Function.

 

After reading this article, I have roughly understood why my test showed that result, because my operating system environment is Win XP + SP3.

 

References:

 

1. MFCIn-depth Introduction

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.