1、先给出C的头文件里的结构体:
typedef struct DetectionZone
{
Zone zone;
DetectionZoneParam param;
} DetectionZone;
typedef struct DetectionZoneList
{
DetectionZone* zoneArray;
int zoneNum;
} DetectionZoneList;
2、我用JNAAeratorStudio工具生成了Java对应的结构体,如下:
public class DetectionZoneList extends Structure<DetectionZoneList, DetectionZoneList.ByValue, DetectionZoneList.ByReference > {
/** C type : DetectionZone* */
public DetectionZone.ByReference zoneArray;
public int zoneNum;
public DetectionZoneList() {
super();
}
protected List<? > getFieldOrder() {
return Arrays.asList("zoneArray", "zoneNum");
}
/** @param zoneArray C type : DetectionZone* */
public DetectionZoneList(DetectionZone.ByReference zoneArray, int zoneNum) {
super();
this.zoneArray = zoneArray;
this.zoneNum = zoneNum;
}
public DetectionZoneList(Pointer peer) {
super(peer);
}
protected ByReference newByReference() { return new ByReference(); }
protected ByValue newByValue() { return new ByValue(); }
protected DetectionZoneList newInstance() { return new DetectionZoneList(); }
public static DetectionZoneList[] newArray(int arrayLength) {
return Structure.newArray(DetectionZoneList.class, arrayLength);
}
public static class ByReference extends DetectionZoneList implements Structure.ByReference {
};
public static class ByValue extends DetectionZoneList implements Structure.ByValue {
};
};
3、下面是我的调用:
DetectionZoneList zoneProp = new DetectionZoneList();
zoneProp.zoneNum = 1;
zoneProp.zoneArray = new DetectionZone.ByReference();
4、问题:
这里我的zoneNum是1,调用没有问题,我的需求是传多个zone,zoneNum会大于1,请问如何传递该结构体?
同事给解决了
public DetectionZone.ByReference zoneArray;
需要修改为
public Pointer zoneArray;
完整的结构体如下所示:
public class DetectionZoneList extends Structure<DetectionZoneList, DetectionZoneList.ByValue, DetectionZoneList.ByReference> {
/**
* C type : DetectionZone*
*/
public Pointer zoneArray;
public int zoneNum;
public DetectionZoneList() {
super();
}
protected List<?> getFieldOrder() {
return Arrays.asList("zoneArray", "zoneNum");
}
public static class ByReference extends DetectionZoneList implements Structure.ByReference {}
public static class ByValue extends DetectionZoneList implements Structure.ByValue {}
}
public Pointer Transform(DetectionZone[] zoneArray) {
Pointer p = new Memory(zoneArray.length * zoneArray[0].size());
int s = 0;
for (int i = 0; i < zoneArray.length; i++) {
byte[] byteArray = zoneArray[i].byReference().getPointer().getByteArray(0, zoneArray[i].size());
for (int j = 0; j < zoneArray[0].size(); j++) {
p.setMemory(s++, 1, byteArray[j]);
}
}
return p;
}