I want to figure out which dependent DLL
s are missing when loading a DLL
fails. By loading a DLL
using Java
's System#loadLibrary
, I only get a long negative exit code such as -1073741515
instead of a Windows error message stating which DLL
is missing. This is unhelpful for addressing the problem.
My idea was to parse dependent DLL
s from the DLL
using Java
and loading them one by one to figure out which one throws an UnsatisfiedLinkError
. I found a library called pecoff4j
which claims to parse PE
executables but I'm not able to parse the imported DLL
names:
PE pe = PEParser.parse("C:\\Users\\User\\Desktop\\my.dll");
final ImportDirectory importTable = pe.getImageData().getImportTable();
for (int i = 0; i < importTable.size(); i++)
{
String name = importTable.getName(i);
System.out.println(name);
}
This yields the following exception since the names don't seem to be available:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at org.boris.pecoff4j.ImportDirectory.getName(ImportDirectory.java:39)
ImportDirectory.java
:
package org.boris.pecoff4j;
import org.boris.pecoff4j.util.DataObject;
import java.util.ArrayList;
import java.util.List;
public class ImportDirectory extends DataObject {
private List<ImportDirectoryEntry> entries = new ArrayList();
private List<String> names = new ArrayList();
private List<ImportDirectoryTable> nameTables = new ArrayList();
private List<ImportDirectoryTable> addressTables = new ArrayList();
public void add(ImportDirectoryEntry entry) {
entries.add(entry);
}
public void add(String name, ImportDirectoryTable names,
ImportDirectoryTable addresses) {
this.names.add(name);
nameTables.add(names);
addressTables.add(addresses);
}
public int size() {
return entries.size();
}
public String getName(int index) {
return names.get(index);
}
public ImportDirectoryTable getNameTable(int index) {
return nameTables.get(index);
}
public ImportDirectoryTable getAddressTable(int index) {
return addressTables.get(index);
}
public ImportDirectoryEntry getEntry(int index) {
return entries.get(index);
}
}
Visual Studio
's dumpbin
works but I need a Java
based solution to integrate into an application which is distributed to users who don't necessarily have Visual Studio
installed.
If you think there is a better/simpler way to figure out or prevent missing DLL
s when loading a DLL
using Java
, feel free to let me know as well.