SetDefaultDllDirectories function (libloaderapi.h)

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 SetDefaultDllDirectories(
  [in] DWORD DirectoryFlags
);

Parameters

[in] DirectoryFlags

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

Value Meaning
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

Requirement Value
Minimum supported client Windows 8 [desktop apps only],KB2533623 on Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008
Minimum supported server Windows Server 2012 [desktop apps only]
Target Platform Windows
Header libloaderapi.h (include Windows.h)
DLL Kernel32.dll

See also

AddDllDirectory

Dynamic-Link Library Search Order

Dynamic-Link Library Security

LoadLibrary

LoadLibraryEx

RemoveDllDirectory