Why does my PrinterSettings.GetHdevmode() returns garbage ???

B

Benoit Courchesne

Why when I do this, I get garbage in my dm2 Structure ?

PrintDocument pd = new PrintDocument();
IntPtr hDevMode = pd.PrinterSettings.GetHdevmode();
IntPtr pDevMode = GlobalLock( hDevMode );
dm2 = (DEVMODE)Marshal.PtrToStructure(pDevMode,typeof(DEVMODE));
// dm2 gets garbage
// I tried with 3 different printers as default printers and I still get the
same result

But if i go with the API, I get good values

int intError = DocumentProperties(IntPtr.Zero,
hPrinter, printerName, IntPtr.Zero , ref Temp , 0);
IntPtr yDevModeData = Marshal.AllocHGlobal(intError);
intError = DocumentProperties(IntPtr.Zero, hPrinter,
printerName, yDevModeData , ref Temp , DM_OUT_BUFFER);
DEVMODE dm1 = (DEVMODE)Marshal.PtrToStructure(yDevModeData,
typeof(DEVMODE));
// dm1 is fine

here is the definition of my DEVMODE struct.

public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string 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.ByValTStr, SizeConst = 32)] public string
dmFormName;
public short dmUnusedPadding;
public short dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}

Thanks in advance,

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top