2

We are trying to develop a Java application that loads a DLL (written in C++) and uses its functions.

When opening this DLL in "DLL Export Viewer" we can see the full signature of the functions exported, unlike any other DLL we load into the viewer:

Exported view of the DLL that doesn't work

We have tried to create some sample DLLs and load them to Java, and we were successful. The visibile difference was that when we loaded these DLLs we created into "DLL Export Viewer", we saw the functions without full signatures (only names):

DLL We created, works from Java

The code we use to load the DLL from Java is using JNA, and looks like this:

Declaring an interface that mathces the DLL functions:

public interface Ariel extends Library {
   Ariel INSTANCE = (Ariel) Native.loadLibrary("ariel", Ariel.class);
   void _ZN5ArielC1Ev();
   int _ZN5Ariel8getArielEv();
}

Loading it and calling its functions:

public static void main(String[] args) {
    Ariel ariel = Ariel.INSTANCE;
    ariel._ZN5ArielC1Ev();

    System.out.println("done");

}

Only when trying to load the DLL shown in the first image, we can't call any function and we always get the following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'resetScale': The specified procedure could not be found.

at com.sun.jna.Function.<init>(Function.java:208)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:536)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:513)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:499)
at com.sun.jna.Library$Handler.invoke(Library.java:199)
at com.sun.proxy.$Proxy0.resetScale(Unknown Source)
at Bolet.main(Bolet.java:6)

1 Answers1

1

Your DLL is exporting C++-mangled names. The DLL export is un-mangling them for you. If you examine the DLL with a different viewer, you should see the raw names instead.

If you want to export the names in a non-mangled manner, you need to use the extern "C" decorator on a given function declaration. This generally will only work for static methods (not class methods).

However, this is only part of your problem. If you want to directly map Java classes onto C++ classes, you'll need to use something like SWIG and compile some native glue code.

Community
  • 1
  • 1
technomage
  • 9,861
  • 2
  • 26
  • 40