title: Type cannot be marshaled as an unmanaged structure
body: <p>I'm trying to change the resolution in a core project. </p>
  
  <pre><code>var devmode = default(Devmode);
  devmode.DmDeviceName = new string(new char[32]);
  devmode.DmFormName = new string(new char[32]);
  devmode.Dmsize = (short)Marshal.SizeOf(devmode);
  </code></pre>
  
  <p>On the 3rd line of the above code, it fails and produces a "cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed" error.</p>
  
  <p>Can anyone provide some insight into how I should go about fixing this? The weird thing is it was working fine before I pulled some other code that shouldn't have effected this code.</p>
  
  <p>Struct:</p>
  
  <pre><code>    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  public struct Devmode
  {
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
      public string DmFormName;
      public string DmDeviceName;
  
      public short Dmsize;
  
      public int DmPelsWidth;
      public int DmPelsHeight;
  
      private readonly short dmlogPixels;
      private readonly short dmbitsPerPel;
      private readonly int dmdisplayFlags;
      private readonly int dmdisplayFrequency;
      private readonly int dmiCMMethod;
      private readonly int dmiCMIntent;
      private readonly int dmmediaType;
      private readonly int dmditherType;
      private readonly int dmreserved1;
      private readonly int dmreserved2;
      private readonly int dmpanningWidth;
      private readonly int dmpanningHeight;
  
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
      private readonly short dmspecVersion;
      private readonly short dmdriverVersion;
      private readonly short dmdriverExtra;
      private readonly int dmfields;
      private readonly int dmpositionX;
      private readonly int dmpositionY;
      private readonly int dmdisplayOrientation;
      private readonly int dmdisplayFixedOutput;
      private readonly short dmcolor;
      private readonly short dmduplex;
      private readonly short dmyResolution;
      private readonly short dmtTOption;
      private readonly short dmcollate;
  }
  </code></pre>
  
  <p>EDIT: I've now changed the code to this:</p>
  
  <pre><code>        [StructLayout(LayoutKind.Sequential)]
      public struct Devmode
      {
          [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
  
          private readonly string dmdeviceName;
  
          private readonly short dmspecVersion;
          private readonly short dmdriverVersion;
  
          public short Dmsize;
  
          private readonly short dmdriverExtra;
          private readonly int dmfields;
          private readonly int dmpositionX;
          private readonly int dmpositionY;
          private readonly int dmdisplayOrientation;
          private readonly int dmdisplayFixedOutput;
          private readonly short dmcolor;
          private readonly short dmduplex;
          private readonly short dmyResolution;
          private readonly short dmtTOption;
          private readonly short dmcollate;
          [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
          private readonly string dmformName;
          private readonly short dmlogPixels;
          private readonly short dmbitsPerPel;
  
          public int dmpelsWidth;
          public int dmpelsHeight;
  
          private readonly int dmdisplayFlags;
          private readonly int dmdisplayFrequency;
          private readonly int dmiCMMethod;
          private readonly int dmiCMIntent;
          private readonly int dmmediaType;
          private readonly int dmditherType;
          private readonly int dmreserved1;
          private readonly int dmreserved2;
          private readonly int dmpanningWidth;
          private readonly int dmpanningHeight;
      }
  </code></pre>
  
  <p>However I am getting an error for dmpelsWidth, dmpelsHeight and Dmsize. 'Public' members must come before 'private' members...</p>
  
  <p>Any ideas?</p>