在 .NET 中打印 PrintDocument 对象时出现奇怪的错误
我有一个 Winforms 应用程序,它生成自己的 PrintDocument 对象以进行打印。 经过我自己的测试,它在 XP 和 Windows 7 中运行良好,但一位用户在 Vista 中选择带有打开文档的打印命令时出现以下异常:
************** 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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保用户选择了默认打印机。 我遇到了许多与打印机相关的不稳定错误,这些错误最终都是由没有默认打印机引起的。
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.
使用混淆器后我遇到了这个错误。 (但没有混淆任何有关打印的代码。)
在挖掘了一些反编译的.NET代码等之后,我找到了一个解决方案:
只需添加一个虚拟代码来读取当前计算机的打印机设置,然后再显示任何打印对话框等。
这是我推荐的代码:
我认为这是一个 .NET 或 Windows 错误。
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:
I think this is a .NET or a Windows bug.
我遇到了一个非常相似的错误:
问题原来是打印驱动程序有故障。 我重新安装后工作正常。
I had a very similar error:
The problem turned out to be that the print driver was faulty. After I reinstalled it worked fine.