7 messages in net.java.dev.jna.usersRe: [jna-users] Problems with Unions
FromSent OnAttachments
Hendrik SalyFeb 11, 2009 10:27 am 
Timothy WallFeb 11, 2009 12:41 pm 
Hendrik SalyFeb 11, 2009 1:19 pm 
Timothy WallFeb 11, 2009 2:16 pm 
Hendrik SalyFeb 11, 2009 10:35 pm 
Timothy WallFeb 12, 2009 7:53 am 
Hendrik SalyFeb 12, 2009 12:14 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] Problems with UnionsActions...
From:Timothy Wall (twal@dev.java.net)
Date:Feb 11, 2009 2:16:31 pm
List:net.java.dev.jna.users

You can omit the union altogether and just make it a Pointer field, then manually extract the appropriate Structure type and use STructure.useMemory with the pointer field.

That's probably easier than coming up with a more complex solution that uses Union.

On Feb 11, 2009, at 4:20 PM, Hendrik Saly wrote:

Timothy,

thank, thats it, great.

But unfortunately i also have to use setType() in the constructor of my union, because if i use it not all is null. The problem is now, that the union is instantiated by the callback and so i have no chance to issue a setType (before data gets written to the union) to choose the right field.

Any clues?

Thanks in advance Hendrik

Am Mittwoch, den 11.02.2009, 15:41 -0500 schrieb Timothy Wall:

Your union members need to be pointer types. By default structure types are inlined when nested, so you must use Pointer or implement StructureByReference when used as a structure field.

On Feb 11, 2009, at 1:27 PM, Hendrik Saly wrote:

Hello again,

is it correct to map a C Union

C:

typedef struct test_callback_data { unsigned int type;

union specific_data {

test__DATA *test_data; test_XX_DATA *data; } callback_data;

} test_CALLBACK_DATA;

to Java using JNA in that way:

Java:

public static class test_callback_data extends Structure { public int type; public UNION_callback_data callback_data; }

public static class UNION_callback_data extends Union {

public test__DATA test_data; public test_XX_DATA data;

}

public interface CALLBACK extends com.sun.jna.Callback { int callback(test_callback_data data); }

The Union is filled by a callback function. The instance fields are then always 0 or null, so i guess they get not properly initialised. How to use the setType(Class clazz) method in/with the Union? Should i use or override the method?

Environment: Linux i386, JNA 3.0.5, Sun JDK 1.5

Thanks Hendrik

PGP 0x22d7f6ec _________________________________