0 out of 1 rated this helpful - Rate this topic

SetDefaultDllDirectories function

Applies to: desktop apps only

Specifies a default set of directories to search when the calling process loads a DLL. This search path is used when LoadLibraryEx is called with no LOAD_LIBRARY_SEARCH flags.

Syntax

 BOOL  WINAPI SetDefaultDllDirectories(
  __in  DWORD DirectoryFlags
);

Parameters

DirectoryFlags [in]

The directories to search. This parameter can be any combination of the following values.

ValueMeaning
LOAD_LIBRARY_SEARCH_APPLICATION_DIR
0x00000200

If this value is used, the application's installation directory is searched.

LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
0x00001000

This value is a combination of LOAD_LIBRARY_SEARCH_APPLICATION_DIR, LOAD_LIBRARY_SEARCH_SYSTEM32, and LOAD_LIBRARY_SEARCH_USER_DIRS.

This value represents the recommended maximum number of directories an application should include in its DLL search path.

LOAD_LIBRARY_SEARCH_SYSTEM32
0x00000800

If this value is used, %windows%\system32 is searched.

LOAD_LIBRARY_SEARCH_USER_DIRS
0x00000400

If this value is used, any path explicitly added using the AddDllDirectory or SetDllDirectory function is searched. If more than one directory has been added, the order in which those directories are searched is unspecified.

 

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The DLL search path is the set of directories that are searched for a DLL when a full path is not specified in a LoadLibrary or LoadLibraryEx function call, or when a full path to the DLL is specified but the system must search for dependent DLLs. For more information about the standard DLL search path, see Dynamic-Link Library Search Order.

The standard DLL search path contains directories that can be vulnerable to a DLL pre-loading attack. An application can use the SetDefaultDllDirectories function to specify a default DLL search path for the process that eliminates the most vulnerable directories and limits the other directories that are searched. The process DLL search path applies only to the calling process and persists for the life of the process.

If the DirectoryFlags parameter specifies more than one flag, the directories are searched in the following order:

  • The directory that contains the DLL (LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR). This directory is searched only for dependencies of the DLL being loaded.
  • The application directory (LOAD_LIBRARY_SEARCH_APPLICATION_DIR).
  • Paths explicitly added to the application search path with the AddDllDirectory function (LOAD_LIBRARY_SEARCH_USER_DIRS) or the SetDllDirectory function. If more than one path has been added, the order in which the paths are searched is unspecified.
  • The System directory (LOAD_LIBRARY_SEARCH_SYSTEM32).

If SetDefaultDllDirectories does not specify LOAD_LIBRARY_SEARCH_USER_DIRS, directories specified with the AddDllDirectory function are used only for LoadLibraryEx function calls that specify LOAD_LIBRARY_SEARCH_USER_DIRS.

It is not possible to revert to the standard DLL search path or remove any directory specified with SetDefaultDllDirectories from the search path. However, the process DLL search path can be overridden by calling LoadLibraryEx with one or more LOAD_LIBRARY_SEARCH flags, and directories added with AddDllDirectory can be removed by calling RemoveDllDirectory.

Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008:  To call this function in an application, use the GetProcAddress function to retrieve its address from Kernel32.dll. KB2533623 must be installed on the target platform.

Requirements

Minimum supported client

Windows 8 Consumer Preview

Minimum supported server

Windows Server 8 Beta

Version

KB2533623 on Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008

Header

LibLoaderAPI.h (include Windows.h);
None on Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008

DLL

Kernel32.dll

See also

AddDllDirectory
Dynamic-Link Library Search Order
Dynamic-Link Library Security
LoadLibrary
LoadLibraryEx
RemoveDllDirectory

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
seems to break LoadLibraryEx() and GDI+ on Windows 7

After SetDefaultDllDirectories() is called on Win7 it seems like LoadLibraryEx() will always fail when LOAD_WITH_ALTERED_SEARCH_PATH is used with a filename that doesn't specify a path.  E.g., GDI+ makes the following API calls that fail, so consequently GDI+ is broken on Windows 7 after SetDefaultDllDirectories() is called:

LoadLibraryExW(L"API-MS-Win-Core-LocalRegistry-L1-1-0.dll", 0, LOAD_WITH_ALTERED_SEARCH_PATH);
LoadLibraryExW(L"advapi32.dll", 0, LOAD_WITH_ALTERED_SEARCH_PATH);

restore
add dll directory