2021年3月22日星期一

JNA: truncated double's from Pointers in Linux(in Windows all is OK)

I use this solution to call the JNA method from .dll/.so library: Creating C++ Structures via JNA

And in Windows, this code works perfect, but in Linux, I receive truncated double values, so from the Pointer data in Windows, I receive this value:

40.7  

from these bytes in Pointer:

[64, 68, 89, -103, -103, -103, -103, -102]  

But in Linux I have this instead:

40.0  [64, 68, 0, 0, 0, 0, 0, 0]  

.so/.dll was compiled from the same source and from the Python(by using "ctypes") I obtain correct values in Linux from the same .so

I already tried to write/read byte[] to/from Pointer - nothing happened - in Windows, all OK, but in the Linux, doubles are truncated.

This is C++ Structure and Method, which it returns inside of .dll/.so:

struct emxArray_real_T  {    double *data;    int *size;    int allocatedSize;    int numDimensions;    boolean_T canFreeData;  };    emxArray_real_T *emxCreate_real_T(int rows, int cols)  {    emxArray_real_T *emx;    ***    return emx;  }  

And this code in Java:

@Structure.FieldOrder({"data", "size", "allocatedSize", "numDimensions", "canFreeData"})  public class emxArray_real_T extends Structure {        public Pointer data;      public Pointer size;      public int allocatedSize = 1;      public int numDimensions = 1;      public boolean canFreeData = false;        ***        public double[] getData() {          if (data == null) {              return new double[0];          }          return data.getDoubleArray(0, allocatedSize);      }        public void setData(double[] data) {          if (data.length != allocatedSize) {              throw new IllegalArgumentException("Data must have a length of " + allocatedSize + " but was "                      + data.length);          }          this.data.write(0, data, 0, data.length);      }      ***  }      

UPD: Obtaining bytes from Pointer data:

public byte[] getDataByte() {      final int times = Double.SIZE / Byte.SIZE;      if (data == null) {          return new byte[0];      }      return data.getByteArray(0, allocatedSize * times);  }  

UPD2: I receive this error when I try to write the double like this -0.0000847 in the Pointer data in Linux(so it seems in Linux .so somehow use C type 'int' to the double representation):

Expected a value representable in the C type 'int'. Found inf instead. Error in ballshaftpasses (line 61)

https://stackoverflow.com/questions/66748266/jna-truncated-doubles-from-pointers-in-linuxin-windows-all-is-ok March 22, 2021 at 10:39PM

没有评论:

发表评论