首页 新闻 会员 周边 捐助

JNA中自定义结构体如何传参?

0
悬赏园豆:50 [已关闭问题] 关闭于 2021-02-08 09:39

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,请问如何传递该结构体?

JNA
张晓风的主页 张晓风 | 菜鸟二级 | 园豆:204
提问于:2021-02-06 16:06
< >
分享
所有回答(1)
0

同事给解决了

1.首先,Java对应的结构体需要修改:

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 {}
}

2.将DetectionZone[]转为Pointer

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;
    }
张晓风 | 园豆:204 (菜鸟二级) | 2021-02-08 09:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册