Java (JNA) - can't find function in DLL (C++)

2019-05-10 18:36发布

问题:

I am new in Java, searched for this question in google and stackoverflow, found some posts, but still I can't understand.

I want to use DLL libary (C++) methods from Java. I use JNA for this purpose. JNA found my library but it can't find my method: Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'LoadCurrentData': The specified procedure could not be found.

My code:

package javaapplication1;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;

public class JavaApplication1 {

    public interface LibPro extends Library {
        LibPro INSTANCE = (LibPro) Native.loadLibrary(
            (Platform.isWindows() ? "LibPro" : "LibProLinuxPort"), LibPro.class);

        public  short LoadCurrentData();
    }

    public static void main(String[] args) {
      LibPro sdll = LibPro.INSTANCE;
      sdll.LoadCurrentData();  // call of void function
    }
 }

I looked in my DLL with Depency Walker Tool and saw that my function name has prefix and suffix - it looks like _LoadCurrentData@0

Thanks for response!

P.S. I found good example which works http://tutortutor.ca/cgi-bin/makepage.cgi?/articles/rjna (Listing 6).

回答1:

I'd say that you need to apply correct name mapper, as you noticed function name got mangled, you need to register CallMapper that will implement the same mangling as your compiler.

Here is a revelant entry from JNA homepage:

Use a dump utility to examine the names of your exported functions to make sure they match (nm on linux, depends on Windows). On Windows, if the functions have a suffix of the form "@NN", you need to pass a StdCallFunctionMapper as an option when initializing your library interface. In general, you can use a function mapper (FunctionMapper) to change the name of the looked-up method, or an invocation mapper (InvocationMapper) for more extensive control over the method invocation.

Here is a possibly revelant question: renaming DLL functions in JNA using StdCallFunctionMapper



标签: java dll jna