C# byteArray 到 Java

C# byteArray to Java

我在 C# 中有一段代码可以调用 dll 中的方法。现在我想在 Java.

中实现什么

C#

[DllImport("dll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int FindNumber([MarshalAs(UnmanagedType.AnsiBStr, SizeConst = 64)] string atr, out IntPtr number);

[DllImport("dll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int GetData(int number, int dataId, byte[] dataBuffer, ref uint dataBufferSize);

int res=0;
IntPtr number=IntPtr.Zero;
res = FindNumber(null, out number);
uint dataBufferSize = 1024;
byte[] dataBuffer = new byte[dataBufferSize];

res = GetData(number, 6, dataBuffer, ref dataBufferSize);

Console.WriteLine(Encoding.UTF8.GetString(dataBuffer, 0, (int)dataBufferSize));

Java

LongByReference number = new LongByReference(0);
int res=0;
res = NativeInterface.FindNumber(null, number); 
IntByReference dataBufferSize = new IntByReference(1024);
Pointer dataBuffer = new Memory(1024);                                                                  
res = NativeInterface.GetData(number.getValue(), 6, dataBuffer, dataBufferSize);                            
if (res == 0) {                         
    byte buffer[] = dataBuffer.getByteArray(0, dataBufferSize.getValue());
    String bufferContent = new String(buffer, StandardCharsets.UTF_8);
    System.out.println(bufferContent);                              
}

如果res为0表示一切正常。在 GetData() res 为 0 但 dataBufferSize 为 1024 之后,在 C# 中为 8 且 bufferContent=?;

如果我这样做

LongByReference number = new LongByReference(0);
int res=0;
res = NativeInterface.FindNumber(null, number); 
IntByReference dataBufferSize = new IntByReference(1024);
byte[] dataBuffer = new byte[dataBufferSize.getValue()];                                                                    
res = NativeInterface.GetData(number.getValue(), 6, dataBuffer, dataBufferSize);                            
if (res == 0) {     
    String bufferContent = new String(dataBuffer, StandardCharsets.UTF_8);
    System.out.println(bufferContent);                              
}

res为0,但bufferContent="",dataBufferSize为1024,dataBuffer为0

现在可以了,我没有在我的项目中包含 dll.lic

    Pointer number;
    PointerByReference ref=new PointerByreference();
    int res=0;
    res = NativeInterface.FindNumber(null, ref); 
    number=ptr.getValue();
    IntByReference dataBufferSize = new IntByReference(1024);
    Pointer dataBuffer = new Memory(1024);                                                                  
    res = NativeInterface.GetData(number, 6, dataBuffer, dataBufferSize);                            
    if (res == 0) {                         
        byte buffer[] = dataBuffer.getByteArray(0, dataBufferSize.getValue());
        String bufferContent = new String(buffer, StandardCharsets.UTF_8);
        System.out.println(bufferContent);                              
    }