使用JNA从java调用VB DLL文件的函数

时间:2015-06-02 07:03:18

标签: java vb.net function dll jna

我是新手使用JNA。我想做的就是在java&中使用vb DLL文件。从java调用函数。 我为此创建了一个简单的java代码。

Debug/Pic/

当我运行这个java程序时,我收到以下错误 -

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

public class Main
{
    public interface test extends Library
    {
        void fn_Today(int a,int b);
    }

    public static void main(String[] args)
    {
        test INSTANCE = (test) Native.loadLibrary(
            (Platform.isWindows() ? "test" : "test"), test.class);

        int a = 1;
        int b=2;

        INSTANCE .fn_Today(a,b); 
    }
}

Dll代码为

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

at com.sun.jna.Function.<init>(Function.java:179)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:344)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:324)
at com.sun.jna.Library$Handler.invoke(Library.java:203)
at com.sun.proxy.$Proxy0.fn_Today(Unknown Source)
at mwrobel.jna.Main.main(Main.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

我怎样才能解决这个问题?

我从依赖性walker获得了以下输出。

Public Function fn_Today(ival As Integer, ival2 As Integer)
Dim ret As Integer
ret = ival + ival2

End Function

1 个答案:

答案 0 :(得分:1)

如果你的VB DLL是一个C兼容的共享库(如果你在它上面运行Dependency Walker,你会看到XXX @ NNN表格的标签),那么以下内容应该有效。

import com.sun.jna.win32.StdCallFunctionMapper;
import com.sun.jna.win32.StdCallLibrary;

public class Main
{
    public interface TestLibrary extends StdCallLibrary
    {
        void fn_Today(int a,int b);
    }

    public static void main(String[] args)
    {
        Map options = new HashMap() {
            { put(Library.OPTION_FUNCTION_MAPPER, new StdCallFunctionMapper()) }
        };
        TestLibrary INSTANCE = (TestLibrary) Native.loadLibrary("test", TestLibrary.class, options);

        // ...
    }
}