JNA:无法创建结构子类的对象-无效的结构字段 [英] JNA: Can't create object of structure subclass - Invalid structure field

查看:93
本文介绍了JNA:无法创建结构子类的对象-无效的结构字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将C typedef映射到JNA Struct.下面是C typedef:

I am mapping a C typedef to a JNA Struct. Below if the C typedef:

typedef struct _GLYCOUNTERS {
unsigned int ulArraySize;
LPGLYCOUNTER lpCounters;
}GLYCOUNTERS, *LPGLYCOUNTERS;

这是我的JNA Structure子类:

And this is my JNA Structure subclass:

public class GlyCounters extends Structure{
    public class ByReference extends GlyCounters implements Structure.ByReference {};
    public class ByValue extends GlyCounters implements Structure.ByValue {};
    public int ulArraySize;
    public GlyCounter lpCounters;

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList(new String[] { "ulArraySize", "lpCounters"});
}
    }

我正在尝试使用代码创建和反对GlyCounters类:

I am trying to create and object of GlyCounters class with code:

lppCounters = glyTypes.new GlyCounters();

如您所见,我正在另一个类中创建对象,而GlyCounters是另一个类的嵌套类.

As you can see I am creating the object in another class and GlyCounters is a nested class of yet another class.

这是我收到的错误消息:

This is the error message I receive:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid Structure field in class btc_dll.GloryTypeDef$GlyCounters, field name 'lpCounters' (class btc_dll.GloryTypeDef$GlyCounter): Can't instantiate class btc_dll.GloryTypeDef$GlyCounter (java.lang.InstantiationException: btc_dll.GloryTypeDef$GlyCounter)
at com.sun.jna.Structure.validateField(Structure.java:1038)
at com.sun.jna.Structure.validateFields(Structure.java:1048)
at com.sun.jna.Structure.<init>(Structure.java:179)
at com.sun.jna.Structure.<init>(Structure.java:172)
at com.sun.jna.Structure.<init>(Structure.java:159)
at com.sun.jna.Structure.<init>(Structure.java:151)
at btc_dll.GloryTypeDef$GlyCounters.<init>(GloryTypeDef.java:298)
at btc_gui.Btc_gui.main(Btc_gui.java:54)
Java Result: 1

引起问题的字段是另一个c typedef结构的另一个JNA Structure类.搜索错误消息后,我尝试将no-args构造函数添加到这两个类中,但错误仍然存​​在.

The field causing the problem is yet another JNA Structure class for another c typedef struct. After googling the error message, I have tried adding no-args constructors to both of these classes but the error still remains.

有人有类似的问题吗?

推荐答案

当您要在结构中使用类型为struct *的字段时,需要使用带有Structure.ByReference接口标记的Structure.

When you want a field of type struct * within your structure, you need to use a Structure tagged with the interface Structure.ByReference.

已编辑

最常用的方法:

public class GlyCounter extends Structure {
    public class ByReference extends GlyCounter implements Structure.ByReference {};
    // More definition here...
}
public class GlyCounters extends Structure{
    public int ulArraySize;
    public GlyCounter.ByReference lpCounters;

    public GlyCounters(Pointer p) {
        super(p);
        read();
    }

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList(new String[] { "ulArraySize", "lpCounters"});
    }
}

// To obtain the full array (assuming they're allocated as a block)
GlyCounters gc = new GlyCounters(pointer);
GlyCounter[] counters = gc.lpCounters.toArray(gc.ulArraySize());

字段lpCounters现在是指针类型,而不是内联的(即,共享封闭的struct的内存).

The field lpCounters is now of pointer type instead of inlined (i.e. sharing the memory of the enclosing struct).

这篇关于JNA:无法创建结构子类的对象-无效的结构字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆