[英]Return byte array from C# to Java using UnmanagedExports and JNA
I recently found the library UnmanagedExports that allowed me to access C#-methods directly from Java using JNA . 最近,我发现了UnmanagedExports库,该库使我可以使用JNA从Java直接访问C#方法。
Has anyone an idea about what is wrong with my attempt to return a byte array from C# to Java? 有谁知道我尝试将字节数组从C#返回Java的问题是什么?
Here is my example: 这是我的示例:
C# code: C#代码:
using System;
using RGiesecke.DllExport;
namespace JnaTestLibrary
{
public class JnaTest
{
[DllExport]
public static byte[] returnT1()
{
byte[] t1 = {1,2,3,4,5};
return t1;
}
}
}
Java code: Java代码:
package me.mt.test;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
public class JnaTest {
public interface JnaTestInterface extends Library{
byte[] returnT1();
}
static JnaTestInterface jnaTest = null;
static{
if(Platform.is64Bit()){
jnaTest = (JnaTestInterface)Native.loadLibrary("JnaTestLibrary64", JnaTestInterface.class);
}
else{
jnaTest = (JnaTestInterface)Native.loadLibrary("JnaTestLibrary86", JnaTestInterface.class);
}
}
public byte[] returnT1(){
return jnaTest.returnT1();
}
}
Java exception: Java异常:
Exception in thread "main" java.lang.IllegalArgumentException: Unsupported return type class [I in function returnT1
I solved the problem by using pointers. 我通过使用指针解决了问题。
C# code: C#代码:
using System;
using RGiesecke.DllExport;
namespace JnaTestLibrary
{
public class JnaTest
{
[DllExport]
public unsafe static byte* returnT1()
{
byte[] t1 = {1,2,3,4,5};
fixed (byte* p1 = t1)
{
return p1;
}
}
}
}
Java code: Java代码:
package me.mt.test;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
public class JnaTest {
public interface JnaTestInterface extends Library{
Pointer returnT1();
}
static JnaTestInterface jnaTest = null;
static{
if(Platform.is64Bit()){
jnaTest = (JnaTestInterface)Native.loadLibrary("JnaTestLibrary64", JnaTestInterface.class);
}
else{
jnaTest = (JnaTestInterface)Native.loadLibrary("JnaTestLibrary86", JnaTestInterface.class);
}
}
public byte[] returnT1(){
Pointer p1 = jnaTest.returnT1();
return p1.getByteArray(0, 5);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.