4 messages in net.java.dev.jna.usersRe: [jna-users] Wrong Structure Value...
FromSent OnAttachments
RWAD...@UP.COMJun 20, 2008 5:41 am 
Michael Brewer-DavisJun 20, 2008 10:59 am 
RWAD...@UP.COMJun 23, 2008 5:27 am 
Timothy WallJun 23, 2008 11:52 am 
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] Wrong Structure Values PassedActions...
From:Timothy Wall (twal@dev.java.net)
Date:Jun 23, 2008 11:52:26 am
List:net.java.dev.jna.users

Feel free to submit a patch for the portions of documentation that need clarification.

On Jun 23, 2008, at 8:27 AM, RWAD@UP.COM wrote:

I suspect you're probably right, though I haven't dug into the details because I found a workaround. I finally got it to work when, instead of creating new objects & storing their references in the array, I just called the getters of the existingreferences in the array.

This is obviously a place where the documentation needs major expansion & clarification.

_______________________________

RWAD@UP.COM wrote:

Point[] points = new Point[2]; points[0] = new Point(1,2); points[1] = new Point(3,4); Point[] structures = (Point[])new Point().toArray(points);

Not an expert, but I think you misunderstand this toArray call. The javadoc:

public Structure[] toArray(Structure[] array) Returns a view of this structure's memory as an array of structures. Note that this Structure must have a public, no-arg constructor. If the structure is currently using a Memory backing, the memory will be resized to fit the entire array.

I parse that as (a) the first element of the returned array will be "this" (b) subsequent elements of the returned array will be from "this"'s memory

This fits your data: (a) first point is Java allocated 0, 0 (b) second point is newly (C) allocated gibberish

The purpose would be to treat Structure* values passed from C as Structure[] values when appropriate. The array param just reuses that array, reduces that allocation.

One possible solution? Point p = new Point(1, 2); Point[] points = p.toArray(2); p[1].setPoint(3, 4); // or some other assignment