I am just trying to send packets back and forth between my two apps but my strings aren't getting through in c++. here's what I'm doing in c#
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Size=32)]
public struct Packet
{
public uint packet_type;
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 8)]
public string file_name;
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 8)]
public string template_name;
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 8)]
public string file_name_list;
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 8)]
public string file_buffer;
}
var data = new Packet
{
packet_type = (uint)action,
file_name = fileName + Char.MinValue,
file_name_list = "" + Char.MinValue,
template_name = "" + Char.MinValue
};
byte[] buffer = new byte[Marshal.SizeOf(typeof(Packet))];
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
Marshal.StructureToPtr(data, handle.AddrOfPinnedObject(), false);
handle.Free();
var bytesSent = _client.Client.Send(buffer);
byte[] buffer = new byte[Marshal.SizeOf(typeof(Packet))];
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
Marshal.StructureToPtr(data, handle.AddrOfPinnedObject(), false);
handle.Free();
var bytesSent = _client.Client.Send(buffer);
and here's what I got in c++
struct Packet {
unsigned int packet_type;
char* file_name;
char* template_name;
char* file_name_list;
char* file_data;
void serialize(char * data) {
memcpy(data, this, sizeof(Packet));
}
void deserialize(char * data) {
memcpy(this, data, sizeof(Packet));
}
};
char network_data[MAX_PACKET_SIZE];
recv(curSocket, buffer, MAX_PACKET_SIZE, 0);
The only value that seems to work is the packet_type which is the first one in the struct. that one always comes through. What am I missing?