17 messages in net.java.dev.jna.usersRe: [jna-users] Array of Structures i...
FromSent OnAttachments
RWAD...@UP.COMJul 18, 2008 8:39 am 
Timothy WallJul 18, 2008 9:26 am 
RWAD...@UP.COMJul 18, 2008 9:46 am 
Timothy WallJul 18, 2008 10:18 am 
RWAD...@UP.COMJul 18, 2008 11:07 am 
RWAD...@UP.COMJul 18, 2008 11:19 am 
Timothy WallJul 18, 2008 11:35 am 
Timothy WallJul 18, 2008 11:40 am 
Timothy WallJul 18, 2008 11:44 am 
Timothy WallJul 18, 2008 11:46 am 
RWAD...@UP.COMJul 18, 2008 12:01 pm 
RWAD...@UP.COMJul 18, 2008 12:07 pm 
RWAD...@UP.COMJul 18, 2008 12:09 pm 
RWAD...@UP.COMJul 18, 2008 12:28 pm 
Timothy WallJul 18, 2008 12:46 pm 
RWAD...@UP.COMJul 18, 2008 12:55 pm 
RWAD...@UP.COMJul 18, 2008 1:16 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] Array of Structures in a StructureActions...
From:Timothy Wall (twal@dev.java.net)
Date:Jul 18, 2008 9:26:57 am
List:net.java.dev.jna.users

On Jul 18, 2008, at 11:39 AM, RWAD@UP.COM wrote:

I'm having trouble properly initializing a C structure which contains an array of other structures. The C structure definition is:

typedef struct op_and_ornt_struct { Obj_Pnt_Struct obj_pnt; short int num_rpls; struct { Rail_Path_Loca_Struct rpl; Ropo_Struct ropo; double dist_from; } ornt[3]; } Op_And_Ornt_Struct;

____________________________________________________________

The Java code I'm using to emulate this structure is as follows:

package com.uprr.efm.nativ.struct; import java.util.List; import com.uprr.net.Orientation; import com.uprr.point.ObjectPointOrientation;

public class ObjectPointAndOrientationStructure extends Structure {

public ObjectPointStructure obj_pnt ; public int num_rpls ; public OrientationStructure[] orientations ;

You can initialize here with "orientations = new OrientationStructure[3];". JNA will auto-initialize the array as needed. You only need to initialize it if you need to initialize the data.

/** Default constructor. */ public ObjectPointAndOrientationStructure() { orientations = (OrientationStructure[])new OrientationStructure().toArray(3); } }

____________________________________________________________

I then create a 2-element array of these structures & pass it to a C function as follows:

final ObjectPointAndOrientationStructure opo = new ObjectPointAndOrientationStructure() ; final ObjectPointAndOrientationStructure[] opos = (ObjectPointAndOrientationStructure[])opo.toArray(2); myFunction(opos);

____________________________________________________________

The C function then tries to fill the 2-element "opos" array with zeroes, but crashes. Apparently the Java structure size differs from the C structure size.

That's a different problem than just initializing the array.

I'm guessing that may that the "orientations" array in the structure is not being inlined as intended, & that the Java constructor above is not creating it correctly. Does this make sense? If so, how do i instantiate the 3-element orientation array correctly so that it's inline inside of its parent structure?

The only way an array of structures would *not* be inlined is if they're declared to implement Structure.ByReference, in which case you'd get an array of structure pointers instead.

Check to see if the structure sizes match and if they don't use Structure.toString to figure out where they don't match.

You're using java "int" for native "short", which in most cases is incorrect. You probably want Java short instead.