是否可以在JNA Mapping中使用二维数组?

时间:2015-06-15 14:03:43

标签: jna

当我尝试在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表示当时没有实施。任何人都可以确认现在是否仍然如此?

1 个答案:

答案 0 :(得分:1)

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.