How do I convert this struct/union from C++ code into my C#-UWP-code? The important thing is, that the logic and references does not change because this struct must be sent to a server.
the difference to this article ( Convert C++ struct to C# )
- i have got nonprimitive datatypes (as another struct and long[]) in my struct
- i have got unions in my struct
typedef struct _HEADER
{
_HEADER_TYPE HeaderType;
ULONG cc;
union
{
struct
{
LONG Protocol;
_TYPE CType;
_INFO InfoDesired; // -> that's another struct
LONG ResolutionX[MAX_]; // -> how do i initialize an array in c# with maximum size ?
LONG ResolutionY[MAX_];
} Identification;
struct
{
LONG Width;
_TYPE Type;
_INFO Info; // -> that's another struct
} Buffer;
} u;
} _HEADER, *_HEADER;
_HEADER_TYPE is an enum:
public enum _HEADER_TYPE
{
_HEADER_TYPE_IDENTIFICATION,
_HEADER_TYPE_PING
}
_INFO is a struct:
public struct _INFO
{
public TJ S;
public long Q;
public long R1;
}
TJ is an enum:
public enum TJSAMP
{
_44,
_42
}
_TYPE is an enum:
public enum _TYPE
{
_OFF
_ON
}
What I've tried so far (C# code):
[StructLayout(LayoutKind.Explicit,Size=TotalBytesInStruct),Serializable]
public struct _HEADER
{
[FieldOffset(0)]
public _HEADER_TYPE HeaderType;
[FieldOffset(2)]
public ulong cc;
[FieldOffset(4)]
public longProtocol;
[FieldOffset(4)]
public _TYPE CType;
[FieldOffset(4)]
public _INFO InfoDesired; // -> that's another struct
[FieldOffset(4)]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)]
public long[] ResolutionX;
[FieldOffset(4)]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)]
public long[] ResolutionY;
[FieldOffset(8)]
public long Width;
[FieldOffset(8)]
public _TYPE Type;
[FieldOffset(8)]
public _INFO Info; // -> that's another struct
}
Does this exactly the same as the c++ struct above ?