I have server application written in C that must send screenshot throguh socket and c# gui client. The problem is that i cant convert byte array to structure because of exception. It is simple to cast char array to struct in C/C++ but not that simple in c#. Here's the test code:
public partial class Form1 : Form
{
const int magic = 101;
enum command {GET_SCREEN = 1, MOVE_MOUSE, CLICK_MOUSE };
public struct send_packet
{
public int magic;
public int cmd;
};
public struct recv_packet
{
public int magic;
public int code;
public int length;
public byte[] body;
};
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int port = 4000;
TcpClient client = new TcpClient("127.0.0.1", 4000);
NetworkStream nws = client.GetStream();
BinaryWriter bw = new BinaryWriter(nws);
BinaryReader br = new BinaryReader(nws);
byte[] buff = new byte[512];
send_packet pkt = new send_packet();
pkt.magic = magic;
pkt.cmd = (int)command.GET_SCREEN;
while (true)
{
bw.Write(pkt.magic);
bw.Write(pkt.cmd);
br.Read(buff, 0, 512);
GCHandle pinnedPacket = GCHandle.Alloc(buff, GCHandleType.Pinned);
recv_packet rcv_pkt = (recv_packet)Marshal.PtrToStructure(pinnedPacket.AddrOfPinnedObject(), typeof(recv_packet));
//string str = System.Text.Encoding.Default.GetString(rcv_pkt.length);
string str = rcv_pkt.length.ToString();
MessageBox.Show(str);
}
}
}
What could it be? The text of exception is in russian, but there is most important info: "System.AccessViolationException" in mscorlib.dll, attempt to read or write to protected memory. Something like that. It happens right at this string recv_packet rcv_pkt = (recv_packet)Marshal.PtrToStructure(pinnedPacket.AddrOfPinnedObject(), typeof(recv_packet));
Here I've got an explanation why this doesn't work and a kind of solution https://social.msdn.microsoft.com/Forums/vstudio/en-US/3c152957-91e7-43bf-91de-a047a3d124f5/exception-when-converting-byte-array-to-structure?forum=csharpgeneral