title: Marshalling C# structure to C++ Using StructureToPtr
body: <p>I have C# class: </p>
  
  <pre><code>namespace Models
  {
      [StructLayout(LayoutKind.Explicit, Size = 120, CharSet = CharSet.Unicode)]
      public struct DynamicState
      {
          [FieldOffset(0)]
          public double[] Position;
  
          [FieldOffset(24)]
          public double[] Velocity;
  
          [FieldOffset(48)]
          public double[] Acceleration;
  
          [FieldOffset(72)]
          public double[] Attitude;
  
          [FieldOffset(96)]
          public double[] AngularVelocity;
      }
  }
  </code></pre>
  
  <p>and C++/CLI method:</p>
  
  <pre><code>Models::DynamicState SomeClassClr::DoSomething(Models::DynamicState ds)
  {
      int struct_size = Marshal::SizeOf(ds);
      System::IntPtr ptr = Marshal::AllocHGlobal(struct_size);
      DynamicStateStruct ds_struct;
      struct_size = sizeof(ds_struct);
  
      Marshal::StructureToPtr(ds, ptr, false);
      ds_struct = *(DynamicStateStruct*)ptr.ToPointer();
  
      Models::DynamicState returnVal;
      mpSomeClass-&gt;doSomething(ds_struct);
  
      return returnVal;
  }
  </code></pre>
  
  <p>where DynamicStateStruct is a native C++ class:</p>
  
  <pre><code>struct DynamicStateStruct
  {
      double mPosition[3];
      double mVelocity[3];
      double mAcceleration[3];
      double mAttitude[3];
      double mAngularVelocity[3];
  };
  </code></pre>
  
  <p>When I recover the struct (<code>ds_struct</code>) in native C++ I am not getting the correct values, any ideas with what I am missing?</p>