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 12, 2009 7:53:39 am
List:net.java.dev.jna.users

On Feb 12, 2009, at 1:35 AM, Hendrik Saly wrote:

so i do the following

public static class test_callback_data extends Structure { public int type;

public UNION_callback_data callback_data;

Just use Pointer here.

new CALLBACK() { int callback(test_callback_data data) {

//use Structure.useMemory here??

Structure s = data.type == 0 ? new Structure0(data.callback_data) : new Structure1(data.callback_data);

In the ctors of your structures, pass in the pointer and then use Structure.useMemory(), followed by Structure.read() if you need to access the data right away.

Presumably data.type tells you what part of the union to use.

System.out.println("my data "+test_callback_data data.callback_data.test_XX_DATA); } }

but i dont find out where to extract the appropriate Structure type and how to use (or to override) the useMemory(Pointer) method.

Sorry, but i'am new to JNA.

Thanks Hendrik

Am Mittwoch, den 11.02.2009, 17:16 -0500 schrieb Timothy Wall:

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