2012-03-11 169 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(),其中围绕本地函数调用必要的,但那之外,你可能需要这些调用显式依赖在你的使用上。