I want to marshall the following c structs with c#.
struct ConnectionMessage
{
static const size_t MESSAGE_MAX_PATH = 260;
uint32_t version;
uint64_t pid;
char machineName[32];
char executablePath[MESSAGE_MAX_PATH];
};
struct TextMessage
{
static const size_t TEXT_SIZE = 256;
uint64_t timestamp;
uint32_t severity;
char module[32];
char channel[32];
char message[TEXT_SIZE];
};
struct RawLogMessage
{
uint32_t type;
union
{
ConnectionMessage connection;
TextMessage text;
};
};
The structs I've been using in the past were working fine until I changed the C# assembly to x64.
These are the structs:
[StructLayout(LayoutKind.Explicit, Pack = 1)]
public struct TextMessage
{
[FieldOffset(0), MarshalAs(UnmanagedType.U8)]
public UInt64 Timestamp;
[FieldOffset(8), MarshalAs(UnmanagedType.U4)]
public LogSeverity Severity;
[FieldOffset(12), MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string Module;
[FieldOffset(44), MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string Channel;
[FieldOffset(76), MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string Message;
public override string ToString()
{
return String.Format("{0} {1} {2} {3} {4}", this.Timestamp, this.Severity, this.Module, this.Channel, this.Message);
}
}
[StructLayout(LayoutKind.Explicit, Pack = 1)]
public struct ConnectionMessage
{
[FieldOffset(0)]
public UInt32 Version;
[FieldOffset(8)]
public UInt32 Pid;
public override string ToString()
{
return String.Format("{0} {1}", this.Version, this.Pid);
}
}
[StructLayout(LayoutKind.Explicit, Pack = 0)]
public struct RawLogMessage
{
[FieldOffset(0), MarshalAs(UnmanagedType.U4)]
public MessageType Type;
[FieldOffset(8)]
public TextMessage TextMessage;
[FieldOffset(8)]
public ConnectionMessage ConnectionMessage;
}
What would be the proper way to handle that? What is the reason that it doesn't work anymore after changing the C# assembly to x64? I appreciate any hint/help.