jna 构造函数,JNA:无法创建结构子类的对象-无效的结构字段

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;

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 getFieldOrder() {

return Arrays.asList(new String[] { "ulArraySize", "lpCounters"});

}

}

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

lppCounters = glyTypes.new 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.(Structure.java:179)

at com.sun.jna.Structure.(Structure.java:172)

at com.sun.jna.Structure.(Structure.java:159)

at com.sun.jna.Structure.(Structure.java:151)

at btc_dll.GloryTypeDef$GlyCounters.(GloryTypeDef.java:298)

at btc_gui.Btc_gui.main(Btc_gui.java:54)

Java Result: 1

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.

Has anyone had similar problems?

解决方案

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

EDITED

The most common way to do this:

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 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());

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值