【问题标题】:Call a function of VB DLL file from java using JNA使用 JNA 从 java 调用 VB DLL 文件的函数
【发布时间】:2015-06-02 07:03:18
【问题描述】:

我是使用 JNA 的新手。我想做的就是在 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.

【问题讨论】:

标签: java vb.net function dll jna


【解决方案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);

        // ...
    }
}

【讨论】:

  • 我仍然收到此错误。我认为问题出在 dll 中。我怎么知道 VB DLL 是否是与 C 兼容的共享库? @technomage
  • 在您的问题中包含来自DependencyWalker 的相关输出。您还应该为您的问题添加更新,以反映您迄今为止所做的更改。
  • 我确实在我的问题中添加了一个更新。查看并给我回复@technomage
  • 您是否更新了代码以匹配答案中的示例?依赖漫游器显示的符号在您的 DLL 中是什么?
  • 我收到警告:未找到至少一个延迟加载依赖模块。警告:由于延迟加载依赖模块中缺少导出功能,至少有一个模块存在未解析的导入。
猜你喜欢
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
相关资源
最近更新 更多