From b1066a1dfeca09f0395eb7b8d985a5f8b558aed6 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 2 Jul 2024 10:41:09 +0200 Subject: [PATCH] windows: making library loading work after `SetDefaultDllDirectories()` call (#1413) --- src/com/sun/jna/NativeLibrary.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/com/sun/jna/NativeLibrary.java b/src/com/sun/jna/NativeLibrary.java index ea5278b9b..caa171be4 100644 --- a/src/com/sun/jna/NativeLibrary.java +++ b/src/com/sun/jna/NativeLibrary.java @@ -164,11 +164,15 @@ Object invoke(Method invokingMethod, Class[] paramTypes, Class returnType, } private static final int DEFAULT_OPEN_OPTIONS = -1; - private static int openFlags(Map options) { + private static final int DEFAULT_WINDOWS_RELATIVE_PATH_OPEN_OPTIONS = 0; + private static int openFlags(Map options, boolean isAbsolutePath) { Object opt = options.get(Library.OPTION_OPEN_FLAGS); if (opt instanceof Number) { return ((Number)opt).intValue(); } + if (Platform.isWindows() && !isAbsolutePath) { + return DEFAULT_WINDOWS_RELATIVE_PATH_OPEN_OPTIONS; + } return DEFAULT_OPEN_OPTIONS; } @@ -178,7 +182,7 @@ private static NativeLibrary loadLibrary(final String libraryName, final Map exceptions = new ArrayList<>(); boolean isAbsolutePath = new File(libraryName).isAbsolute(); LinkedHashSet searchPath = new LinkedHashSet<>(); - int openFlags = openFlags(options); + int openFlags = openFlags(options, isAbsolutePath); // // Prepend any custom search paths specifically for this library @@ -475,7 +479,7 @@ public static final NativeLibrary getInstance(String libraryName, Map if (library == null) { if (libraryName == null) { - library = new NativeLibrary("", null, Native.open(null, openFlags(options)), options); + library = new NativeLibrary("", null, Native.open(null, openFlags(options, true)), options); } else { library = loadLibrary(libraryName, options);