从 Java 调用 C# dll
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10977200/
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
Calling C# dll from Java
提问by rajshekhar
I am a Java developer. But, for some reason I have to take the help of C# to accomplish my task. I have below mentioned C# code which is used to create a DLL. That DLL has to be used in my Java program to do the needful.
我是一名 Java 开发人员。但是,出于某种原因,我必须借助 C# 来完成我的任务。我在下面提到了用于创建 DLL 的 C# 代码。必须在我的 Java 程序中使用该 DLL 来完成所需的工作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
namespace Yrl.Tracingtool
{
public class DocxUtil
{
public Application Jump(string fileName)
{
object fileNameAsObject = (object)fileName;
Application wordApplication;
try
{
wordApplication = new Application();
object readnly = false;
object missing = System.Reflection.Missing.Value;
wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readnly, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
object count = 3;
wordApplication.Selection.GoTo(ref what, ref which, ref count, ref missing);
return wordApplication;
}
catch (Exception ex)
{
//LogEntry log = new LogEntry();
//log.Categories.Add("Trace");
//log.Message = ex.ToString();
//Logger.Write(log, "Trace");
throw new System.IO.FileLoadException("File cannot be opened");
}
finally
{
wordApplication = null;
}
}
}
}
I have checked this forum and other forums too, but most of them talk about using a C++ or C DLL file in a JNI call. If anyone is having knowledge of calling C# DLL from Java please let me know.
我也检查了这个论坛和其他论坛,但他们中的大多数都谈论在 JNI 调用中使用 C++ 或 C DLL 文件。如果有人知道从 Java 调用 C# DLL,请告诉我。
回答by olldman
We use ICEto communicate between .NET and Java apps. It is not exactly what you need but might help.
我们使用ICE在 .NET 和 Java 应用程序之间进行通信。这并不完全是您所需要的,但可能会有所帮助。
Also I'd recommend googling ".NET Java bridge"- there are several frameworks for this (jni4net, JNBridge etc)
此外,我建议使用谷歌搜索“.NET Java 桥”- 有几个框架(jni4net、JNBridge 等)
回答by Massimiliano Peluso
you may have to use JNI.
您可能必须使用 JNI。
JNI is an acronym of Java Native Interface. Using JNI, we can call functions which are written in other languages from Java
JNI 是 Java Native Interface 的缩写。使用JNI,我们可以从Java调用其他语言编写的函数
have a look at the below
看看下面的