gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:10:23 25 4
gpt4 key购买 nike

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

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

MyConfiguration 接口(interface)

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 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 不会以这种方式导出函数。当它们这样做时,它是通过一种称为反向 P/调用的机制实现的,大多数 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com