passing a struct through a namedpipe
Hi. I got a problem with a program made up with 2 modules. The 1st written in C++ that generates data and the 2nd one that reads data. The transmission take place through a NamedPipe that transport data from "c++" to "c#". Data are in form of structure.
Sending a simple integer isn't a problem but when I try to send a struct, i got a problem in the C# module that seems not able to transform data in the read buffer to the original struct.
This is a simplified code i wrote:
C++ SIDE
npipe = CreateFile("\\\\.\\pipe\\TestChannel",
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);
struttura _s;
_s.numero = 12;
WriteFile(npipe, &_s, sizeof(struttura), &bread, NULL)
C# SIDE
[StructLayout(LayoutKind.Sequential)]
public struct struttura {
public int numero;
}
FileStream fStream = new FileStream(clientPipeHandle, FileAccess.ReadWrite, BUFFER_SIZE, true);
byte[] buffer = new byte[BUFFER_SIZE];
ASCIIEncoding encoder = new ASCIIEncoding();
struttura _s;
_s.numero = -1;
fStream.Read(buffer, 0, System.Runtime.InteropServices.Marshal.SizeOf(_s.GetType()));
IntPtr i = (IntPtr) System.Runtime.InteropServices.Marshal.SizeOf(_s);
_s = (struttura) System.Runtime.InteropServices.Marshal.PtrToStructure(i ,_s.GetType());