JNA Structure作为另一个Structure字段

时间:2016-01-29 13:14:16

标签: exception structure native jna init

我想使用 SYSTEMTIME 结构作为 WFSRESULT 结构的字段。但是当我尝试基于Pointer初始化WFSRESULT的新对象时,我得到了异常。有我的结构:

public class WFSRESULT extends Structure {

    public WFSRESULT() {

    }

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

    public int RequestID;
    public short hService;
    public SYSTEMTIME tsTimestamp;
    public int hResult;
    public int dwCommandCode;
    public int dwEventID;
    public Pointer lpBuffer;

    @Override
    protected List getFieldOrder() {
        return Arrays.asList(new String[]{"RequestID", "hService", "tsTimestamp", "hResult", "dwCommandCode", "dwEventID", "lpBuffer"});
    }
}

    public class SYSTEMTIME extends Structure {

    public SYSTEMTIME() {
    }

    public short wYear;
    public short wMonth;
    public short wDayOfWeek;
    public short wDay;
    public short wHour;
    public short wMinute;
    public short wSecond;
    public short wMilliseconds;

    @Override
    protected List getFieldOrder() {
        return Arrays.asList(new String[]{"wYear", "wMonth", "wDayOfWeek", "wDay", "wHour", "wMinute", "wSecond", "wMilliseconds"});
    }
}

初​​始化:

wfsResult = new WFS().new WFSRESULT(ipResult);

异常(在超级(p)行中):

Exception in thread "main" java.lang.IllegalArgumentException: Invalid Structure field in class g60.wfs.WFS$WFSRESULT, field name 'tsTimestamp' (class g60.wfs.WFS$SYSTEMTIME): Can't instantiate class g60.wfs.WFS$SYSTEMTIME
    at com.sun.jna.Structure.validateField(Structure.java:1109)
    at com.sun.jna.Structure.validateFields(Structure.java:1119)
    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:168)
    at g60.wfs.WFS$WFSRESULT.<init>(WFS.java:44)
    at g60.g60Handling.logMessage(g60Handling.java:132)
    at g60.g60Handling.runInitializationRoutine(g60Handling.java:117)
    at g60.g60Handling.<init>(g60Handling.java:56)
    at g60.g60Handling.main(g60Handling.java:40)
Caused by: java.lang.IllegalArgumentException: Can't instantiate class g60.wfs.WFS$SYSTEMTIME
    at com.sun.jna.Structure.newInstance(Structure.java:1781)
    at com.sun.jna.Structure.newInstance(Structure.java:1759)
    at com.sun.jna.Structure.size(Structure.java:1030)
    at com.sun.jna.Native.getNativeSize(Native.java:1172)
    at com.sun.jna.Structure.getNativeSize(Structure.java:2072)
    at com.sun.jna.Structure.getNativeSize(Structure.java:2062)
    at com.sun.jna.Structure.validateField(Structure.java:1105)
    ... 9 more
Caused by: java.lang.InstantiationException: g60.wfs.WFS$SYSTEMTIME
    at java.lang.Class.newInstance(Class.java:427)
    at com.sun.jna.Structure.newInstance(Structure.java:1773)
    ... 15 more
Caused by: java.lang.NoSuchMethodException: g60.wfs.WFS$SYSTEMTIME.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 16 more

1 个答案:

答案 0 :(得分:0)

您需要将SYSTEMTIME设为静态,或在WFSRESULT之外定义它。否则它总是隐含引用周围的WFSRESULT类,JNA无法自动生成。