How far is it possible to handle unmanaged C++ declarations of unions i
C#?? A struct with a union is resolved wit
[StructLayout(LayoutKind.Explicit)] and [FieldOffset(pos)] but wha
about this one??
typedef Byte Bits;
typedef union
{
struct {
Bits queue :4; /* Network interface messag
queue */
Bits q_cmd :4; /* Network interface comman
with queue */
Bits length :8; /* Length of the buffer t
follow */
} q; /* Queue option
*/
struct {
Byte cmd; /* Network interface comman
w/o queue */
Byte length; /* Length of the buffer t
follow */
} noq; /* No queue option
*/
} NI_Hdr;
Is there a solution in C# managed code?
--
bazook
Robert Jordan - 08 Sep 2005 13:26 GMT
bazooka,
> How far is it possible to handle unmanaged C++ declarations of unions in
> C#?? A struct with a union is resolved with
[quoted text clipped - 24 lines]
>
> Is there a solution in C# managed code??
The default marshal doesn't handle nested structs, so
you have to "explode" them:
[StructLayout(LayoutKind.Explicit)]
public struct NI_Hdr {
[FieldOffset(0)] public byte q;
[FieldOffset(0)] public byte noq_cmd;
[FieldOffset(1)] public byte noq_length;
}
The bits of NI_Hdr.q must be extracted using bit arithmetic.
Rob
Jared Parsons [MSFT] - 09 Sep 2005 00:35 GMT
Try reading this and see if it helps.
http://blogs.msdn.com/jaredpar/archive/2005/05/13/417263.aspx

Signature
Jared Parsons [MSFT]
jaredpar@online.microsoft.com
http://blogs.msdn.com/jaredpar
"This posting is provided "AS IS" with no warranties, and confers no rights"
>
> How far is it possible to handle unmanaged C++ declarations of unions in
[quoted text clipped - 25 lines]
>
> Is there a solution in C# managed code??