Skip to content

Commit

Permalink
Avoid undefined behaviour of LoadLibrary() on Windows.
Browse files Browse the repository at this point in the history
According to [1] the flag LOAD_WITH_ALTERED_SEARCH_PATH
should be used with absolute paths only. It has the effect,
that the path of the DLL to be loaded is temporary added
to the search path, so that dependent DLLs in the same
directory can be found and loaded implicit.

However for relative paths the standard LoadLibrary()
search order should be used, because the behaviour of
LOAD_WITH_ALTERED_SEARCH_PATH isn't defined for relative
paths. In practice (on Windows 10) relative paths to
Windows system DLLs do work, so that the library is loaded,
but DLLs in other search paths are not found, when this
flag is set.

Ruby's fiddle library uses LoadLibrary without flags in
both cases.

[1]
https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx
  • Loading branch information
larskanis committed Jan 13, 2017
1 parent cd1ea5c commit b3a0a87
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ext/ffi_c/DynamicLibrary.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ dl_open(const char* name, int flags)
if (name == NULL) {
return GetModuleHandle(NULL);
} else {
return LoadLibraryExA(name, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
DWORD dwFlags = PathIsRelativeA(name) ? 0 : LOAD_WITH_ALTERED_SEARCH_PATH;
return LoadLibraryExA(name, NULL, dwFlags);
}
}

Expand Down

0 comments on commit b3a0a87

Please sign in to comment.