我想在C DLL中调用一个函数,该函数需要一个指向结构体的指针。这个结构(Data
)的定义是这样的:如何使用指向Java指针的指针填充结构?
struct BD
{
char* Data;
int Length;
};
struct EE
{
char* Key;
BD* Value;
};
struct Data
{
char* Name;
BD* Picture;
// A NULL-terminated array of pointers to EE structures.
EE** Elements;
};
在Java中我已经定义了一些类,像这样:
public static class BD extends Structure implements Structure.ByReference {
public byte[] Data;
public int Length;
}
public static class EE extends Structure implements Structure.ByReference {
public String Key;
public BD Value;
}
public static class Data extends Structure {
public String Name;
public BD Picture;
public PointerByReference Elements;
}
但现在我不知道究竟如何填充Data
对象正确。我想我可以计算出Name
和Picture
字段,但是我怎样将Elements
字段设置为?我可以创建一个EE
对象的Java数组,但是如何从中获取PointerByReference?也许我需要将Elements
声明为Pointer[]
,但是接下来我只需要为每个EE
对象填充数组的每个元素,即getPointer()
对象?虽然这看起来不太合适?
编辑:给的什么,我试图做一个更好的主意:
Data data = new Data();
// Fill in Name and Picture fields.
EE[] elements = new Elements[10];
// Fill in the elements array.
// Now how do I set the Elements field on the data object from the elements array?
data.Elements = ???
EDIT2:这是我如何与technomage的帮助下解决了这个问题:
我改变了我的Data
结构看起来像这样:
public static class Data extends Structure {
public String Name;
public BD Picture;
public Pointer Elements;
}
而我的BD
结构是这样的:
public static class BD extends Structure implements Structure.ByReference {
public Pointer Data;
public int Length;
}
将Java byte[]
转换为JNA Pointer
我不得不使用ByteBuffer
:
ByteBuffer buf = ByteBuffer.allocateDirect(bytes.length);
buf.put(bytes);
bd.Data = Natvie.getDirectBufferPointer(buf);
JNA不以struture喜欢ByteBuffer
小号不幸。
为了让我的元素的指针,我需要创建的Pointer
s到每个EE
对象的数组(参见technomage的答案PointerArray
实现):
EE e = new EE();
// Populate e object.
// ...
// Important: ensure that the contents of the objects are written out to native memory since JNA can't do this automatically
e.write();
ptrs.add(e);
// Once each object is setup we can simply take the array of pointers and use the PointerArray
data.Elements = new PointerArray(ptrs.toArray(new Pointer[0]));
我不能使用ByteBuffer
或PointerArray
直接在结构定义,所以我不得不依靠Pointer
。
当您另外传递指针变量的地址时,将使用'PointerByReference',以便为被调用者提供空间以“返回”一个值。 – technomage
如果您的本地'struct'使用'char []'作为相应的字段类型,则只能使用'byte []'作为'Structure'字段。 – technomage