Search code examples
javanativejna

Why is structure size always returning -1?


I have created the following structure mapping it into a C structure.But when I print the size of the structure using CALCULATESIZE() it is always returning -1. Why is that?

Below is my java structure.

public class ipj_action extends Structure {

public long ipj_action_value;
Pointer p;

public ipj_action() {
    setAlignType(Structure.ALIGN_NONE);
    System.out.println("Structure size is  : "+CALCULATE_SIZE);
}

@Override
protected List getFieldOrder() {
    return Arrays.asList("ipj_action_value");

}

}

Below is the main class where I call it.

public class RFIDMain {

    public  rfidlib rlib;
    public  ipj_iri_device ipj_iri_device;
    public  ipj_action ipj_action;
    public  ipj_error errorStatus;

    public static void main(String[] args) {

    RFIDMain r = new RFIDMain();


    r.rlib = (rfidlib) Native.loadLibrary("rfidlib", rfidlib.class);
    r.ipj_iri_device = new ipj_iri_device();
    r.ipj_action = new ipj_action();
    r.errorStatus = new ipj_error();
    r.ipj_action.ipj_action_value = 0x1;

    r.errorStatus = r.rlib.start(r.ipj_iri_device, r.ipj_action);

    System.out.println(r.errorStatus);

}

Solution

  • You want Structure.size().

    That method will fail if JNA cannot determine the structure size, which may happen if you've neglected to initialize any array fields or used a type which JNA cannot convert into a native equivalent.