Mapping struct with arrays

104 views
Skip to first unread message

Mausi

unread,
Feb 26, 2012, 10:38:40 AM2/26/12
to Java Native Access
Hi guys,

i´m a new "jna-user" an need some help.

I have to use a dll where some structs are defined like

struct{
variable int[5][4]
}

for example one original definition:


struct ddTableResults {
int resTable[5][4];
};


when trying the following code

public static class ddTableResults extends Structure {
public int[][] resTable;
public ddTableResults() {
resTable=new int[5][4];
allocateMemory();
}
}

i get an NullPointerException when creating an new ddTableResults -
class:

java.lang.NullPointerException
at java.lang.reflect.Array.getLength(Native Method)
at com.sun.jna.Native.getNativeSize(Native.java:940)
at com.sun.jna.Structure.getNativeAlignment(Structure.java:912)
at com.sun.jna.Structure.getNativeAlignment(Structure.java:937)
at com.sun.jna.Structure.calculateSize(Structure.java:840)
at com.sun.jna.Structure.allocateMemory(Structure.java:279)
...

what am i missing?

Mausi

Timothy Wall

unread,
Feb 26, 2012, 11:01:39 AM2/26/12
to jna-...@googlegroups.com
It will help if you understand the layout of the structure in memory.

struct { int[5][4] field; } in C looks like this:

0x00: X X X X // field[0]
0x10: X X X X // field[1]
0x20: X X X X // field[2]
0x30: X X X X // field[3]
0x40: X X X X // field[4]

This field most directly maps in Java to "int[]" (initialized with "new int[20]"), and calculate your own offsets. You could also use a Pointer initialized with Memory of the appropriate size.

You *could* define a type converter to translate a Java multi-dimensional array into a native multi-dimensional array, but it's probably easier to just use a single-dimensional array in Java and calculate the appropriate offsets.

Reply all
Reply to author
Forward
0 new messages