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

Call a function of VB DLL file from java using JNA

我刚开始使用 JNA.In 我想做的就是使用 java 中的 vb DLL 文件并调用 java 中的函数。 我为此创建了一个简单的 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); 
    }
}

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

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)

Dll代码如

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

End Function

我该如何解决这个问题?

我从 dependency walker 得到以下输出。

Warning: At least one delay-load dependency module was not found.
Warning: At least one module has an unresolved import due to a missing  
export function in a delay-load dependent module.

如果您的 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);

        // ...
    }
}