none
GetHdevmode and DEVMODE struct RRS feed

  • Question

  • Hi,

     

    We're having a strange problem with the GetHdevmode method of the PrinterSettings class.

    The following does not seem to work:

     

    Code Snippet

    PrinterSettings myPrinterSettings1 = new PrinterSettings();

    myPrinterSettings1.PrinterName = "Kyocera FS-9000";

    PageSettings PageSettings1 = new PageSettings(myPrinterSettings1);

    PageSettings1.PaperSource = myPrinterSettings1.PaperSources[2];

    IntPtr hDev1 = myPrinterSettings1.GetHdevmode(PageSettings1);

    DEVMODE structDev1 = (DEVMODE)Marshal.PtrToStructure(hDev1, typeof(DEVMODE));

     

     

    I'd expect the fields in structDev to be filled according to the PageSettings, however they're complete garbage. Actually what we want to do is to pass this handle to CreateDC, which also doesn't work - I suppose it's because the DEVMODE handle created by GetHdevmode is not compatible/valid. Any hints would be greatly appreciated.

     

    John

     

    PS:

    I'm using the following definition for DEVMODE:

     

    Code Snippet

    // Dev Mode String Sizes

    private const int CCDEVICENAME = 32;

    private const int CCFORMNAME = 32;

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]

    public struct DEVMODE

    {

    public const int CCHDEVICENAME = 32;

    public const int CCHFORMNAME = 32;

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

    public char[] dmDeviceName;

    public short dmSpecVersion;

    public short dmDriverVersion;

    public short dmSize;

    public short dmDriverExtra;

    public int dmFields;

    public short dmOrientation;

    public short dmPaperSize;

    public short dmPaperLength;

    public short dmPaperWidth;

    public short dmScale;

    public short dmCopies;

    public short dmDefaultSource;

    public short dmPrintQuality;

    public short dmColor;

    public short dmDuplex;

    public short dmYResolution;

    public short dmTTOption;

    public short dmCollate;

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

    public char[] dmFormName;

    public short dmLogPixels;

    public short dmBitsPerPel;

    public int dmPelsWidth;

    public int dmPelsHeight;

    public int dmDisplayFlags;

    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;

    public int dmPositionX; // Using a PointL Struct does not work

    public int dmPositionY;

    }

     

     

    Friday, April 18, 2008 8:50 AM

Answers

  • The value you get back from GetHdevMode is a handle to memory, obtained from GlobalAlloc().  You'd have to P/Invoke GlobalLock() to get the memory address, then use Marshal.PtrToStructure() to copy the data.

    These are all undocumented details that can break in the future.  Avoid calling CreateDC() directly, if you can.  Perhaps PrinterSettings.CreateMeasurementGraphics() can do the job.
    Friday, April 18, 2008 5:26 PM
    Moderator

All replies

  • The value you get back from GetHdevMode is a handle to memory, obtained from GlobalAlloc().  You'd have to P/Invoke GlobalLock() to get the memory address, then use Marshal.PtrToStructure() to copy the data.

    These are all undocumented details that can break in the future.  Avoid calling CreateDC() directly, if you can.  Perhaps PrinterSettings.CreateMeasurementGraphics() can do the job.
    Friday, April 18, 2008 5:26 PM
    Moderator
  • This should work on a 32-Bit machine:

    Code Snippet

     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
        public class DevMode
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            internal string dmDeviceName;           
            internal short dmSpecVersion;           
            internal short dmDriverVersion;         
            internal short dmSize;                  
            internal short dmDriverExtra;         
            internal int dmFields;                  
            internal short dmOrientation;          
            internal short dmPaperSize;             
            internal short dmPaperLength;          
            internal short dmPaperWidth;           
            internal short dmScale;                 
            internal short dmCopies;               
            internal short dmDefaultSource;        

            internal short dmPrintQuality;        
            internal short dmColor;               
            internal short dmDuplex;                
            internal short dmYResolution;           
            internal short dmTTOption;              

            internal short dmCollate;               
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            internal string dmFormName;         

            internal UInt16 dmLogPixels;            
            internal UInt32 dmBitsPerPel;           
            internal UInt32 dmPelsWidth;            
            internal UInt32 dmPelsHeight;           
            internal UInt32 dmNup;                  
            internal UInt32 dmDisplayFrequency;     
            internal UInt32 dmICMMethod;            
            internal UInt32 dmICMIntent;            
            internal UInt32 dmMediaType;            
            internal UInt32 dmDitherType;           
            internal UInt32 dmReserved1;            
            internal UInt32 dmReserved2;            
            internal UInt32 dmPanningWidth;         
            internal UInt32 dmPanningHeight;       
    }

     

     

    Saturday, April 19, 2008 7:53 PM