Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2005

Tip: Looking for answers? Try searching our database.

Union of structs in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bazooka - 08 Sep 2005 09:32 GMT
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??

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.