【发布时间】:2014-06-11 15:59:42
【问题描述】:
我有 C 代码,我正在尝试使用 JNA 将其包装到 Java 中的函数调用中。在这段代码中,我声明了以下结构,其中 struct B 是 aFunction(我在 Java 库中包装的函数)的返回值。
CORE_EXPORT typedef struct{
int number;
char* word;
} A;
CORE_EXPORT typedef struct{
int numElements;
A** Astructures;
} B;
CORE_EXPORT B* aFunction(int num);
以下是我对该函数的 Java 包装器:
public class MyLibrary extends Library{
B aFunction(int num);
public static class A extends Structure implements Structure.ByReference {
public int number;
public String word;
public A(Pointer p){
super(p);
read();
}
@Override
protected List getFieldOrder() {
return Arrays.asList("number","word");
}
}
public static class B extends Structure implements Structure.ByValue {
public PointerByReference Astructures;
public int numElements;
public B(Pointer p){
super(p);
read();
}
@Override
protected List getFieldOrder() {
return Arrays.asList("numElements","Astructures");
}
}
}
根据我对 JNA 的理解,我尝试通过以下方式将 aFunction 返回的结构中的 PointerByReference 取消引用到 A 结构的数组:
MyLibrary.B structB = aFunction(1);
PointerByReference ptrRef = structB.Astructures;
Pointer[] pointersToStructs = ptrRef.getValue().getPointerArray(0);
//use each of these pointers to create each A struct
MyLibrary.A[] aStructures = new MyLibrary.A[pointersToStructs.length];
for(int i = 0; i < pointersToStructs.length; i++){
aStructures[i] = new MyLibrary.A(pointersToStructs[i]);
}
调用该函数可以正常工作,但是当我尝试从 aFunction 返回的结构 B 中提取 A 结构数组时,我收到一条错误消息,指出“A 返回的名称 ([number,word]) 与声明的字段名称 ([])"。当我在 Java 中取消引用 PointerByReference 时,我做错了什么吗?
谢谢!
更新:
经过更多研究,我将解压结构数组的方法更改为:
MyLibrary.B structB = aFunction(1);
Pointer resultPtr = structB.Astructures.getValue();
MyLibrary.A tempStruct = new MyLibrary.A(resultPtr);
tempStruct.read();
MyLibrary.A[] aStructures = (MyLibrary.A[])tempStruct.toArray(structB.numElements);
当执行第二行 (structB.Astructures.getValue()) 时,我现在收到 Java 运行时环境 (SIGSEGV) 检测到的致命错误。这说明有问题的框架是C [libc.so.6+0x89a00] memcpy+0xa0。
【问题讨论】:
标签: jna