I'm looking into the possibility of persisting data by putting it into a structure and then copying the structure (via the marshal class) to a byte array which is then output to the file.

I'm using the following code:
VB Code:
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class Form1
  4.  
  5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  6.         Dim a As test
  7.         Dim b() As Byte
  8.  
  9.  
  10.         a.a = "testing"
  11.  
  12.         ReDim b(Marshal.SizeOf(a))
  13.  
  14.         Dim ptr As GCHandle = GCHandle.Alloc(b, GCHandleType.Pinned)
  15.  
  16.         Marshal.StructureToPtr(a, ptr.AddrOfPinnedObject, True)
  17.     End Sub
  18. End Class
  19.  
  20. <StructLayout(LayoutKind.Sequential)> _
  21. Public Structure test
  22.     Public a As String
  23. End Structure

What I'm getting, however, is b()'s length is set to 5 and only the first three elements are use and contain different values each time. Why isn't the actual contents of the structure contained here? Is there a correct way of doing this? Am I not understanding what GCHandle.Alloc and Marshal.StructureToPtr do?