Crash when showing print setup dialog from Windows forms

Crash when showing print setup dialog from Windows forms

Post by WilS » Thu, 15 Oct 2009 08:38:10

I have a simple Windows Forms app that has printing support. The app
uses System.Drawing.Printing.PrintDocument support to launch the print
dialog, and subsequently print. On several machines, this all works fine.
But on a couple of machines, it crashes itermittantly, and on a couple more
machines, it crashes pretty consistently. The errors/exceptions I get vary
from <poof> app disapperas, to OutOfMemoryException to native access
violation (0xC0000005), and even occasionally StackOverflowException. My
thinking is that there must be a bug in .NET 3.5 SP1 in the print wrapper
stuff (System.Drawing.Printing.PrintDocument) that is effectively a wild
write which manifests itself differently at different times and on different

Here's the basic code that I'm trying to run (omittng the basic Form that
hosts my 'print' button)

public Form1()
m_PageSettings = new PageSettings();
m_PrinterSettings = new PrinterSettings();
printDocument1.DefaultPageSettings = m_PageSettings;
printDocument1.PrinterSettings = m_PrinterSettings;
printDocument1.PrintPage += new

private PageSettings m_PageSettings;
private PrinterSettings m_PrinterSettings;

private void button1_Click(object sender, EventArgs e)
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)

It crashes the first time you load it up, hit the print button, and select
(in my case) the Microsoft XPS Document Writer printer. If you're trying to
debug, it often takes out Visual Studio altogether. (Note that
m-PrintDoc_PrintPage never gets called - the crash occurs before it gets to
any of my actual printing logic.)

One other test case: I have a Windows 7 32-bit machine that I Remote
Desktop into for testing. I see the failure when I use the 'local print'
resources (so my local printers are "exported" to the Windows7 machine), but
I don't see the failure if I don't export the printers. (Both the host and
target machines have the MS XPS Document printer.) So perhaps having a
longer list of installed printers or a particular list of installed printers
contributes to the bug?

Does anybody have any suggestions about what this might be, or anything to
try to work around this? I'm very suspicious that the CopyToHdevmode call
is the one crashing, as this is the function that copies the managed printer
setup state into the native DEVMODE. Could this function have a wild write


Here's a callstack from one of the crashes where VS didn't get killed:

ntdll.dll!_RtlpAnalyzeHeapFailure@12() + 0x20a bytes
ntdll.dll!_RtlpCoalesceFreeBlocks@16() + 0x349a2 bytes
ntdll.dll!@RtlpFreeHeap@16() + 0x10b bytes
ntdll.dll!_RtlFreeHeap@12() + 0xe37 bytes
ntdll.dll!_RtlDebugFreeHeap@12() + 0x1f9 bytes
ntdll.dll!@RtlpFreeHeap@16() + 0x34d58 bytes
ntdll.dll!_RtlFreeHeap@12() + 0xe37 bytes
rpcrt4.dll!FreeWrapper() + 0x35 bytes
rpcrt4.dll!operator delete() + 0xe bytes
rpcrt4.dll!DCE_BINDING::`scalar deleting destructor'() + 0x2c bytes

1. Printing without showing the printing dialog window

2. Word MailMerge does not show print setup dialog


I succesfully used MailMerge to create documents filled with data. But
since Office XP, the client have lost the ability to select a printer of their choic
because the print dialog does not show up

The "engine" works with some template documents, and a RTF document containing the data

So, my question is, how can I force Word to show the print dialog when the Destionation of the
MailMerge document is the printer

I tried to make a new document with the results of the mail merge, but, unfortunately, the only
way to get a print dialog is to execute the document's PrintPreview method, and my clients does no
accept this

Can you help me

Thanks in advance

3. How do I print my form without the dialog box showing

4. IE8 Print->Print->Page Setup doesn't open dialog

5. windows XP setup freezes during first appearing of setup-dialog (blue-screen)

6. Cannot press OK in Windows Print Dialog / How can i print in Windows ?

7. printing a file created by the windows printing dialog (print

8. printing a file created by the windows printing dialog (print to f

9. Print link that doesn't show the print dialog

10. Showing the print dialog without printing when pressing ok?

11. the print dialog box does not show up when i click the print icon

12. print dialog box shows landscape but it prints in portrait

13. Cannot print without showing the print dialog