473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Marshalling union/structure within as structure?

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.

--
BP
Jul 12 '06 #1
3 2828
PickwickBob3 <Pi**********@discussions.microsoft.comwrote:
union
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

--
http://barrkel.blogspot.com/
Jul 12 '06 #2
PickwickBob3 <Pi**********@discussions.microsoft.comwrote:
[ 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

--
http://barrkel.blogspot.com/
Jul 12 '06 #3
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" wrote:
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.

--
BP
Jul 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and...
5
by: Mike | last post by:
Within the following structure, TopStruct, I'd like to create 3 other structures, 2 of which make up a union. The first structure will always contain some data that I need and should never be...
2
by: Howard Kaikow | last post by:
I've got the following in a VB 6 project: Private Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long...
18
by: Mockey Chen | last post by:
My friend ask me a question as following: give a union SU define as: typedef union _SU { short x; struct y{ char a; short b; char c;
2
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall...
1
by: jsshah | last post by:
Hi I want to marshal following Win32 struct into .NEt class or struct as I want to call a native dll function from a Csharp code. The win32 struct ----------------------- typedef struct...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
2
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
1
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.