System.Drawing.Printing.PrintDocument.PrinterSettings.GetHdevmode() returns some garbage

  • Thread starter Thread starter valentin tihomirov
  • Start date Start date
V

valentin tihomirov

When I OpenPrinter() with defaults set to the result GetHdevmode() result,
it fails with 1804 (The specified datatype is invalid) error:

DEVMODE *pDevMode = (DEVMODE*) pd.PrinterSettings.GetHdevmode();
try {
IntPtr hPrinter;
Win32Check(OpenPrinter( printerName, out hPrinter, new
IntPtr(pDevMode)), "Opening printer");



The inspection of the returned devmode structure shows it is nonsense. See
yourself:
for (int i = 0; i < sizeof(DEVMODE) ; i++)
Console.Write(" " + ((byte*)pDevMode));

Output:
184 35 23 0 0 0 211 0 0 0 0 0 32 0 211 0 0 0 0 0 40 0 211 0 0 0 0 0 48 0
211 0
0 0 0 0 56 0 211 0 0 0 0 0 64 0 211 0 0 0 0 0 72 0 211 0 0 0 0 0 80 0 211 0
0 0
0 0 88 0 211 0 0 0 0 0 96 0 211 0 0 0 0 0 104 0 211 0 0 0 0 0 112 0 211 0 0
0 0
0 120 0 211 0 0 0 0 0 128 0 211 0 0 0 0 0 136 0 211 0 0 0 0 0 144 0 211 0 0
0 0
0 152 0 211 0 0 0 0 0 160 0 211 0 0 0 0 0 168 0 211 0 0 0 0 0 176 0 211 0 0
0 0
0 184 0 211 0 0 0 0 0 192 0 211 0 0 0 0 0 200 0 211 0 0 0 0 0

I wanted the .Net to generate the pritner configuration and use it to for
Win32's OpenPrinter().
 
I have discovered that the pointer to DEVMODE can be obtained from the
handle by GlobalLock. I totally miss the concept of the microsoft
documentation. People must have extrasensory capabilities to derive that
from MSDN.
 
Back
Top