在 .NET 中打印 PrintDocument 对象时出现奇怪的错误

发布于 2024-07-26 22:29:20 字数 1733 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹤舞 2024-08-02 22:29:20

确保用户选择了默认打印机。 我遇到了许多与打印机相关的不稳定错误,这些错误最终都是由没有默认打印机引起的。

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.

风吹短裙飘 2024-08-02 22:29:20

使用混淆器后我遇到了这个错误。 (但没有混淆任何有关打印的代码。)

在挖掘了一些反编译的.NET代码等之后,我找到了一个解决方案:

只需添加一个虚拟代码来读取当前计算机的打印机设置,然后再显示任何打印对话框等。

这是我推荐的代码:

Console.Write(PrintSettings.InstalledPrinters);

我认为这是一个 .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:

Console.Write(PrintSettings.InstalledPrinters);

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

萌梦深 2024-08-02 22:29:20

我遇到了一个非常相似的错误:

    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()

问题原来是打印驱动程序有故障。 我重新安装后工作正常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文