开发者

Need help to converting from union in c to c# [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

C++ union in C#

Code in C:

typedef struct _EVENT_HEADER {
USHORT              Size;                   // Even开发者_StackOverflowt Size
USHORT              HeaderType;             // Header Type
USHORT              Flags;                  // Flags
USHORT              EventProperty;          // User given event property
ULONG               ThreadId;               // Thread Id
ULONG               ProcessId;              // Process Id
LARGE_INTEGER       TimeStamp;              // Event Timestamp
GUID                ProviderId;             // Provider Id
EVENT_DESCRIPTOR    EventDescriptor;        // Event Descriptor
union {
    struct {
        ULONG       KernelTime;             // Kernel Mode CPU ticks
        ULONG       UserTime;               // User mode CPU ticks
    } DUMMYSTRUCTNAME;
    ULONG64         ProcessorTime;          // Processor Clock 
                                            // for private session events
} DUMMYUNIONNAME;
GUID                ActivityId;             // Activity Id

} EVENT_HEADER, *PEVENT_HEADER;

I converted anything but the union. How to convert it to C#?


You can use [StructLayout(LayoutKind.Explicit)] to explicitly place the members at the correct offsets.

Here is an example from an answer I provided previously

[StructLayout(LayoutKind.Explicit)]
public struct CharUnion
{
  [FieldOffset(0)] public char UnicodeChar;
  [FieldOffset(0)] public byte AsciiChar;
}

[StructLayout(LayoutKind.Explicit)]
public struct CharInfo
{
  [FieldOffset(0)] public CharUnion Char;
  [FieldOffset(2)] public short Attributes;
}


C# doesn't natively support the C/C++ notion of unions. You can however use the StructLayout(LayoutKind.Explicit) and FieldOffset attributes to create equivalent functionality.

Regarding to union: in the code below you can see that Kernel and ProcessorTime have the same offset. LargeInteger is also a good example of union implementation in C#.

EventHeader

[StructLayout(LayoutKind.Explicit)]
public struct EventHeader
{
    [FieldOffset(0)]
    public ushort Size;

    [FieldOffset(2)]
    public ushort HeaderType;

    [FieldOffset(4)]
    public ushort Flags;

    [FieldOffset(6)]
    public ushort EventProperty;

    [FieldOffset(8)]
    public uint ThreadId;

    [FieldOffset(12)]
    public uint ProcessId;

    [FieldOffset(16)]
    public LargeInteger TimeStamp;

    [FieldOffset(24)]
    public Guid ProviderId;

    [FieldOffset(40)]
    public Guid EventDescriptor;

    [FieldOffset(52)]
    public uint KernelTime;

    [FieldOffset(56)]
    public uint UserTime;

    [FieldOffset(52)]
    public ulong ProcessorTime;

    [FieldOffset(60)]
    public Guid ActivityId;
}

LargeInteger

[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct LargeInteger
{
    [FieldOffset(0)]
    public long QuadPart;

    [FieldOffset(0)]
    public uint LowPart;

    [FieldOffset(4)]
    public uint HighPart;
}

EventDescriptor

[StructLayout(LayoutKind.Sequential)]
public struct EventDescriptor
{
    public ushort Id;

    public byte Level;

    public byte Channel;

    public byte LevelSeverity;

    public byte Opcode;

    public ushort Task;

    public uint Keyword;
}

Disclaimer: I just made this code. Didn't test it. The code may have errors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜