JNA Struct and Pointer mapping

llang picture llang · Jan 30, 2009 · Viewed 10.9k times · Source

How does one map the function below to java?

VOID WriteToStruct(BOOL *Status, STRUCT_MSG RecBuff)

What this function does:
1) Populates the struct RecBuff
2) Updates status

How do I map to a boolean pointer in Java and access the struct data updated by the function?

Answer

Brian picture Brian · Feb 11, 2009

I was searching for another issue concerning JNA and structs, and Google redirected me here. I hope this helps.

From JNA API

To pass a structure by value, first define the structure, then define an empty class from that which implements Structure.ByValue. Use the ByValue class as the argument or return type.

// Original C code
typedef struct _Point {
  int x, y;
} Point;

Point translate(Point pt, int dx, int dy);

// Equivalent JNA mapping
class Point extends Structure {
    public static class ByValue extends Point implements Structure.ByValue { }
    public int x, y;
}
Point.ByValue translate(Point.ByValue pt, int x, int y);
...
Point.ByValue pt = new Point.ByValue();
Point result = translate(pt, 100, 100);