On May 12, 2008, at 2:06 PM, Jay Walters wrote:
Ok, so I'll ditch the by reference for this case. I'll also let the
variable length stuff allocated at the end stay put for the moment.
So if I have a native function on windows like
int foo(PX * x);
If this actually resolves to 'struct **x', then you need to pass in
something that holds a Pointer value; PointerByReference, Pointer[1],
or your own custom com.sun.jna.ptr.ByReference class (NOT
com.sun.jna.Structure.ByReference).
and a structure X defined as
typdef struct _X {int a; int b; } X;
typedef X * PX;
The function foo will allocate an X and put the ptr into the pointer
to which x refers (*x).
I can in Java create a class
class PX extends Structure {
public int a;
public int b;
}
define foo as
int foo(PX x);
and away I go? How does JNA know that x is returned by reference
and that it's allocated in the native code? How would I distinguish
from the case where I actually pass a ptr to a PX object, rather
than a ptr to a ptr to a PX object?
There is a bit of confusion here over JNA's re-use of the term
"ByReference". There are a group of classes called XXXByReference
(which you were probably intended to refer to in the first place),
which in general mean "pointer to XXX". This is subtly different than
the Structure.ByReference tagging interface, which means "when you
encounter this structure within another structure, use 'struct *'
rather than inlining".
The simplest way to get a structure address returned from a function
is via PointerByReference, which avoids potential confusion between
com.sun.jna.ptr.ByReference and com.sun.jna.Structure.ByReference.
Now also, if my structure PX has a nested structure
class P1 extends Structure {
public static class ByReference extends P1 implements
Structure.ByReference;
public int a1;
public int b1;
}
public class PX extends Structure
public P1.ByReference p1;
public int a;
public int b;
}
Does this also 'just work' or do I need to do something else?
The above will have the field "p1" automatically populated by JNA on
Structure.read (which happens automatically after a function call).