473,623 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ulExtraInformat ion;
} RAWMOUSE, *PRAWMOUSE, *LPRAWMOUSE;*/

To the following in C#:

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

--
BP
Jul 12 '06 #1
3 2840
PickwickBob3 <Pi**********@d iscussions.micr osoft.comwrote:
union
Union means that each member in the inner structure starts at the same
address, the start of the inner structure.
[ StructLayout( LayoutKind.Sequ ential )]
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**********@d iscussions.micr osoft.comwrote:
[ StructLayout( LayoutKind.Sequ ential )]
public struct MyUnion
Forgot to mention: you'll want to change the LayoutKind to
LayoutKind.Expl icit 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 tagRAWINPUTHEAD ER {
DWORD dwType;
DWORD dwSize;
HANDLE hDevice;
WPARAM wParam;
} RAWINPUTHEADER, *PRAWINPUTHEADE R;*/
[ StructLayout( LayoutKind.Sequ ential )]
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 ulExtraInformat ion;
} RAWMOUSE, *PRAWMOUSE, *LPRAWMOUSE;*/

[ StructLayout( LayoutKind.Sequ ential )]
public struct ButtonFlagsData
{
public UInt16 usButtonFlags;
public UInt16 usButtonData;
}
[ StructLayout( LayoutKind.Expl icit )]
public struct MyUnion
{
[FieldOffset(0)]
public UInt32 ulButtons;
[FieldOffset(0)]
ButtonFlagsData buttonFlagsData ;
}
[ StructLayout( LayoutKind.Sequ ential )]
public struct RAWMOUSE
{
public UInt16 Flags;
MyUnion myUnion;
public UInt32 RawButtons;
public Int32 LastX;
public Int32 LastY;
public UInt32 ExtraInformatio n;
}

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

[ StructLayout( LayoutKind.Expl icit )]
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.Sequ ential )]
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 ExtraInformatio n;
} RAWKEYBOARD, *PRAWKEYBOARD, *LPRAWKEYBOARD;
*/
[ StructLayout( LayoutKind.Sequ ential )]
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 ExtraInformanti on;
}
/*typedef struct tagRAWINPUT {
RAWINPUTHEADER header;
union {
RAWMOUSE mouse;
RAWKEYBOARD keyboard;
RAWHID hid;
} data;
} RAWINPUT, *PRAWINPUT; *LPRAWINPUT;*/
[ StructLayout( LayoutKind.Sequ ential )]
public struct MyUnion2
{
RAWMOUSE mouse;
RAWKEYBOARD keyboard;
RAWHID hid;
}
[ StructLayout( LayoutKind.Sequ ential )]
public struct RAWINPUT
{
RAWINPUTHEADER header;
MyUnion2 myUnion2;
}

/*
typedef struct tagRAWINPUTDEVI CELIST {
HANDLE hDevice;
DWORD dwType;
} RAWINPUTDEVICEL IST, *PRAWINPUTDEVIC ELIST;
*/
[ StructLayout( LayoutKind.Sequ ential )]
public struct RAWINPUTDEVICEL IST // must use struct instead of class
{
public IntPtr Device;
public int Type;
}
/*typedef struct tagRID_DEVICE_I NFO_MOUSE {
DWORD dwId;
DWORD dwNumberOfButto ns;
DWORD dwSampleRate;
} RID_DEVICE_INFO _MOUSE, *PRID_DEVICE_IN FO_MOUSE;*/
[ StructLayout( LayoutKind.Sequ ential )]
public struct RID_DEVICE_INFO _MOUSE
{
public int dwId;
public int dwNumberOfButto ns;
public int dwSampleRate;
}
/*typedef struct tagRID_DEVICE_I NFO_KEYBOARD {
DWORD dwType;
DWORD dwSubType;
DWORD dwKeyboardMode;
DWORD dwNumberOfFunct ionKeys;
DWORD dwNumberOfIndic ators;
DWORD dwNumberOfKeysT otal;
} RID_DEVICE_INFO _KEYBOARD, *PRID_DEVICE_IN FO_KEYBOARD;
*/
[ StructLayout( LayoutKind.Sequ ential )]
public struct RID_DEVICE_INFO _KEYBOARD
{
public int dwType;
public int dwSubType;
public int dwKeyboardMode;
public int dwNumberOfFunct ionKeys;
public int dwNumberOfIndic ators;
public int dwNumberOfKeysT otal;
}
/*typedef struct tagRID_DEVICE_I NFO_HID {
DWORD dwVendorId;
DWORD dwProductId;
DWORD dwVersionNumber ;
* Top level collection UsagePage and Usage

USHORT usUsagePage;
USHORT usUsage;
} RID_DEVICE_INFO _HID, *PRID_DEVICE_IN FO_HID;
*/
[ StructLayout( LayoutKind.Sequ ential )]
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_I NFO {
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_IN FO, *LPRID_DEVICE_I NFO;
*/
[ StructLayout( LayoutKind.Sequ ential )]
public struct Union_RID_DEVIC E_INFO
{
RID_DEVICE_INFO _MOUSE mouse;
RID_DEVICE_INFO _KEYBOARD keyboard;
RID_DEVICE_INFO _HID hid;
}
[ StructLayout( LayoutKind.Sequ ential )]
public struct RID_DEVICE_INFO
{
public int cbSize;
public int dwType;
Union_RID_DEVIC E_INFO unionRidDeviceI nfo;
}

--
BP
"PickwickBo b3" 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 ulExtraInformat ion;
} RAWMOUSE, *PRAWMOUSE, *LPRAWMOUSE;*/

To the following in C#:

[ StructLayout( LayoutKind.Sequ ential )]
public struct ButtonFlagsData
{
public UInt16 usButtonFlags;
public UInt16 usButtonData;
}
[ StructLayout( LayoutKind.Sequ ential )]
public struct MyUnion
{
public UInt32 ulButtons;
ButtonFlagsData buttonFlagsData ;
}
[ StructLayout( LayoutKind.Sequ ential )]
public struct RAWMOUSE
{
public UInt16 Flags;
MyUnion myUnion;
public UInt32 RawButtons;
public Int32 LastX;
public Int32 LastY;
public UInt32 ExtraInformatio n;
}
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
2370
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 compiler), and it is, what I conclude from the below code union data_type {
5
1689
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 overwritten, i.e. should not be part of the union. The remaining 2, UnionData1 and UnionData2, make up the union and are populated as needed. I want to be certain that this code will work as planned. Specifically, if the field offsets of the...
2
5541
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 th32ParentProcessID As Long
18
12947
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
2484
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 GetChildren(GetChildrenParm *, Results *);
1
1542
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 _FILE_OBJECT_ID_BUFFER {
2
3338
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 need to pass an array of RADIO_INFO2 structures to be filled by a function in the DLL. This is how the structure is defined in the C++ example that comes with the DLL:
2
2145
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 SendMessage() with WM_COPYDATA to send a string data between two instances of a program. As part of the debugging I've written two methods one where a given instance will obtain its own hWnd and send itself the message, one where it accepts
1
2533
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 SendMessage() with WM_COPYDATA to send a string data between two instances of a program. As part of the debugging I've written two methods one where a given instance will obtain its own hWnd and send itself the message, one where it accepts
0
8227
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8165
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8613
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8469
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4074
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1778
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.