Windows DLL search order
When Windows program loads a DLL directly or indirectly through loadlibrary / loadlibraryex, windows will search the system for the specified DLL in a certain order, which is called DLL search order.
The following is an example of a.exe calling b.dll:
- System environmental impact
- If the path of b.dll is specified when loadlibrary / loadlibraryex is called, and there is no b.dll under the path, loadlibrary / loadlibraryex will fail directly.
- If the b.dll module already exists in the memory, the system only performs the manifest check and redirection check, regardless of whether the specified b.dll path is consistent with the b.dll in memory, and the system will not try to search b.dll again.
- Knowndlls, HKEY in the registry_ LOCAL_ The name of the so-called knowndlls and their paths are registered in machine / system / currentcontrolset / control / session manager / knownddlls. If b.dll exists under the key, the system will load the b.dll with the path specified by the registry key, regardless of the path specified in loadlibrary / loadliraryex.
- If the loaded b.dll depends on other DLLs, the dependent DLLs will be loaded in the same way as the b.dll does not specify a path. (if b.dll depends on its DepDll.dll , search DepDll.dll Does not start from the directory where b.dll is located)
- Standard DLL search order Without any additional conditions, the standard DLL search order is as follows:
- Loading directory of application program: D: sr0ad
- Current directory (the default is the program loading directory, which can be modified through SetCurrentDirectory and obtained through GetCurrentDirectory)
- System directory (for 32-bit system, it is usually C: Windows / system32, which can be obtained through GetSystemDirectory)
- 16 bit system directory (ignored)
- Windows directory (usually C: windows, which can be obtained through getwindows directory)
- All paths listed in the path environment variable
- DLL redirection
- DLL redirection is used to solve the problem that multiple versions of the same DLL exist in the system. When b.dll already exists in the system, and a needs its own b.dll, it needs DLL redirection.
- Create a redirection file with the suffix of application name +. Local in the application installation directory D: ﹣ sr0ad ﹣ a exe.local , b.dll must be installed in the directory D:.
- Regardless of redirection file a exe.local When using loadlibrary / loadlibraryex to load b.dll, the system will load D:: (sr0ad) b.dll as long as it exists, regardless of whether loadlibrary / loadlibraryex specifies the full path for it.
- If a exe.local It's a directory, and D: sr0ad / A exe.local \B. If the DLL exists, the system will load D: exe.local \B. DLL, again regardless of whether the full path is specified for loadlibrary / loadlibraryex.