

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
29 messages in net.java.dev.jna.usersAW: AW: [jna-users] Struct with Union...| From | Sent On | Attachments |
|---|---|---|
| Wolfgang Pichler | Mar 25, 2009 5:58 am | |
| Timothy Wall | Mar 25, 2009 7:02 am | |
| Timothy Wall | Mar 25, 2009 7:06 am | |
| Wolfgang Pichler | Mar 25, 2009 7:21 am | |
| Timothy Wall | Mar 25, 2009 7:24 am | |
| Wolfgang Pichler | Mar 25, 2009 8:11 am | |
| Wolfgang Pichler | Mar 25, 2009 8:20 am | |
| Novatchkov Hristo | Mar 25, 2009 8:41 am | |
| Timothy Wall | Mar 25, 2009 9:59 am | |
| Timothy Wall | Mar 25, 2009 10:28 am | |
| Timothy Wall | Mar 25, 2009 10:38 am | |
| Hristo Novatchkov | Mar 25, 2009 11:40 am | |
| Wolfgang Pichler | Mar 25, 2009 12:02 pm | |
| LYou...@gkservices.com | Mar 25, 2009 12:11 pm | |
| Timothy Wall | Mar 25, 2009 12:52 pm | |
| Wolfgang Pichler | Mar 26, 2009 12:22 am | |
| Timothy Wall | Mar 26, 2009 4:46 am | |
| Wolfgang Pichler | Mar 26, 2009 5:23 am | |
| Timothy Wall | Mar 26, 2009 6:09 am | |
| Wolfgang Pichler | Mar 26, 2009 6:13 am | |
| Wolfgang Pichler | Mar 26, 2009 6:26 am | |
| Timothy Wall | Mar 26, 2009 6:40 am | |
| Wolfgang Pichler | Mar 26, 2009 12:22 pm | |
| Timothy Wall | Mar 26, 2009 1:00 pm | |
| Wolfgang Pichler | Mar 26, 2009 3:15 pm | |
| Timothy Wall | Mar 30, 2009 6:06 am | |
| Wolfgang Pichler | Mar 30, 2009 11:55 am | |
| Timothy Wall | Mar 30, 2009 12:14 pm | |
| Wolfgang Pichler | Mar 31, 2009 12:35 am |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | AW: AW: [jna-users] Struct with Union with Structs... | Actions... |
|---|---|---|
| From: | Wolfgang Pichler (wpic...@callino.at) | |
| Date: | Mar 26, 2009 3:15:48 pm | |
| List: | net.java.dev.jna.users | |
Hi,
i should declare the dummy in the original c implementation ?
An other workaround would be to use a Pointer or Memory Object sized to the size of the greatest structure in the union - then parse the content out of the memory into the real structure as jna already does it...
I will try this and let you know
I have forgot to send the mail - so no second mail is necessary. I found a workaround which is quite useful.
I have changed the primary struct (which did contained the union) to the following public class iaxc_event_struct extends Structure { public static class ByValue extends iaxc_event_struct implements Structure.ByValue {} public Pointer next; public int type; public byte[] memory = new byte[1040];
}
So all possible struct data for the union gets into memory (a dummy buffer)
Second thing - I have subclassed Structure - and added function setMemory which makes useMemory public.
And in the callback - I do check for the type - then create the needed structure (which is now subclassed of my Structure subclass) - called setMemory with the parent structure .share function (with offset 8 - that is the next pointer and the int type) to point my structure to the right memory region. After that called read so that the fields get initialized - and voila - you can use it ;-)
Here is the callback - maybe code is clearer than my words ;-)
IAXClient.iaxc_ev_registration reg = new IAXClient.iaxc_ev_registration(); reg.setMemory(event.getPointer().share(8)); reg.read(); System.out.println("Event Event Raw: " + reg.toString());
Here is the subclassed Structure package org.callino.jiaxc;
import com.sun.jna.Pointer; import com.sun.jna.Structure;
public class GetStructure extends Structure {
/** * */ public void setMemory(Pointer m) { this.useMemory(m); }
}
Regards, Wolfgang
-----Ursprüngliche Nachricht----- Von: Timothy Wall [mailto:twal...@dev.java.net] Gesendet: Donnerstag, 26. März 2009 21:01 An: use...@jna.dev.java.net Betreff: Re: AW: [jna-users] Struct with Union with Structs...
I forgot that you're passing the original by value. That's probably where the hole is; the original pointer is temporary space, so its contents are read into the structure and the structure written back out to its original memory. That's why the union data doesn't show up in the native memory.
I'll have to think about how to fix this. As a workaround, you can declare a dummy field in the union that is the size of the union in bytes, e.g.
union _myunion { char dummy[SIZE]; // other union fields here }
And in the ctor of your event structure set the type of the union so that it'll be read properly on entry to the callback.
On Mar 26, 2009, at 3:22 PM, Wolfgang Pichler wrote:
Here is the whole dumb when using the specific structure IAXClient$iaxc_event_struct$ByValue(allocated@0x2fc8c98 (20 bytes) (shared from allocated@0x2fc8c98 (20 bytes))) { Pointer next@0=native@0x3b3fa9c int type@4=8 IAXClient$iaxc_ev_registration ev@8=IAXClient$iaxc_ev_registration(allocated@0x2fc8ca0 (12 bytes) (shared from allocated@0x2fc8c98 (20 bytes) (shared from allocated@0x2fc8c98 (20 bytes)))) { int id@0=1 int reply@4=18 int msgcount@8=-1 } } [9cfab303] -> ptr next [08000000] -> type=8 [01000000] -> from specific structure -> id=1 [12000000] -> from specific structure -> reply=18 [ffffffff] -> from specific structure -> msgcount=-1
Here the dump when using the union IAXClient$iaxc_event_struct$ByValue(allocated@0x2fc8c00 (20 bytes) (shared from allocated@0x2fc8c00 (20 bytes))) { Pointer next@0=native@0x3b3fa9c int type@4=8 IAXClient$iaxc_event_union ev@8=IAXClient$iaxc_event_union(allocated@0x2fc8c08 (12 bytes) (shared from allocated@0x2fc8c00 (20 bytes) (shared from allocated@0x2fc8c00 (20 bytes)))) { IAXClient$iaxc_ev_registration registration@0=IAXClient$iaxc_ev_registration(allocated@0x2fc8c20 (12 bytes)) { int id@0=0 int reply@4=0 int msgcount@8=0 } IAXClient$iaxc_ev_levels levels@0=IAXClient$iaxc_ev_levels(allocated@0x2fc8b18 (8 bytes)) { float input@0=0.0 float output@4=0.0 } } } [9cfab303] -> prt next [08000000] -> type=8 [00000000] -> from specific structure -> empty [00000000] -> from specific structure -> empty [00000000] -> from specific structure -> empty
When using the union - at iaxc_ev_registration - there should be shared from allocated, or do I misunderstand here something ?
Many thanks for your help
Regards, Wolfgang
-----Ursprüngliche Nachricht----- Von: Timothy Wall [mailto:twal...@dev.java.net] Gesendet: Donnerstag, 26. März 2009 14:41 An: use...@jna.dev.java.net Betreff: Re: [jna-users] Struct with Union with Structs...
What does the dump of the whole event struct look like, both when using the union and when using a specific struct as the data field? NOTE: the memory should *not* indicate "allocated", since it should be using whatever pointer was passed to the callback.
On Mar 26, 2009, at 9:27 AM, Wolfgang Pichler wrote:
As soon as i replace the union with one of the structs the memory does contain the correct data... this is driving me crazy....
public class iaxc_event_struct extends Structure { public static class ByValue extends iaxc_event_struct implements Structure.ByValue { } public Pointer next; public int type; public iaxc_ev_registration ev; }
This does give me correct results...
-----Ursprüngliche Nachricht----- Von: Timothy Wall [mailto:twal...@dev.java.net] Gesendet: Donnerstag, 26. März 2009 14:10 An: use...@jna.dev.java.net Betreff: Re: AW: AW: AW: AW: [jna-users] Struct with Union with Structs...
On Mar 26, 2009, at 8:23 AM, Wolfgang Pichler wrote:
this didn't help either...
here is the current code
public class iaxc_event_struct extends Structure { public static class ByValue extends iaxc_event_struct implements Structure.ByValue { } public Pointer next; public int type; public iaxc_event_union ev; }
switch(event.type) { case IAXClient.IAXC_EVENT_REGISTRATION: System.out.println("Event Raw: " + event.toString()); System.out.println("Event Event Raw: " + event.ev.toString());
Does this dump have the correct raw memory contents? If it does, then the problem lies with synching the union to its parent memory.
event.ev.setType(IAXClient.iaxc_ev_registration.class); event.read(); System.out.println("Event Raw: " + event.toString()); System.out.println("Event Event Raw: " + event.ev.toString());
Does the raw memory output change after event.read()?
break; case IAXClient.IAXC_EVENT_LEVELS: break; default: break; }
Regards, Wolfgang
-----Ursprüngliche Nachricht----- Von: Timothy Wall [mailto:twal...@dev.java.net] Gesendet: Donnerstag, 26. März 2009 12:47 An: use...@jna.dev.java.net Betreff: Re: AW: AW: AW: [jna-users] Struct with Union with Structs...
yes, post the callback code when using the union definition, and include the union definition with its surrounding structure.
I think the problem is that you need to set the union type, then call read() on the containing structure, *not* the union. The union only gets mapped into its containing structure when the containing structure is read; otherwise it reads from its own preallocated memory, which is why you get zeroes.
On Mar 26, 2009, at 3:23 AM, Wolfgang Pichler wrote:
Hi,
yep - that is the problem. The data is incorrect - better to say - there is no data where there should be data. The fields are all filled with 0 values. If i don't use the union - but directly the sub event structure - then i do have the data. So the problem must be with using the union.
I can provide the full source if it would help.
regards, Wolfgang
Am Mittwoch, den 25.03.2009, 15:53 -0400 schrieb Timothy Wall:
I just wanted to check to see that the union fields were all correctly aligned with the start of the union. That is the case, so something else must be the problem if you're reading back a union field and the data appears to be incorrect. That *is* the problem, right?
On Mar 25, 2009, at 3:03 PM, Wolfgang Pichler wrote:
I am not sure if i have understood you right - but here is what I have tried
Here is the source
System.out.println("Event Raw: " + event.toString()); System.out.println("Event Event Raw: " + event.ev.toString()); event.ev.setType(IAXClient.iaxc_ev_registration.class); event.ev.read(); IAXClient.iaxc_ev_registration reg = (IAXClient.iaxc_ev_registration) event.ev.getTypedValue(IAXClient.iaxc_ev_registration.class); reg.reply = 2; reg.writeField("reply"); Object reply = reg.readField("reply"); System.out.println("Event Reg: " + reg.toString());
The event.ev.toString gives me
Event Event Raw: IAXClient$iaxc_event_union(allocated@0x2fc8c08 (12 bytes) (shared from allocated@0x2fc8c00 (20 bytes) (shared from allocated@0x2fc8c00 (20 bytes)))) { IAXClient$iaxc_ev_registration registration@0=IAXClient$iaxc_ev_registration(allocated@0x2fc8c20 (12 bytes)) { int id@0=0 int reply@4=0 int msgcount@8=0 } IAXClient$iaxc_ev_levels levels@0=IAXClient$iaxc_ev_levels(allocated@0x2fc2760 (8 bytes)) { float input@0=0.0 float output@4=0.0 } } memory dump [00000000] [00000000] [00000000]
After the write and read operation I have this
Event Reg: IAXClient$iaxc_ev_registration(allocated@0x2fc8c08 (12 bytes) (shared from allocated@0x2fc8c08 (12 bytes) (shared from allocated@0x2fc8c00 (20 bytes) (shared from allocated@0x2fc8c00 (20 bytes))))) { int id@0=0 int reply@4=2 int msgcount@8=0 } memory dump [00000000] [02000000] [00000000]
So it was written correctly...
But I don't understand what this has to do with my problem...
I can provide the whole source code with test code (its not that much at time) if this would help...
Regards, Wolfgang
-----Ursprüngliche Nachricht----- Von: Timothy Wall [mailto:twal...@dev.java.net] Gesendet: Mittwoch, 25. März 2009 18:29 An: use...@jna.dev.java.net Betreff: Re: AW: AW: [jna-users] Struct with Union with Structs...
It's not actually an issue; the structure fields' memory offset gets synched to the enclosing memory when the enclosing memory is read or written. See what the values are after a read/write (note that a read/ write on a union is a no-op if it doesn't have an active type as set by setType).
On Mar 25, 2009, at 11:20 AM, Wolfgang Pichler wrote:
Hi,
i have now seen that when using union i do have 2 different base addresses for the two possible structs - that is wrong, or ? The should be equal if I am not wrong.
Also the base address should be 0x3f8810 - and not 0x3f8ff0 or 0x3f87c0
I am right with this ?
-----Ursprüngliche Nachricht----- Von: Wolfgang Pichler [mailto:wpic...@callino.at] Gesendet: Mittwoch, 25. März 2009 16:12 An: 'use...@jna.dev.java.net' Betreff: AW: AW: [jna-users] Struct with Union with Structs...
Hi,
this is what i get when I use the union as struct member
Event Raw: IAXClient$iaxc_event_struct $iaxc_event_union(allocated@0x3f8810 (12 bytes) (shared from allocated@0x3f8808 (20 bytes) (shared from allocated@0x3f8808 (20 bytes)))) { IAXClient$iaxc_ev_registration registration@0=IAXClient$iaxc_ev_registration(allocated@0x3f8ff0 (12 bytes)) { int id@0=0 int reply@4=0 int msgcount@8=0 } IAXClient$iaxc_ev_levels levels@0=IAXClient$iaxc_ev_levels(allocated@0x3f87c0 (8 bytes)) { float input@0=0.0 float output@4=0.0 } } memory dump [00000000] [00000000] [00000000]
And here is what I get when directly using the registration struct as struct member
Event Raw: IAXClient$iaxc_ev_registration(allocated@0x2fc8ca0 (12 bytes) (shared from allocated@0x2fc8c98 (20 bytes) (shared from allocated@0x2fc8c98 (20 bytes)))) { int id@0=1 int reply@4=18 int msgcount@8=-1 } memory dump [01000000] [12000000] [ffffffff]
As far as I can see it does look correct to me...
Regards, Wolfgang
-----Ursprüngliche Nachricht----- Von: Timothy Wall [mailto:twal...@dev.java.net] Gesendet: Mittwoch, 25. März 2009 15:25 An: use...@jna.dev.java.net Betreff: Re: AW: [jna-users] Struct with Union with Structs...
Check to see that the union field has the proper base address, which should be some offset from the enclosing structure's base address. All union field members will have the same base address as the union.
On Mar 25, 2009, at 10:21 AM, Wolfgang Pichler wrote:
The code does not produce an exception or a crash... - but it does return wrong values.
I will return nulled values - but there are real values - as I have seen when I do directly use the registration structure instead of the union. It does look like it does not get initialized...
Regards, Wolfgang
-----Ursprüngliche Nachricht----- Von: Timothy Wall [mailto:twal...@dev.java.net] Gesendet: Mittwoch, 25. März 2009 15:07 An: use...@jna.dev.java.net Betreff: Re: [jna-users] Struct with Union with Structs...
On Mar 25, 2009, at 8:59 AM, Wolfgang Pichler wrote:
Here is the callback function IAXClient.INSTANCE.iaxc_set_event_callback(new IAXClient.FUNCTION() { @Override public int invoke(iaxc_event_struct.ByValue event) { if (event==null) { return 0; } switch(event.type) { case IAXClient.IAXC_EVENT_REGISTRATION: System.out.println("Event Raw: " + event.ev.toString());
event.ev.setType(IAXClient.iaxc_ev_registration.class); event.ev.read(); IAXClient.iaxc_ev_registration reg = (iaxc_ev_registration) event.ev.getTypedValue(IAXClient.iaxc_ev_registration.class); System.out.println("Event Reg: " + reg.reply); break;
Once you've called "read", you can simply access the corresponding union field. getTypedValue is a shortcut for setType + read. How exactly does this code fail?
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------







