Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
让JNA使用Java=>;C#?_C#_Java_Jna - Fatal编程技术网

让JNA使用Java=>;C#?

让JNA使用Java=>;C#?,c#,java,jna,C#,Java,Jna,我在C#库中编写了很多代码,现在需要从Java调用这些代码 我看到它被推荐在SO上使用,但我甚至在起步阶段都有困难;那里的文件很粗略 首先,它似乎只是向您展示如何连接到本机C库,这对我没有好处;我想连接到我自己的图书馆。那里的代码示例显示: // This is the standard, stable way of mapping, which supports extensive // customization and mapping of Java to native types. pu

我在C#库中编写了很多代码,现在需要从Java调用这些代码

我看到它被推荐在SO上使用,但我甚至在起步阶段都有困难;那里的文件很粗略

首先,它似乎只是向您展示如何连接到本机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
接口中声明一个方法?或者我可以忽略我不关心的接口吗


欢迎使用任何示例代码

您将无法从Java直接调用C#代码。JNA只能访问本机库(C或C++)。但是,您可以在库中启用COM互操作,并使用本机包装器将2链接在一起。也就是说,它看起来像:

Java--(JNA)-->C/C++--(COM互操作)-->C#

有两种选择:

  • 让C#成为一个独立的命令行应用程序,让Java代码使用stdin/stdout从中发送/接收数据(在这里可以帮助您)
  • 将C#作为一个独立的应用程序运行,并使用某种形式的平台无关消息传递协议,以在其中两个应用程序之间进行通信,例如HTTP、AMQP

    • 你可以用咖啡因来做这个

      这个金块非常容易使用,效果非常好

      您需要Visual Studio 2012(Express可以正常工作)。 安装后,只需在要导出的任何静态函数之前添加
      [rgisecke.DllExport.DllExport]
      。就这样

      示例:

      C#

      Java

      在顶部添加导入:

         import com.sun.jna.Native;
      
      在类中添加接口。您的C#函数名前面有字母“I”:

      在类中需要DLL的位置调用DLL:

      IYourFunction iYourFunction = (IYourFunction )Native.loadLibrary("full or relative path to DLL withouth the .dll extention", IYourFunction.class);//call JNA
              System.out.println("Returned: " + IYourFunction.YourFunction("some parameter"));
      

      JNA和C#不会混在一起。这听起来让人很痛苦track@mbx当前位置虽然如此,但Michael Barker在简洁地描述选项方面做得很好+气垫船-同意。这不是我想要的答案,但这是一个答案,我会接受的。实际上,虽然我可能不得不购买一些其他的解决方案,比如JNBridge。实际上,虽然与您的问题没有1:1的关系,但知道有开源的交叉编译器允许您编译C/C++“中间件”可能会有所帮助在Linux这样的平台上,在WIndows上运行C#stuff。当C#dll与dll有依赖关系时,此过程将失败。如果dll有一些外部引用,它将崩溃
        public interface IYourFunction extends com.sun.jna.Library
          {
             public int YourFunction(String tStr);
          };
      
      IYourFunction iYourFunction = (IYourFunction )Native.loadLibrary("full or relative path to DLL withouth the .dll extention", IYourFunction.class);//call JNA
              System.out.println("Returned: " + IYourFunction.YourFunction("some parameter"));