pointersstructpass-by-referencejnajnaerator

JNA: Pointer to a pointer to a struct


I have a function call:

long foo(mystruct **list)

where the definition of mystruct is

typedef struct {
    otherstruct *bar[64];
} mystruct;

and I'm trying to get at the (JNA) Structure[] corresponding to bar. My current function definition is int foo(PointerByReference list); since that's what a pointer to a pointer is, but I can't figure out how to get the Structure[].

In C, the code is used as follows:

mystruct *list;
foo(&list);
for (i=0; i<64; i++)
    doStuff(list->bar[i]);

Solution

    1. PointerByReference is appropriate; use getValue() to get the Pointer value.
    2. With that value, initialize your "mystruct" structure
    3. Since "otherstruct *bar[64]" represents an array of struct*, you need to explicitly use a field of type Structure.ByReference[] to force JNA to treat the array as pointers instead of inlined structs.

    code:

    class OtherStruct extends Structure {
        class ByReference extends OtherStruct implements Structure.ByReference { }
        ...
    }
    class MyStructure extends Structure {
        public OtherStruct.ByReference[] bar = new OtherStruct.ByReference[64];
    }
    

    JNA should implicitly call Structure.read() and Structure.write() where necessary around native function calls, but outside of that you may need to make those calls explicitly depending on your usage.