[wpiutil] Restore existing dll directory when setting dll directory (#2559)

* Allow getting existing dll directory when setting dll directory
This commit is contained in:
Thad House
2020-06-29 16:33:26 -07:00
committed by GitHub
parent dac0e5b133
commit d9c7bbd046

View File

@@ -34,7 +34,7 @@ public final class CombinedRuntimeLoader {
extractionDirectory = directory;
}
public static native boolean addDllSearchDirectory(String directory);
public static native String setDllDirectory(String directory);
private static String getLoadErrorMessage(String libraryName, UnsatisfiedLinkError ule) {
StringBuilder msg = new StringBuilder(512);
@@ -126,7 +126,12 @@ public final class CombinedRuntimeLoader {
public static void loadLibrary(String libraryName, List<String> extractedFiles)
throws IOException {
String currentPath = null;
String oldDllDirectory = null;
try {
if (RuntimeDetector.isWindows()) {
var extractionPathString = getExtractionDirectory();
oldDllDirectory = setDllDirectory(extractionPathString);
}
for (var extractedFile : extractedFiles) {
if (extractedFile.contains(libraryName)) {
// Load it
@@ -138,6 +143,10 @@ public final class CombinedRuntimeLoader {
throw new IOException("Could not find library " + libraryName);
} catch (UnsatisfiedLinkError ule) {
throw new IOException(getLoadErrorMessage(currentPath, ule));
} finally {
if (oldDllDirectory != null) {
setDllDirectory(oldDllDirectory);
}
}
}
@@ -163,7 +172,6 @@ public final class CombinedRuntimeLoader {
// Load windows, set dll directory
currentPath = Paths.get(extractionPathString, "WindowsLoaderHelper.dll").toString();
System.load(currentPath);
addDllSearchDirectory(extractionPathString);
}
} catch (UnsatisfiedLinkError ule) {
throw new IOException(getLoadErrorMessage(currentPath, ule));