Is it possible to use two dimensional arrays in JNA Mapping?

306 Views Asked by At

I am facing below issue/error when I try to map a two dimensional array in JNA Mapping

        Public Class TestMultiDimensionArray extends Structure{
          public String testArray[][];
          public interface CTrainLibrary extends Library {
                void processArray(testMultiDimensionalArray);
            }
        }

    in main..


        TestMultiDimensionalArray test = new testMultiDimensionalArray();
//Initialaize array
         test.testArray=  {{"ABC","EFG"},
                            {"TEST1","TEST2"}
                           };
         //....some code here
         CLibrary lib = (CTrainLibrary)Native.loadLibrary("c", CLibrary.class);
         lib.processArray(testMultiDimensionalArray);

Error message
    Exception in thread "main" java.lang.NullPointerException
        at java.lang.reflect.Array.getLength(Native Method)
        at com.sun.jna.Native.getNativeSize(Native.java:1066)
        at com.sun.jna.Structure.getNativeAlignment(Structure.java:1283)
        at com.sun.jna.Structure.getNativeAlignment(Structure.java:1308)
        at com.sun.jna.Structure.deriveLayout(Structure.java:1150)
        at com.sun.jna.Structure.calculateSize(Structure.java:982)
        at com.sun.jna.Structure.allocateMemory(Structure.java:363)
        at com.sun.jna.Structure.ensureAllocated(Structure.java:339)
        at com.sun.jna.Structure.ensureAllocated(Structure.java:329)
        at com.sun.jna.Structure.write(Structure.java:701)
        at com.sun.jna.Structure.autoWrite(Structure.java:1923)
        at com.sun.jna.Function.convertArgument(Function.java:505)
        at com.sun.jna.Function.invoke(Function.java:297)
        at com.sun.jna.Library$Handler.invoke(Library.java:212) 

http://osdir.com/ml/java.jna.user/2008-05/msg00075.html states it is not implemented by then. Can anyone confirm whether the same is the case still now?

1

There are 1 best solutions below

0
On

What you use depends on the nature of your native multidimensional array.

If your native array looks like either of these:

// two definitions of a contiguous block of ROWS*COLS ints
int array[ROWS][COLS]; // row-major
int* parray = malloc(ROWS * COLS); // may be row-major or col-major depending on implementation

Then you can use Memory, int[], or an NIO Buffer of size ROWS*COLS, and you just need to ensure that you iterate the single block of memory appropriately.