title: Casting a byte array to a managed structure
body: <p><strong>Update: Answers to this question helped me code the open sourced project <a href="https://github.com/AlicanC/AlicanC-s-Modern-Warfare-2-Tool" rel="noreferrer">AlicanC's Modern Warfare 2 Tool on GitHub</a>. You can see how I am reading these packets in <a href="https://github.com/AlicanC/AlicanC-s-Modern-Warfare-2-Tool/blob/master/ACMW2HostTool/MW2Packets.cs" rel="noreferrer">MW2Packets.cs</a> and the extensions I've coded to read big endian data in <a href="https://github.com/AlicanC/AlicanC-s-Modern-Warfare-2-Tool/blob/master/ACMW2HostTool/Extensions.cs" rel="noreferrer">Extensions.cs</a>.</strong></p>
  
  <p>I am capturing UDP packets of <a href="http://store.steampowered.com/app/10180/" rel="noreferrer">Call of Duty: Modern Warfare 2</a> using <a href="http://pcapdotnet.codeplex.com/" rel="noreferrer">Pcap.Net</a> in my C# application. I receive a <code>byte[]</code> from the library. I tried to parse it like a string, but that didn't work well.</p>
  
  <p>The <code>byte[]</code> I have has a generic packet header, then another header specific to the packet type then info about each player in the lobby.</p>
  
  <p>A helpful person inspected some packets for me and came up with these structures:</p>
  
  <pre><code>// Fields are big endian unless specified otherwise.
  struct packet_header
  {
      uint16_t magic;
      uint16_t packet_size;
      uint32_t unknown1;
      uint32_t unknown2;
      uint32_t unknown3;
      uint32_t unknown4;
      uint16_t unknown5;
      uint16_t unknown6;
      uint32_t unknown7;
      uint32_t unknown8;
      cstring_t packet_type; // \0 terminated string
  };
  
  // Fields are little endian unless specified otherwise.
  struct header_partystate //Header for the "partystate" packet type
  {
      uint32_t unknown1;
      uint8_t unknown2;
      uint8_t player_entry_count;
      uint32_t unknown4;
      uint32_t unknown5;
      uint32_t unknown6;
      uint32_t unknown7;
      uint8_t unknown8;
      uint32_t unknown9;
      uint16_t unknown10;
      uint8_t unknown11;
      uint8_t unknown12[9];
      uint32_t unknown13;
      uint32_t unknown14;
      uint16_t unknown15;
      uint16_t unknown16;
      uint32_t unknown17[10];
      uint32_t unknown18;
      uint32_t unknown19;
      uint8_t unknown20;
      uint32_t unknown21;
      uint32_t unknown22;
      uint32_t unknown23;
  };
  
  // Fields are little endian unless specified otherwise.
  struct player_entry
  {
      uint8_t player_id;
  
      // The following fields may not actually exist in the data if it's an empty entry.
      uint8_t unknown1[3];
      cstring_t player_name;
      uint32_t unknown2;
      uint64_t steam_id;
      uint32_t internal_ip;
      uint32_t external_ip;
      uint16_t unknown3;
      uint16_t unknown4;
      uint32_t unknown5;
      uint32_t unknown6;
      uint32_t unknown7;
      uint32_t unknown8;
      uint32_t unknown9;
      uint32_t unknown10;
      uint32_t unknown11;
      uint32_t unknown12;
      uint16_t unknown13;
      uint8_t unknown14[???];     // Appears to be a bit mask, sometimes the length is zero, sometimes it's one. (First entry is always zero?)
      uint8_t unknown15;
      uint32_t unknown16;
      uint16_t unknown17;
      uint8_t unknown18[???];     // Most of the time this is 4 bytes, other times it is 3 bytes.
  };
  </code></pre>
  
  <p>I recreated the packet header structure in my C# application like this:</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential, Pack=1)]
  struct PacketHeader
  {
      public UInt16 magic;
      public UInt16 packetSize;
      public UInt32 unknown1;
      public UInt32 unknown2;
      public UInt32 unknown3;
      public UInt32 unknown4;
      public UInt16 unknown5;
      public UInt16 unknown6;
      public UInt32 unknown7;
      public UInt32 unknown8;
      public String packetType;
  }
  </code></pre>
  
  <p>Then I tried to make a structure for the "partystate" header, but I got errors saying <code>fixed</code> keyword is unsafe:</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential, Pack=1)]
  struct PartyStateHeader
  {
      UInt32 unknown1;
      Byte unknown2;
      Byte playerEntryCount;
      UInt32 unknown4;
      UInt32 unknown5;
      UInt32 unknown6;
      UInt32 unknown7;
      Byte unknown8;
      UInt32 unknown9;
      UInt16 unknown10;
      Byte unknown11;
      fixed Byte unknown12[9];
      UInt32 unknown13;
      UInt32 unknown14;
      UInt16 unknown15;
      UInt16 unknown16;
      fixed UInt32 unknown17[10];
      UInt32 unknown18;
      UInt32 unknown19;
      Byte unknown20;
      UInt32 unknown21;
      UInt32 unknown22;
      UInt32 unknown23;
  }
  </code></pre>
  
  <p>I couldn't do anything for the player entries because of the varying size of <code>unknown14</code> and <code>unknown18</code>. (Player entries are the most important.)</p>
  
  <p>Now, somehow, I have to cast the <code>byte[]</code> I have to these <code>PacketHeader</code> structures. Sadly, it's not easy as <code>(PacketHeader)bytes</code>. I tried this method I've found on the internet but it threw an <code>AccessViolationException</code>:</p>
  
  <pre><code>GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  PacketHeader packetHeader = (PacketHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(PacketHeader));
  </code></pre>
  
  <p>How can I achieve this?</p>