Marshalling union/structure within as structure?

G

Guest

I am try to marshall the following:
/*typedef struct tagRAWMOUSE
{
USHORT usFlags;
union
{
ULONG ulButtons;
struct
{
USHORT usButtonFlags;
USHORT usButtonData;
};
};
ULONG ulRawButtons;
LONG lLastX;
LONG lLastY;
ULONG ulExtraInformation;
} RAWMOUSE, *PRAWMOUSE, *LPRAWMOUSE;*/

To the following in C#:

[ StructLayout( LayoutKind.Sequential )]
public struct ButtonFlagsData
{
public UInt16 usButtonFlags;
public UInt16 usButtonData;
}
[ StructLayout( LayoutKind.Sequential )]
public struct MyUnion
{
public UInt32 ulButtons;
ButtonFlagsData buttonFlagsData;
}
[ StructLayout( LayoutKind.Sequential )]
public struct RAWMOUSE
{
public UInt16 Flags;
MyUnion myUnion;
public UInt32 RawButtons;
public Int32 LastX;
public Int32 LastY;
public UInt32 ExtraInformation;
}
Is that correct?? If not can you suggest the correct formation?? Thanks.
 
B

Barry Kelly

PickwickBob3 said:

Union means that each member in the inner structure starts at the same
address, the start of the inner structure.
[ StructLayout( LayoutKind.Sequential )]
public struct MyUnion
{
So, you need this:

[FieldOffset(0)]
public UInt32 ulButtons; [FieldOffset(0)]
ButtonFlagsData buttonFlagsData;
}
Is that correct?? If not can you suggest the correct formation?? Thanks.

That is how you correct the union, at the very least.

-- Barry
 
B

Barry Kelly

PickwickBob3 said:
[ StructLayout( LayoutKind.Sequential )]
public struct MyUnion

Forgot to mention: you'll want to change the LayoutKind to
LayoutKind.Explicit when you're using FieldOffset.


-- Barry
 
G

Guest

Thanks for your help. Does this look right? The method does not return any
values other than zero in the structures. Here is the code:

/* typedef struct tagRAWINPUTHEADER {
DWORD dwType;
DWORD dwSize;
HANDLE hDevice;
WPARAM wParam;
} RAWINPUTHEADER, *PRAWINPUTHEADER;*/
[ StructLayout( LayoutKind.Sequential )]
public struct RAWINPUTHEADER
{
private int Type;
private int Size;
private IntPtr Device;
private int wParam;
}

/*typedef struct tagRAWMOUSE
{
USHORT usFlags;
union
{
ULONG ulButtons;
struct
{
USHORT usButtonFlags;
USHORT usButtonData;
};
};
ULONG ulRawButtons;
LONG lLastX;
LONG lLastY;
ULONG ulExtraInformation;
} RAWMOUSE, *PRAWMOUSE, *LPRAWMOUSE;*/

[ StructLayout( LayoutKind.Sequential )]
public struct ButtonFlagsData
{
public UInt16 usButtonFlags;
public UInt16 usButtonData;
}
[ StructLayout( LayoutKind.Explicit )]
public struct MyUnion
{
[FieldOffset(0)]
public UInt32 ulButtons;
[FieldOffset(0)]
ButtonFlagsData buttonFlagsData;
}
[ StructLayout( LayoutKind.Sequential )]
public struct RAWMOUSE
{
public UInt16 Flags;
MyUnion myUnion;
public UInt32 RawButtons;
public Int32 LastX;
public Int32 LastY;
public UInt32 ExtraInformation;
}

[ StructLayout( LayoutKind.Explicit )]
public struct BUTTONFLDAT
{
[ FieldOffset( 0 )]
public UInt16 ButtonFlags;
[ FieldOffset( 0 )]
public UInt16 ButtonData;
}

[ StructLayout( LayoutKind.Explicit )]
public struct BUTTONS
{
[ FieldOffset( 0 )]
public UInt32 Buttons;
[ FieldOffset( 0 )]
public IntPtr buttonFlData;
}
/*typedef struct tagRAWHID {
DWORD dwSizeHid;
DWORD dwCount;
BYTE bRawData;
} RAWHID, **LPRAWHID*/
[ StructLayout( LayoutKind.Sequential )]
public struct RAWHID
{
public int SizeHid;
public int Count;
public System.Byte RawData;
}
/*typedef struct tagRAWKEYBOARD {
USHORT MakeCode;
USHORT Flags;
USHORT Reserved;
USHORT VKey;
UINT Message;
ULONG ExtraInformation;
} RAWKEYBOARD, *PRAWKEYBOARD, *LPRAWKEYBOARD;
*/
[ StructLayout( LayoutKind.Sequential )]
public struct RAWKEYBOARD
{
public System.Int16 MakeCode;
public System.Int16 Flags;
public System.Int16 Reserved;
public System.Int16 VKey;
public System.UInt32 Message;
public System.UInt32 ExtraInformantion;
}
/*typedef struct tagRAWINPUT {
RAWINPUTHEADER header;
union {
RAWMOUSE mouse;
RAWKEYBOARD keyboard;
RAWHID hid;
} data;
} RAWINPUT, *PRAWINPUT; *LPRAWINPUT;*/
[ StructLayout( LayoutKind.Sequential )]
public struct MyUnion2
{
RAWMOUSE mouse;
RAWKEYBOARD keyboard;
RAWHID hid;
}
[ StructLayout( LayoutKind.Sequential )]
public struct RAWINPUT
{
RAWINPUTHEADER header;
MyUnion2 myUnion2;
}

/*
typedef struct tagRAWINPUTDEVICELIST {
HANDLE hDevice;
DWORD dwType;
} RAWINPUTDEVICELIST, *PRAWINPUTDEVICELIST;
*/
[ StructLayout( LayoutKind.Sequential )]
public struct RAWINPUTDEVICELIST // must use struct instead of class
{
public IntPtr Device;
public int Type;
}
/*typedef struct tagRID_DEVICE_INFO_MOUSE {
DWORD dwId;
DWORD dwNumberOfButtons;
DWORD dwSampleRate;
} RID_DEVICE_INFO_MOUSE, *PRID_DEVICE_INFO_MOUSE;*/
[ StructLayout( LayoutKind.Sequential )]
public struct RID_DEVICE_INFO_MOUSE
{
public int dwId;
public int dwNumberOfButtons;
public int dwSampleRate;
}
/*typedef struct tagRID_DEVICE_INFO_KEYBOARD {
DWORD dwType;
DWORD dwSubType;
DWORD dwKeyboardMode;
DWORD dwNumberOfFunctionKeys;
DWORD dwNumberOfIndicators;
DWORD dwNumberOfKeysTotal;
} RID_DEVICE_INFO_KEYBOARD, *PRID_DEVICE_INFO_KEYBOARD;
*/
[ StructLayout( LayoutKind.Sequential )]
public struct RID_DEVICE_INFO_KEYBOARD
{
public int dwType;
public int dwSubType;
public int dwKeyboardMode;
public int dwNumberOfFunctionKeys;
public int dwNumberOfIndicators;
public int dwNumberOfKeysTotal;
}
/*typedef struct tagRID_DEVICE_INFO_HID {
DWORD dwVendorId;
DWORD dwProductId;
DWORD dwVersionNumber;


* Top level collection UsagePage and Usage

USHORT usUsagePage;
USHORT usUsage;
} RID_DEVICE_INFO_HID, *PRID_DEVICE_INFO_HID;
*/


[ StructLayout( LayoutKind.Sequential )]
public struct RID_DEVICE_INFO_HID
{
public int dwVendorId;
public int dwProductId;
public int dwVersionNumber;

// Top level collection UsagePage and Usage

public UInt16 usUsagePage;
public UInt16 usUsage;
}
/*typedef struct tagRID_DEVICE_INFO {
DWORD cbSize;
DWORD dwType;
union {
RID_DEVICE_INFO_MOUSE mouse;
RID_DEVICE_INFO_KEYBOARD keyboard;
RID_DEVICE_INFO_HID hid;
};
} RID_DEVICE_INFO, *PRID_DEVICE_INFO, *LPRID_DEVICE_INFO;
*/
[ StructLayout( LayoutKind.Sequential )]
public struct Union_RID_DEVICE_INFO
{
RID_DEVICE_INFO_MOUSE mouse;
RID_DEVICE_INFO_KEYBOARD keyboard;
RID_DEVICE_INFO_HID hid;
}
[ StructLayout( LayoutKind.Sequential )]
public struct RID_DEVICE_INFO
{
public int cbSize;
public int dwType;
Union_RID_DEVICE_INFO unionRidDeviceInfo;
}

--
BP


PickwickBob3 said:
I am try to marshall the following:
/*typedef struct tagRAWMOUSE
{
USHORT usFlags;
union
{
ULONG ulButtons;
struct
{
USHORT usButtonFlags;
USHORT usButtonData;
};
};
ULONG ulRawButtons;
LONG lLastX;
LONG lLastY;
ULONG ulExtraInformation;
} RAWMOUSE, *PRAWMOUSE, *LPRAWMOUSE;*/

To the following in C#:

[ StructLayout( LayoutKind.Sequential )]
public struct ButtonFlagsData
{
public UInt16 usButtonFlags;
public UInt16 usButtonData;
}
[ StructLayout( LayoutKind.Sequential )]
public struct MyUnion
{
public UInt32 ulButtons;
ButtonFlagsData buttonFlagsData;
}
[ StructLayout( LayoutKind.Sequential )]
public struct RAWMOUSE
{
public UInt16 Flags;
MyUnion myUnion;
public UInt32 RawButtons;
public Int32 LastX;
public Int32 LastY;
public UInt32 ExtraInformation;
}
Is that correct?? If not can you suggest the correct formation?? Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top