title: Reading Serialized MFC CArray in C#
body: <p>MFC CArray was Serialized and saved to a database. I need to read this data into a C# project. I am able to retrieve the data as byte[] from the database. I then write the byte[] to a MemoryStream. Now I need to read the data from the MemoryStream.</p>
  
  <p>Someone has apparently solved this before, but did not write their solution.</p>
  
  <pre><code>http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/17393adc-1f1e-4e12-8975-527f42e5393e
  </code></pre>
  
  <p>I followed these projects in my attempt to solve the problem.</p>
  
  <p><a href="http://www.codeproject.com/Articles/32741/Implementing-MFC-Style-Serialization-in-NET-Part-1" rel="nofollow">http://www.codeproject.com/Articles/32741/Implementing-MFC-Style-Serialization-in-NET-Part-1</a></p>
  
  <p><a href="http://www.codeproject.com/Articles/32742/Implementing-MFC-Style-Serialization-in-NET-Part-2" rel="nofollow">http://www.codeproject.com/Articles/32742/Implementing-MFC-Style-Serialization-in-NET-Part-2</a></p>
  
  <p>The first thing in the byte[] is the size of the array, and I can retrieve that with binaryReader.readInt32(). However, I cannot seem to get back the float values. If I try binaryReader.readSingle() or</p>
  
  <pre><code>public void Read(out float d) {
      byte[] bytes = new byte[4];
      reader.Read(bytes, m_Index, 4);
      d = BitConverter.ToSingle(bytes, 0);
  }
  </code></pre>
  
  <p>I do not get back the correct data. What am I missing?</p>
  
  <p><strong>EDIT</strong> Here is the C++ code that serializes the data</p>
  
  <pre><code>typedef CArray&lt;float, float&gt; FloatArray;
  FloatArray floatArray;
  // fill floatArray
  CSharedFile memoryFile(GMEM_MOVEABLE | GMEM_ZEROINIT);
  CArchive ar(&amp;memoryFile, CArchive::store); 
  floatArray.Serialize(ar);
  ar.Close();
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>By reading backward, I was able to get all of the floats, and was also able to determine that the size for CArray is byte[2], or Int16. Does anyone know if this is always the case?</p>