简体   繁体   中英

Exception when converting byte array to struct

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

    //Functions to turn byte arrays to structs. 

    public static byte[] RawSerialize<T>(T frqStruct)
    where T : struct
    {
        int rawsize = Marshal.SizeOf<T>();

        IntPtr i = IntPtr.Zero;
        try
        {
            i = Marshal.AllocHGlobal(rawsize);
            Marshal.StructureToPtr<T>(frqStruct, i, false);
            byte[] rawdatas = new byte[rawsize];
            Marshal.Copy(i, rawdatas, 0, rawsize);
            return rawdatas;
        }
        finally
        {
            if (i != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(i);
            }
        }
    }

    public static T RawDeserialize<T>(byte[] bytearray)
    where T : struct
    {
        int len = Marshal.SizeOf<T>();

        IntPtr i = IntPtr.Zero;
        try
        {
            i = Marshal.AllocHGlobal(len);

            Marshal.Copy(bytearray, 0, i, len);

            return Marshal.PtrToStructure<T>(i);
        }
        finally
        {
            if (i != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(i);
            }
        }
    }

use like this

 Struct struct1=new Struct();
 byte[] outarr = RawSerialize(struct1);

struct1 = RawDeserialize<Struct>(outarr);

Also make sure your structs are declared correctly (with sizes etc):

[StructLayout(LayoutKind.Sequential, Size = 24)]
public struct myStruct
{
    public byte byte1;
    public byte byte2;
    public ushort reserved;
    //Was char[16]
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
    public string name; // null terminated

    public uint version;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM