Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Windows: JNA fails to load libraries if SetDefaultDllDirectories() was used

Open
#1,413 1 comment 0 reactions 0 assignees View on GitHub

@trespasserw is already working on this.

Since Jul 2, 2024.

  • #1614 by @trespasserw — open

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

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

  1. Version of JNA and related jars - any since 93ebb9f7
  2. Version and vendor of the java virtual machine - any
  3. Operating system - any Windows
  4. 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:

  1. Since 93ebb9f7, JNA uses LOAD_WITH_ALTERED_SEARCH_PATH in LoadLibraryExW().
  2. MSDN says: If this value is used and lpFileName specifies a relative path, the behavior is undefined
  3. When library name is given like "dwmapi", this is a relative path.
  4. 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)
  5. However, if SetDefaultDllDirectories() was also used, ntdll!LdrpGetDllPath considers this a fatal error and returns STATUS_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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from java-native-access/jna

All issues in java-native-access/jna

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.