Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
DEVMODE (Structures)
Contains information about the initialization and environment of a printer or a display device.

C# Definition:

[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
struct DEVMODE
    public const int CCHDEVICENAME = 32;
    public const int CCHFORMNAME = 32;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
    public string dmDeviceName;
    public Int16 dmSpecVersion;
    public Int16 dmDriverVersion;
    public Int16 dmSize;
    public Int16 dmDriverExtra;
    public DM dmFields;

    Int16 dmOrientation;
    Int16 dmPaperSize;
    Int16 dmPaperLength;
    Int16 dmPaperWidth;
    Int16 dmScale;
    Int16 dmCopies;
    Int16 dmDefaultSource;
    Int16 dmPrintQuality;

    public POINTL dmPosition;
    public Int32 dmDisplayOrientation;
    public Int32 dmDisplayFixedOutput;

    public short dmColor; // See note below!
    public short dmDuplex; // See note below!
    public short dmYResolution;
    public short dmTTOption;
    public short dmCollate; // See note below!
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
    public string dmFormName;
    public Int16 dmLogPixels;
    public Int32 dmBitsPerPel;
    public Int32 dmPelsWidth;
    public Int32 dmPelsHeight;
    public Int32 dmDisplayFlags;
    public Int32 dmNup;
    public Int32 dmDisplayFrequency;

VB.NET Definition:

<StructLayout(LayoutKind.Sequential)> _
Public Structure DEVMODE
    Public Const CCHDEVICENAME As Integer = 32
    Public Const CCHFORMNAME As Integer = 32

    <MarshalAs(UnmanagedType.ByValTStr, SizeConst := CCHDEVICENAME)> _
    Public dmDeviceName As String
    Public dmSpecVersion As Short
    Public dmDriverVersion As Short
    Public dmSize As Short
    Public dmDriverExtra As Short
    Public dmFields As DM

    Public dmOrientation As Short
    Public dmPaperSize As Short
    Public dmPaperLength As Short
    Public dmPaperWidth As Short

    Public dmScale As Short
    Public dmCopies As Short
    Public dmDefaultSource As Short
    Public dmPrintQuality As Short
    Public dmColor As Short
    Public dmDuplex As Short
    Public dmYResolution As Short
    Public dmTTOption As Short
    Public dmCollate As Short
    <MarshalAs(UnmanagedType.ByValTStr, SizeConst := CCHFORMNAME)> _
    Public dmFormName As String
    Public dmLogPixels As Short
    Public dmBitsPerPel As Integer ' Declared wrong in the full framework
    Public dmPelsWidth As Integer
    Public dmPelsHeight As Integer
    Public dmDisplayFlags As Integer
    Public dmDisplayFrequency As Integer

    Public dmICMMethod As Integer
    Public dmICMIntent As Integer
    Public dmMediaType As Integer
    Public dmDitherType As Integer
    Public dmReserved1 As Integer
    Public dmReserved2 As Integer
    Public dmPanningWidth As Integer
    Public dmPanningHeight As Integer

    Public dmPositionX As Integer ' Using a PointL Struct does not work
    Public dmPositionY As Integer
End Structure

User-Defined Types:


struct POINTL
    public Int32 x;
    public Int32 y;


C# add:

using System.Runtime.InteropServices;

VB.NET add:

Imports System.Runtime.InteropServices


I couldn't use that structure to attach secondary monitor properly.

Instead I've used structure from:, which works for me fine. I suppose that dmPositionX and dmPositionY should be placed after dmFields.

The C# new structure allow to work with secondary monitor. In intitial structure fields order and alignment was wrong.

Tips & Tricks:

Since the DEVMODE members beyond dmDisplayFrequency do not have to be declared, the structure can vary in size. You should set dmSize to effective size of your implemetation before calling API functions:

d.dmSize = Marshal.SizeOf(typeof(DEVMODE));

This way the API is informed of the version of DEVMODE used.

If you use Reflector, you can find Microsoft's managed version of the structure.

As of .net 3.5 the DEVMODE structure is declared incorrectly causing incorrect values to be returned after dmBitsPerPel. The structures above have been updated to reflect the correct implementation.

POINTL was incorrectly declared to use longs instead of ints, this has been fixed.

Instead of the definitions

public short dmColor;
public short dmDuplex;
public short dmCollate;

your can also use

public DMCOLOR dmColor;
public DMDUP dmDuplex;
public DMCOLLATE dmCollate;

See the DMCOLOR, DMDUP and DMCOLLATE enums for more details.

Sample Code:

Please add some!



  [DllImport("kernel32.dll", ExactSpelling = true)]
  public static extern IntPtr GlobalSize(IntPtr handle);


  devMode = (DEVMODE)Marshal.PtrToStructure(pDevMode, typeof(DEVMODE));
  devMode.dmSize = (short)Marshal.SizeOf(devMode);
  int isize = GlobalSize(hDevMode).ToInt32() - (int)devMode.dmSize;
  devMode.dmDriverExtra = Convert.ToInt16(isize);


Alternative Managed API:

Do you know one? Please contribute it!


Please edit this page!

Do you have...

  • helpful tips?
  • corrections to the existing content?
  • alternate definitions?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing any supporting types needed.

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version