vb.net 如何在 Java 中使用 .Net dll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16460899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:32:57  来源:igfitidea点击:

How to use .Net dll in Java

javavb.netdlljava-native-interface

提问by Sudz

I have dll created in vb.net. How can i use its functions in JAVA.

我在 vb.net 中创建了 dll。我如何在 JAVA 中使用它的功能。

I found something JNI while searching on google , but not getting it. Is there any simple documentation with example.

我在 google 上搜索时发现了一些 JNI,但没有得到它。是否有任何带有示例的简单文档。

回答by Santosh

I would recommend Java Native Access (JNA)as its easier than using JNI. Lets say you have a DLL with some functions,

我会推荐Java Native Access (JNA),因为它比使用 JNI 更容易。假设你有一个带有一些函数的 DLL,

  • Create an java interface which has the same method signaturesas the functions in DLL.
  • 创建一个DLL 中的函数具有相同方法签名的 java 接口。

For example

例如

public interface NativeExample{

  public int method1(String param1);
  public boolean mehthod2();

}
  • Now following is the way you load the DLL (assuming its name is NativeLib.dll)

    NativeExample nativeExample= (NativeExample) Native.loadLibrary("NativeLib", NativeExample.class);

  • 下面是你加载DLL的方式(假设它的名字是NativeLib.dll)

    NativeExample nativeExample= (NativeExample) Native.loadLibrary("NativeLib", NativeExample.class);

Once you have this, you can call the method from the DLL via java methods.

一旦你有了这个,你就可以通过 java 方法从 DLL 调用该方法。

`nativeExample.method("test");`

`nativeExample.method2();`

For mappings of the datatypes between Java and Native, please refer the the link above.

Java 和 Native 之间的数据类型映射,请参考上面的链接。

Here is one more example.

这里还有一个例子

回答by Przemys?aw ?adyński

Exactly JNI will not give you direct access to .NET unless you work a lot on this. You can build own wrappers and use C/C++ as middle-ware but for small projects it will never pay off. Remember that calling method is one thing but passing arguments in proper types, retrieving results, subscribing events etc..etc.. is a lot more.

除非您在这方面做了很多工作,否则 JNI 不会让您直接访问 .NET。您可以构建自己的包装器并使用 C/C++ 作为中间件,但对于小型项目,它永远不会有回报。请记住,调用方法是一回事,但以正确的类型传递参数、检索结果、订阅事件等……等等……还有很多。

Therefore I would also propose to check for third-party tools which are well prepared for these kind of scenarios.

因此,我还建议检查为此类场景做好充分准备的第三方工具。

First check at least these two:

首先至少检查这两个:

Javonet I would recommend for all small and quick projects as this is light solution which provide very high performance due to in process communication and does all background work for you. All you need is call "AddReference(your.dll)" and next invoke any method using reflection-style API. You can invoke any methods, set/get fields and properties, get results, subscribe events or handle exceptions.

Javonet 我会推荐用于所有小型和快速项目,因为这是一种轻量级的解决方案,由于进程中的通信而提供非常高的性能,并为您完成所有后台工作。您所需要的只是调用“AddReference(your.dll)”,然后使用反射式 API 调用任何方法。您可以调用任何方法、设置/获取字段和属性、获取结果、订阅事件或处理异常。

Very similar way works JNBridge which has a lit bit more additional staff/extensions for popular enterprise scenarios like cloud integration or websphere but this one I would recommend for bigger projects were you expect to bridge java and .net on separate machines it's more powerful but more complicated and heavy as well.

JNBridge 的工作方式非常相似,它为流行的企业场景(如云集成或网络领域)提供了更多的额外人员/扩展,但如果您希望在不同的机器上桥接 java 和 .net,我会推荐这个用于更大的项目,它更强大,但更多复杂而沉重。

Both are free to try and Javonet is free for non-commercial and academic usage, so try, test and choose what best suits your requirements.

两者都可以免费试用,而 Javonet 可免费用于非商业和学术用途,因此请尝试、测试并选择最适合您的要求。

回答by Rajesh

Well, there are third party tools / libraries that will help you connect Java to .NET. If you want to do it yourself -- meaning implement the JNI wrappers yourself -- you actually need to implement 2 sets of wrappers.

嗯,有第三方工具/库可以帮助您将 Java 连接到 .NET。如果你想自己做——意味着你自己实现 JNI 包装器——你实际上需要实现 2 组包装器。

JNI will get you to C/C++, which is not allowed to directly access .NET objects. At that point, you can implement another wrapper, a .NET object with only static methods, that your C/C++ wrapper can call. Since unmanaged C/C++ code can't own .NET objects, it can only call static methods of .NET classes.

JNI 将使您使用 C/C++,它不允许直接访问 .NET 对象。此时,您可以实现另一个包装器,一个只有静态方法的 .NET 对象,您的 C/C++ 包装器可以调用它。由于非托管 C/C++ 代码不能拥有 .NET 对象,因此它只能调用 .NET 类的静态方法。