Search code examples
javac++pointersstructjna

typedef struct pointer into JNA


I am working on JNA for my application and I am confused about using typedef struct pointer in java. Please check my code and guide me. Thank you in advance!

Below is my c++ code

typedef struct tagHWDeviceInfo
{
USHORT          VendorID;      // vendor id
USHORT          ProductID;     // product id
DWORD           nXExt;         // firmware width
DWORD           nYExt;         // firmware height
DWORD           preesure;      // pressure level
DWORD           penState;      // pen state
}HWDeviceInfo, *PHWDeviceInfo;

and java code

public class HWDeviceInfo extends Structure{
    short VendorID;
    short ProductID;
    int   nXExt;        // firmware width
    int   nYExt;        // firmware height
    int   preesure;     // pressure level
    int   penState;
}

Now my question is: what does it mean by *PHWDeviceInfo in c++ code and how can I use this pointer in my java code?


Solution

  • JNA automatically converts instances of Structure to struct * in function calls, and to struct in structure field definitions. Structure.getPointer() gives you the pointer that JNA will use.

    You can modify this default behavior by tagging your class and/or parameter types with the Structure.ByReference and Structure.ByValue interfaces.

    JNA documents this clearly as noted by @areeba-irfan-ullah-khan.