2012-03-11 166 views
1

我有一個函數調用:JNA:指針的指針到一個struct

long foo(mystruct **list)

其中mystruct定義是

typedef struct { 
    otherstruct *bar[64]; 
} mystruct; 

,我試圖讓在( JNA)Structure []對應的吧。我當前的函數定義是int foo(PointerByReference list);,因爲這是指向指針的指針,但我無法弄清楚如何獲取Structure []。

在C中,代碼的使用如下:

mystruct *list; 
foo(&list); 
for (i=0; i<64; i++) 
    doStuff(list->bar[i]); 

回答

2
  1. PointerByReference爲宜;使用getValue()可獲得Pointer 的值。
  2. 與該值,初始化「MYSTRUCT」結構
  3. 由於「otherstruct *bar[64]」代表的struct*一個數組,你需要明確使用Structure.ByReference[]類型的字段,迫使JNA處理數組的指針,而不是內聯結構。

代碼:

class OtherStruct extends Structure { 
    class ByReference extends OtherStruct implements Structure.ByReference { } 
    ... 
} 
class MyStructure extends Structure { 
    public OtherStruct.ByReference[] bar = new OtherStruct.ByReference[64]; 
} 

JNA應該隱含調用Structure.read()和Structure.write(),其中圍繞本地函數調用必要的,但那之外,你可能需要這些調用顯式依賴在你的使用上。