On May 16, 2007, at 6:55 AM, Wayne Meissner wrote:
Union class from it. My C is a bit rusty these days, so I'm looking
for a nice challenge.
Unions are a bit tricky to implement properly in JNA. Although a
Union class could be implemented, where it maps each of its members
to the same memory area (basically the same as my example), there
is no way for JNA to know which one of those members holds the most
recently modified values that need to be written out to native memory.
A union is really nothing more than a shorthand for a cast, where
something outside your client code is writing the memory and you
don't know beforehand what's in it. Typically you have some flag
(often the first field in the union) which tells you which union
variant to use to extract the data, but using a union variant is no
different than applying a typecast to the data pointer.
If you're just passing info to an API, then you should just define
the data using the union variant you're using. There's no point in
anonymizing the data.