8 messages in net.java.dev.jna.usersRe: [jna-users] Question on variable ...
FromSent OnAttachments
Jay WaltersMay 12, 2008 9:42 am 
Timothy WallMay 12, 2008 9:52 am 
Jay WaltersMay 12, 2008 11:05 am 
Jay WaltersMay 12, 2008 11:12 am 
Timothy WallMay 12, 2008 12:05 pm 
Timothy WallMay 12, 2008 12:10 pm 
Jay WaltersMay 12, 2008 12:39 pm 
Timothy WallMay 12, 2008 2:05 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Question on variable length structures in Win32Actions...
From:Timothy Wall (twal@dev.java.net)
Date:May 12, 2008 12:05:06 pm
List:net.java.dev.jna.users

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).