Possible Duplicate:
C++ union in C#
Code in C:
typedef struct _EVENT_HEADER {
USHORT Size; // Event 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#?
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 thatKernelandProcessorTimehave the same offset. LargeInteger is also a good example of union implementation in C#.EventHeader
LargeInteger
EventDescriptor
Disclaimer: I just made this code. Didn’t test it. The code may have errors.