0

I want to figure out which dependent DLLs 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 DLLs 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 DLLs when loading a DLL using Java, feel free to let me know as well.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • It looks like the method `ImportDirectory.size()` does not give the correct size of the actual list size of names. The ArrayList of Names is not in sync. To tell more, we would need to see the code of the Class ImportDirectory. – Sunchezz Oct 22 '19 at 14:16
  • @Sunchezz: Yeah, they're not in sync. I added the class to the question. – BullyWiiPlaza Oct 22 '19 at 14:18
  • You never add something to the list entries, but in the method size() you are asking for the size of the list entries, which is always 0. – Sunchezz Oct 22 '19 at 14:20
  • My Bad... there is a method which adds entries. but, if this one is called, no names are added to the names list. – Sunchezz Oct 22 '19 at 14:22

0 Answers0