c# - java中通过JNA访问.net dll

标签 c# java dll java-native-interface jna

我有一个 .net 4.0 dll,它有一个命名空间,在该命名空间中有一个类,我想使用 jna 访问该类内的过程。

我已经使用 maven 包含了 jna.jar 以及 platform.jar(以防万一), 我的 java 代码如下所示

MyConfiguration界面

import com.sun.jna.Library;

public interface MyConfiguration extends Library{
    public void callInterface();
}

访问dll代码

MyConfiguration myAPI = (MyConfiguration) Native
                       .loadLibrary("dll/MyAPI.dll", MyConfiguration.class);
System.out.println("Interface Created");
System.out.println("Calling Interface");
myAPI.callInterface();

但我遇到了异常(exception)--->

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

    at com.sun.jna.Function.<init>(Function.java:208)
    at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:536)
    at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:513)
    at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:499)
    at com.sun.jna.Library$Handler.invoke(Library.java:199)
    at com.sun.proxy.$Proxy0.myInterface(Unknown Source)
    at foo.App.main(App.java:83)

我用dll反编译工具检查了dll,里面有调用的函数,谁能帮忙。

最佳答案

dll using dll decompiler tool

您需要使用 PE(可移植可执行文件)查看器来查找导出表中的条目。 ( Depends 就是其中之一。)

大多数 .NET DLL 不以这种方式导出函数。当他们这样做时,是通过一种称为 Reverse P/Invoke 的机制进行的,大多数 Microsoft .NET 语言编译器都不支持该机制。 C++/CLI 语言就是为此目的而设计的。

您可能会通过 Java-.NET 桥接产品找到一条更短的成功之路。或者,如果 .NET DLL 将类公开为 COM 对象,则为 Java-COM 桥接产品。 (使用 OLE/COM Object Viewer 检查 COM DLL。)

此外,请确保 DLL 与您的 JVM 进程(例如 java.exe 或 javaw.exe)具有相同的位数,除非您用作进程外 COM 对象。

关于c# - java中通过JNA访问.net dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22559840/

相关文章:

c# - 如何从 C++ dll 中删除 C# dll

c++ - 用变量模拟 C++ 命令行参数

c# - 在 Visual Studio 代码中隐藏 .meta 和 .cs.meta 文件

java - 对于(房间 r : ArrayList<Room>) returns empty Room

c# - 如何让异常在 C# 中定义自己的消息?

java - Java 中的动态方法绑定(bind)

java - 测试 java 列表中的值

c# - .NET 中 dll 的版本号

c# - 将数组作为属性值传递

c# - 判断程序集是否是一个gui应用程序