Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


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

DEVMODE (Structures)
 
.
Summary
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)]
    [System.Runtime.InteropServices.FieldOffset(0)]
    public string dmDeviceName;
    [System.Runtime.InteropServices.FieldOffset(32)]
    public Int16 dmSpecVersion;
    [System.Runtime.InteropServices.FieldOffset(34)]
    public Int16 dmDriverVersion;
    [System.Runtime.InteropServices.FieldOffset(36)]
    public Int16 dmSize;
    [System.Runtime.InteropServices.FieldOffset(38)]
    public Int16 dmDriverExtra;
    [System.Runtime.InteropServices.FieldOffset(40)]
    public DM dmFields;

    [System.Runtime.InteropServices.FieldOffset(44)]
    Int16 dmOrientation;
    [System.Runtime.InteropServices.FieldOffset(46)]
    Int16 dmPaperSize;
    [System.Runtime.InteropServices.FieldOffset(48)]
    Int16 dmPaperLength;
    [System.Runtime.InteropServices.FieldOffset(50)]
    Int16 dmPaperWidth;
    [System.Runtime.InteropServices.FieldOffset(52)]
    Int16 dmScale;
    [System.Runtime.InteropServices.FieldOffset(54)]
    Int16 dmCopies;
    [System.Runtime.InteropServices.FieldOffset(56)]
    Int16 dmDefaultSource;
    [System.Runtime.InteropServices.FieldOffset(58)]
    Int16 dmPrintQuality;

    [System.Runtime.InteropServices.FieldOffset(44)]
    public POINTL dmPosition;
    [System.Runtime.InteropServices.FieldOffset(52)]
    public Int32 dmDisplayOrientation;
    [System.Runtime.InteropServices.FieldOffset(56)]
    public Int32 dmDisplayFixedOutput;

    [System.Runtime.InteropServices.FieldOffset(60)]
    public short dmColor;
    [System.Runtime.InteropServices.FieldOffset(62)]
    public short dmDuplex;
    [System.Runtime.InteropServices.FieldOffset(64)]
    public short dmYResolution;
    [System.Runtime.InteropServices.FieldOffset(66)]
    public short dmTTOption;
    [System.Runtime.InteropServices.FieldOffset(68)]
    public short dmCollate;
    [System.Runtime.InteropServices.FieldOffset(72)]
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
    public string dmFormName;
    [System.Runtime.InteropServices.FieldOffset(102)]
    public Int16 dmLogPixels;
    [System.Runtime.InteropServices.FieldOffset(104)]
    public Int32 dmBitsPerPel;
    [System.Runtime.InteropServices.FieldOffset(108)]
    public Int32 dmPelsWidth;
    [System.Runtime.InteropServices.FieldOffset(112)]
    public Int32 dmPelsHeight;
    [System.Runtime.InteropServices.FieldOffset(116)]
    public Int32 dmDisplayFlags;
    [System.Runtime.InteropServices.FieldOffset(116)]
    public Int32 dmNup;
    [System.Runtime.InteropServices.FieldOffset(120)]
    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:

DM

DMCOLLATE

struct POINTL
{
    public Int32 x;
    public Int32 y;
}

Notes:

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: http://www.microsoft.com/indonesia/msdn/pinvoke.aspx, 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:

DEVMODE d = new DEVMODE();
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 definition

public short dmCollate;

your can also use

public DMCOLLATE dmCollate;

See the DMCOLLATE enum 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);

...

http://nicholas.piasecki.name/blog/2008/11/programmatically-selecting-complex-printer-options-in-c-shar/

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
DEVMODE on MSDN

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 PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions