JNA array structure
- by Burny
I want to use a dll (IEC driver) in Java, for that I am using JNA. The problem in pseudo code:
start the server
allocate new memory for an array (JNA)
client connect
writing values from an array to the memory
sending this array to the client
client disconnect
new client connect
allocate new memory for an array (JNA) - JVM crash (EXCEPTION_ACCESS_VIOLATION)
The JVM crash not by primitve data types and if the values will not writing from the array to the memory.
the code in c:
struct DataAttributeData CrvPtsArrayDAData = {0};
CrvPtsArrayDAData.ucType = DATATYPE_ARRAY;
CrvPtsArrayDAData.pvData = XYValDAData;
XYValDAData[0].ucType = FLOAT;
XYValDAData[0].uiBitLength = sizeof(Float32)*8;
XYValDAData[0].pvData = &(inUpdateValue.xVal);
XYValDAData[1].ucType = FLOAT;
XYValDAData[1].uiBitLength = sizeof(Float32)*8;
XYValDAData[1].pvData = &(inUpdateValue.yVal);
Send(&CrvPtsArrayDAData, 1);
the code in Java:
DataAttributeData[] data_array = (DataAttributeData[]) new DataAttributeData()
.toArray(d.bitLength);
for (DataAttributeData d_temp : data_array) {
d_temp.data = new Memory(size / 8);
d_temp.type = type_iec;
d_temp.bitLength = size;
d_temp.write();
}
d.data = data_array[0].getPointer();
And then writing values whith this code:
for (int i = 0; i < arraySize; i++) {
DataAttributeData dataAttr = new DataAttributeData(d.data.share(i * d.size()));
dataAttr.read();
dataAttr.data.setFloat(0, f[i]);
dataAttr.write();
}
the struct in c:
struct DataAttributeData{
unsigned char ucType;
int iArrayIndex;
unsigned int uiBitLength;
void * pvData;};
the struct in java:
public static class DataAttributeData extends Structure {
public DataAttributeData(Pointer p) {
// TODO Auto-generated constructor stub
super(p);
}
public DataAttributeData() {
// TODO Auto-generated constructor stub
super();
}
public byte type;
public int iArrayIndex;
public int bitLength;
public Pointer data;
@Override
protected List<String> getFieldOrder() {
// TODO Auto-generated method stub
return Arrays.asList(new String[] { "type", "iArrayIndex",
"bitLength", "data" });
}
}
Can anybody help me?