Skip to content

Commit c8d189b

Browse files
gtong-nvslangbot
andauthored
Use LOAD_LIBRARY_SEARCH_DEFAULT_DIRS for LoadLibraryExW (#8491)
Before: - Uses `LOAD_LIBRARY_SEARCH_USER_DIRS` in `LoadLibraryExW`, which might cause exception if there is no pathes added by `AddDllDirectory()` After: - Use the composite flag `LOAD_LIBRARY_SEARCH_DEFAULT_DIRS`, which searches for several locations. - Will still search dir added by `AddDllDirectory()`, but avoids empty path seraching if there is no AddDllDirectory() calls. Related to #8462 --------- Co-authored-by: slangbot <[email protected]> Co-authored-by: slangbot <[email protected]>
1 parent c6d8c6b commit c8d189b

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

source/core/slang-platform.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,24 @@ SLANG_COMPILE_TIME_ASSERT(E_OUTOFMEMORY == SLANG_E_OUT_OF_MEMORY);
129129
}
130130

131131
// We try to search the DLL in two different attempts.
132-
// First attempt tries on the directories explicitly specified with AddDllDirectory(),
133-
// If it failed to find one, we will search over all PATH.
134-
// Windows API made two approaches mutually exclusive and we need to try two times.
132+
// First attempt - LoadLibraryExW()
133+
// If it failed to find one, we will use LoadLibraryW() to search over all PATH.
134+
// Search order: 1) The directory that contains the DLL (LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR).
135+
// This directory is searched only for dependencies of the DLL being loaded.
136+
// 2) Application directory
137+
// 3) User directories (AddDllDirectory/SetDllDirectory)
138+
// 4) System32
139+
// 5) PATH environment variable (by the 2nd attempt with LoadLibraryW())
135140
// https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw
136-
String platformFileNameStr(platformFileName);
137-
HMODULE h =
138-
LoadLibraryExW(platformFileNameStr.toWString(), nullptr, LOAD_LIBRARY_SEARCH_USER_DIRS);
139-
// If LoadLibraryExW failed, try again with LoadLibraryW.
140141
// https://docs.microsoft.com/en-us/windows/desktop/api/libloaderapi/nf-libloaderapi-loadlibraryw
141-
if (!h)
142-
h = LoadLibraryW(platformFileNameStr.toWString());
142+
String platformFileNameStr(platformFileName);
143+
OSString wideFileName = platformFileNameStr.toWString();
144+
HMODULE handle = LoadLibraryExW(wideFileName, nullptr, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
145+
146+
if (!handle)
147+
handle = LoadLibraryW(wideFileName);
143148
// If still not found, return an error.
144-
if (!h)
149+
if (!handle)
145150
{
146151
const DWORD lastError = GetLastError();
147152
switch (lastError)
@@ -164,7 +169,7 @@ SLANG_COMPILE_TIME_ASSERT(E_OUTOFMEMORY == SLANG_E_OUT_OF_MEMORY);
164169
// Turn to Result, if not one of the well known errors
165170
return HRESULT_FROM_WIN32(lastError);
166171
}
167-
handleOut = (Handle)h;
172+
handleOut = (Handle)handle;
168173
return SLANG_OK;
169174
}
170175

0 commit comments

Comments
 (0)