C# 如何从 Java 调用 .NET dll
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10743715/
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
How to call into .NET dll from Java
提问by soamazing
I have this code to create a simple .NET .dll. It only returns an int.
我有这个代码来创建一个简单的 .NET .dll。它只返回一个int.
But, it is not working inside Java.
但是,它在 Java 中不起作用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReturnINT
{
public class ReturnINT
{
public static int RetornaInteiro ()
{
try
{
int number = 2;
return number;
}
catch (Exception)
{
return 1;
}
}
}
}
How can I call the method from within Java?
如何从 Java 内部调用该方法?
When I Use JNI i have this error IN java:
当我使用 JNI 时,我在 java 中出现此错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Dll.RetornaInteiro()V
at Dll.RetornaInteiro(Native Method)
at Dll.main(Dll.java:27)
回答by Jirka Hanika
You can call it directly: http://jni4net.sourceforge.net/
可以直接调用:http: //jni4net.sourceforge.net/
Or you can call it as an executable.
或者您可以将其称为可执行文件。
回答by Przemys?aw ?adyński
Check the http://www.javonet.comas well. With one-jar file you can load this dll and call as follows:
也请检查http://www.javonet.com。使用一个 jar 文件,您可以加载此 dll 并按如下方式调用:
Javonet.AddReference("your-lib.dll");
int result = Javonet.getType("ReturnINT").Invoke("RetornaInteiro");
Javonet will automatically load your library in .NET process and give you access to any classes and types contain within it. Next you can get your type and invoke static method. Method results and arguments are automatically translated between JAVA and .NET types. You can pass for example string or bool arguments like that
Javonet 将在 .NET 进程中自动加载您的库,并允许您访问其中包含的任何类和类型。接下来,您可以获取您的类型并调用静态方法。方法结果和参数会在 JAVA 和 .NET 类型之间自动转换。您可以传递例如字符串或布尔参数
Boolean arg1 = true;
String arg2 = "test";
Javonet.getType("ReturnINT").Invoke("MethodWithArguments",arg1,arg2);
And they will be translated automatically.
它们将被自动翻译。
In addition you can also create instance of your type, subscribe events, set/get properties and fields, handle exceptions or even pass value-type arguments. Check the docs for more details:
此外,您还可以创建类型的实例、订阅事件、设置/获取属性和字段、处理异常甚至传递值类型参数。查看文档以获取更多详细信息:
http://www.javonet.com/quick-start-guide/
http://www.javonet.com/quick-start-guide/
PS: I am member of Javonet team. Therefore feel free to ask me any detailed questions regarding native integrations and our product.
PS:我是 Javonet 团队的成员。因此,请随时向我询问有关本机集成和我们产品的任何详细问题。

