views:

342

answers:

3

I have a Winforms application that generates its own PrintDocument object for printing. It works fine in XP and Windows 7 with my own testing, but one user has been getting the following exception in Vista when they select the print command with an open document:

************** Exception Text **************
System.ArgumentException: Handle 0 is not valid.
   at System.Drawing.Printing.PrinterSettings.SetHdevmode(IntPtr hdevmode)
   at System.Windows.Forms.PrintDialog.UpdatePrinterSettings(IntPtr hDevMode, IntPtr hDevNames, Int16 copies, Int32 flags, PrinterSettings settings, PageSettings pageSettings)
   at System.Windows.Forms.PrintDialog.ShowPrintDialog(IntPtr hwndOwner, WndProc hookProcPtr, PRINTDLG data)
   at System.Windows.Forms.PrintDialog.RunDialog(IntPtr hwndOwner)
   at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.CommonDialog.ShowDialog()
   at Deck_Studio.DSMain.PrintDeck(Object sender, EventArgs e)
   at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.MenuItem.MenuItemData.Execute()
   at System.Windows.Forms.Command.Invoke()
   at System.Windows.Forms.Command.DispatchID(Int32 id)
   at System.Windows.Forms.Control.WmCommand(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

I'm having a hard time piecing out the nature of this error and any potential ways of duplicating this. The manifest on the executable is in the "limited" permissions mode and even having the user run the application as an administrator generates the error.

+4  A: 

Make sure the user has a default printer selected. I've run into a lot of erratic printer-related bugs that were ultimately caused by no default printer.

MusiGenesis
Yes, I'll bet that's it.
Henk Holterman
+1  A: 

I've got this error after using an obfuscator. (but didn't obfuscate any code about printing.)

After digging some decompiled .NET code etc. I've found a solution:

Just add a dummy code that reads the printer settings of current computer, before displaying any printdialog etc.

Here is the code that I'll recommend:

Console.Write(PrintSettings.InstalledPrinters);

I think this is a .NET or a Windows bug.

ahmkara
+1  A: 

I had a very similar error:

    Error Description: Handle 0 is not valid.

Error Stack Trace:
   at System.Drawing.Printing.PrinterSettings.SetHdevmode(IntPtr hdevmode)
   at System.Windows.Forms.PrintDialog.UpdatePrinterSettings(IntPtr hDevMode, IntPtr hDevNames, Int16 copies, Int32 flags, PrinterSettings settings, PageSettings pageSettings)
   at System.Windows.Forms.PrintDialog.ShowPrintDialog(IntPtr hwndOwner, WndProc hookProcPtr, PRINTDLG data)
   at System.Windows.Forms.PrintDialog.RunDialog(IntPtr hwndOwner)
   at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.CommonDialog.ShowDialog()

The problem turned out to be that the print driver was faulty. After I reinstalled it worked fine.

grahamesd