The following C# code has the goal of enabling managed code to call CreateDC, but it's incorrect.  Calling all Interop aficionados... Can you see what's wrong?

using System;

using System.Runtime.InteropServices;

internal class DeviceContext

{

  [DllImport("gdi32.dll", CharSet=CharSet.Auto)]

  internal static extern IntPtr CreateDC(

    string lpszDriver, string lpszDevice,

    string lpszOutput, ref DEVMODE lpInitData);

}

 

[StructLayout(LayoutKind.Sequential)]

internal class DEVMODE

{

  [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]

  public char [] dmDeviceName;

  public short dmSpecVersion;

  public short dmDriverVersion;

  public short dmSize;

  public short dmDriverExtra;

  public int dmFields;

  public DEVMODE_UNION u;

  public short dmColor;

  public short dmDuplex;

  public short dmYResolution;

  public short dmTTOption;

  public short dmCollate;

  [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]

  public byte [] dmFormName;

  public short dmLogPixels;

  public int dmBitsPerPel;

  public int dmPelsWidth;

  public int dmPelsHeight;

  public int dmDisplayFlagsOrdmNup;

  public int dmDisplayFrequency;

  public int dmICMMethod;

  public int dmICMIntent;

  public int dmMediaType;

  public int dmDitherType;

  public int dmReserved1;

  public int dmReserved2;

  public int dmPanningWidth;

  public int dmPanningHeight;

}

 

[StructLayout(LayoutKind.Explicit)]

internal struct DEVMODE_UNION

{

  [FieldOffset(0)]

  public short dmOrientation;

  [FieldOffset(2)]

  public short dmPaperSize;

  [FieldOffset(4)]

  public short dmPaperLength;

  [FieldOffset(6)]

  public short dmPaperWidth;

  [FieldOffset(8)]

  public short dmScale;

  [FieldOffset(10)]

  public short dmCopies;

  [FieldOffset(12)]

  public short dmDefaultSource;

  [FieldOffset(14)]

  public short dmPrintQuality;

 

  [FieldOffset(0)]

  public int dmPosition_x;

  [FieldOffset(4)]

  public int dmPosition_y;

  [FieldOffset(8)]

  public int dmDisplayOrientation;

  [FieldOffset(12)]

  public int dmDisplayFixedOutput;

}