java-native-access / java-native-access/jna
Windows: JNA fails to load libraries if SetDefaultDllDirectories() was used
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 8.9k
- Forks
- 1.7k
- PR merge metrics
- No merged PRs in 30d
Description
- Version of JNA and related jars - any since 93ebb9f7
- Version and vendor of the java virtual machine - any
- Operating system - any Windows
- System architecture (CPU type, bitness of the JVM) - any
Example snippet:
public interface Dwmapi extends StdCallLibrary {
}
public interface Kernel32_2 extends StdCallLibrary {
Pointer AddDllDirectory(WString NewDirectory);
boolean SetDefaultDllDirectories(int DirectoryFlags);
}
public static void main(String[] args) {
final Kernel32_2 kernel32 = Native.loadLibrary("kernel32", Kernel32_2.class);
final int LOAD_LIBRARY_DEFAULT_DIRS = 0x00001000;
// SetDefaultDllDirectories() is used to supply another DLL search path.
// But it causes JNA to fail later.
Path pluginsPath = Paths.get("plugins").toAbsolutePath();
kernel32.SetDefaultDllDirectories(LOAD_LIBRARY_DEFAULT_DIRS);
kernel32.AddDllDirectory(new WString(pluginsPath.toString()));
// This is perfectly fine, but fails after SetDefaultDllDirectories().
Native.load("dwmapi", Dwmapi.class);
}
Explanation:
- Since 93ebb9f7, JNA uses
LOAD_WITH_ALTERED_SEARCH_PATHinLoadLibraryExW(). - MSDN says:
If this value is used and lpFileName specifies a relative path, the behavior is undefined - When library name is given like
"dwmapi", this is a relative path. - Windows logs this error, but still tries to proceed - set WinDBG breakpoint to see for yourself:
bp ntdll!LdrpLogRelativePathWithAlteredSearchError "du @rcx"(here, even the function's name is already a proof) - However, if
SetDefaultDllDirectories()was also used,ntdll!LdrpGetDllPathconsiders this a fatal error and returnsSTATUS_INVALID_PARAMETER
TLDR: JNA uses LOAD_WITH_ALTERED_SEARCH_PATH and this is a bug to use it with non-absolute path. However, in most cases it still works, because Windows is kind to interlopers.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing JNA's native library-loading path from Native.load to its LoadLibraryExW call, then reproduce the provided example after SetDefaultDllDirectories() is used. Compare the relative-library-name behavior with Windows' documented search-path rules; done means the example loads successfully and the regression is covered by a test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100