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

本秂侑毒 提交于 2019-12-07 16:34:25

问题


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



来源:https://stackoverflow.com/questions/14304530/java-jna-cant-find-function-in-dll-c-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!