Java Native Access and arrays
I plan to write an interface for a spectrometer in JAVA. The manufacturer provides a c-dll to access the spectrometer. Therefore I have written a JNA-wrapper.
I could connect to the spectrometer and access simple methods of the dll. But I have problems to write appropriate functions, if the c-functions accept pointer-to-type arguments to return arrays.
For example:
c-header to access calibration data, from rcu1a.h:
Code :
USHORT WINAPI RC_ReadCalibrationValue(
HANDLE DeviceHandle,
double *DataArray
);
according function in rcu1a.java:
Code :
short RC_ReadCalibrationValue(Pointer DeviceHandle, DoubleByReference DataArray);
and the code snipplet from Spectrometer.java, the class to access the spectrometer data:
Code :
private rcu1a lib = (rcu1a) Native.loadLibrary("rcu1a", rcu1a.class);
private DoubleByReference calibData = new DoubleByReference();
//...
public double [] calibrationData()
{
lib.RC_ReadCalibrationValue(DeviceHandle, calibData);
return calibData.getPointer().getDoubleArray(0,6);
}
The function should return an array with 6 calibration parameters. Like this I get and array out of bounds exception:
Quote:
java.lang.IndexOutOfBoundsException: Bounds exceeds available space : size=8, offset=48
if I change to calibData.getValue(); I get the value that should be in the first field of the array.
Am I doing something substantially wrong in handling arrays?
Thx Richard