Windows: JNA fails to load libraries if SetDefaultDllDirectories() was used
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- java
- Domain
- operating-systems
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.
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.
- Dominant language
- Java
- Stars
- 8.9k
- Forks
- 1.7k
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 1
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
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.
More from java-native-access/jna
-
Difficulty 4/5 3-5 days Newbie friendliness 66/100
java-native-access/jna#1738 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
java-native-access/jna#1736 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 65/100
java-native-access/jna#1717 ·
-
Windows32Exception - The parameter is incorrectPossibly taken @marktech0813 claimed this 332 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
java-native-access/jna#1700 · 11 comments ·
-
feature request
Difficulty 4/5 3-5 days Newbie friendliness 35/100
java-native-access/jna#1698 · 1 comment ·
All issues in java-native-access/jna
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Netcracker/qubership-integration-platform#1046 ·
Maintainers usually reply within 2 days
-
`check_java_version()` fails when Java path contains spaces (Windows / Git Bash, `C:\Program Files`)Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Fix Math.ceilDiv wrong result for exact positive divisionsPossibly taken @pamod-madubashana claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
scala-native/scala-native#5094 ·
Maintainers usually reply within 1 day
-
NullPointerException in blocking command completion callback when the command succeeds (3.52.0)Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days