本文介绍了JNA获得与Java = GT工作; C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在C#库,我现在需要从Java调用了大量的代码。

I've written a lot of code in a C# library, which I now need to call from Java.

我看到了它建议对SO使用 JNA ,但我有甚至走出起跑器的麻烦;该文件有非常粗略

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.

首先,它只会出现向您展示如何连接到本机C库,它没有对我好;我想连接到我自己的库。代码示例有显示:

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);
}



我想连接到我的图书馆(MyLibrary.dll),并调用在静态方法 MyNamespace.MyClass 的C#签名是:

public static string HelloWorld(string p)

那么,什么参数要我给本地。调用LoadLibrary()

So what parameters to I give to Native.loadLibrary()?

这还只是Hello World的。如果我想要什么返回一个对象?比方说, MyClass的也有一个静态方法

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()

我怎么会叫,使用JNA?我想我必须定义在Java中相匹配的C# MyClass的接口的接口...但它必须是详尽的,即对于<$ C的每个公共成员$ C> MyClass的我必须申报Java中的 IMyClass 接口的方法是什么?或可我只是离开了我不在乎接口?

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!

推荐答案

您将无法直接调用从Java C#代码。 JNA将只能够访问一个本地库(C或C ++)。然而,你可以使COM互操作在您的图书馆,并与本地包装连接在一起的2。即它看起来像一些事情:

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:

爪哇 - (JNA) - GT; C / C ++ - (COM互操作) - GT; C#

有几个选择:


  • 请在C#一个独立的命令行应用程序,并具备Java代码发送/使用标准输入/输出(的可以帮助你在这里)。

  • 运行C#作为一个独立的应用某种形式的平台中立的消息协议之间的其中2,如沟通HTTP,AMQP。

  • Make the C# a stand alone command line app and have the Java code send/receive data from it using stdin/stdout (ProcessBuilder can help you here).
  • Run the C# as a stand alone app with some form of platform neutral messaging protocol to communicate between the 2 of them, e.g. HTTP, AMQP.

这篇关于JNA获得与Java = GT工作; C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 02:45