Search code examples
javapointersjnalocal-variables

JNA: How to pass a pointer to a local variable?


I have two local variables:

Pointer output;
int output_len; /* or better `size_t output_len;` */

I need to pass pointers to these variables into (the same named arguments of) a JNA function:

    public int libcomcom_run_command(
            byte[] input,
            size_t input_len,
            PointerByReference output,
            Pointer output_len,
            byte[] file,
            PointerByReference argv,
            PointerByReference envp,
            int timeout);

How?


Solution

  • new PointerByReference(pointerValue);
    new IntByReference(intValue);
    

    You can't actually "take the address" of a local Java variable. The XByReference types allocate memory for the value in question, and then refer to the address of that memory.

    After the invocation you can call XByReference.getValue() to see any changes to that memory.