我已经在 C# 库中编写了很多代码,现在需要从 Java 调用它们。
我看到有人建议使用 JNA,但我什至无法摆脱起始块;那里的文档非常粗略。
首先,它似乎只是向您展示如何连接到 Native C 库,这对我来说没有好处;我想连接到我自己的图书馆。那里的代码示例显示:
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)
Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),
CLibrary.class);
void printf(String format, Object... args);
}
我想连接到我的库 (MyLibrary.dll),并调用 MyNamespace.MyClass
上的静态方法,其 C# 签名是:
public static string HelloWorld(string p)
那么我要给 提供哪些参数Native.loadLibrary()?
这只是“你好世界”。如果我想返回一个对象怎么办?假设 MyClass
也有一个静态方法,
public static MyClass GetInstance()
我如何使用 JNA 调用它?我想我必须在 Java 中定义一个与 C# MyClass
接口相匹配的接口...但是它必须是详尽的,即对于 MyClass
的每个公共成员我必须在 Java 的 IMyClass
接口中声明一个方法吗?或者我可以省略我不关心的接口吗?
任何示例代码将不胜感激!
I've written a lot of code in a C# library, which I now need to call from Java.
I saw it recommended on SO to use JNA, but I'm having trouble even getting out of the starting blocks; the documentation there is very sketchy.
Firstly, it only appears to show you how to connect to the Native C library, which is no good to me; I want to connect to my own library. The code sample there shows:
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)
Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),
CLibrary.class);
void printf(String format, Object... args);
}
I want to connect to my library (MyLibrary.dll), and call a static method on MyNamespace.MyClass
whose C# signature is:
public static string HelloWorld(string p)
So what parameters to I give to Native.loadLibrary()
?
That's just for "Hello World". What if I want to return an object? Let's say MyClass
also has a static method
public static MyClass GetInstance()
How would I call that using JNA? I guess I would have to define an interface in Java that matches the C# MyClass
interface... but would it have to be exhaustive, i.e. for every public member of MyClass
I would have to declare a method in an IMyClass
interface in Java? Or can I just leave out the interfaces I don't care about?
Any sample code would be gratefully welcomed!
发布评论
评论(3)
您将无法从 Java 直接调用 C# 代码。 JNA 将只能访问本机库(C 或 C++)。但是,您可以在库中启用 COM Interop,并将两者与本机包装器链接在一起。即它看起来像:
Java --(JNA)--> C/C++ --(COM 互操作)--> C#
有几种替代方案:
You won't be able to call directly into your C# code from Java. JNA will only be able to access a native library (C or C++). However you could enable COM Interop in your library and link the 2 together with a native wrapper. I.e. it would look some thing like:
Java --(JNA)--> C/C++ --(COM Interop)--> C#
There are a couple of alternatives:
这个 Nugget 非常易于使用并且工作完美。 )
您需要 Visual Studio 2012(Express 可以工作 美好的)。
安装后,只需在要导出的任何静态函数之前添加
[RGiesecke.DllExport.DllExport]
即可。就是这样!示例:
C#
Java
在顶部添加导入:
在类中添加接口。它是您的 C# 函数名称,前面带有字母“I”:
在您的类中需要的地方调用您的 DLL:
This Nugget is super easy to use and works perfectly. https://www.nuget.org/packages/UnmanagedExports
You need Visual Studio 2012 (Express works fine).
Once installed, just add
[RGiesecke.DllExport.DllExport]
before any static function you want to export. That's it!Example:
C#
Java
Add the import at the top:
Add the interface in your class. Its your C# function name preceded by the letter "I":
Call your DLL where you need it in your class:
您可以使用咖啡因http://caffeine.berlios.de/site/documentation/
You can use Caffeine for this http://caffeine.berlios.de/site/documentation/