Recently when printing the 3rd time and up, you get this error in many Delphi programs and the Delphi IDE:
In the past this only occurred when you used a TPrinter
and forgot to call BeginDoc
.
But now it always occurs after reusing the same TPrinter
instance for the 3rd time and up. Since the Delphi Galileo based IDEs (8 and higher; likely older ones as well: the source code printing hasn’t changed in a long time). The error actually occurs twice: after starting a source code print job, but also after cancelling the same failed source code print job.
The second error stroke me as odd, so I went searching for “printer is not currently printing” “IDE” leading to this stack overflow question: c++builder – Why is TPrinter (XE7) suddenly having problems today? – Stack Overflow [WayBack].
The pattern there is using the
Printer()
function which has been the way the (un)official code examples have shown for ages (Delphi 2007 Printers.Printer Function [WayBack]; earlier examples like Delphi 7 [WayBack] usually in PDF files).Like in the Delphi 7 “5-32 Developer’s Guide” page example:
procedure TForm1.Button1Click(Sender: TObject); var r: TRect; i: Integer; begin with Printer do begin r := Rect(200,200,(Pagewidth - 200),(PageHeight - 200)); BeginDoc; Canvas.Brush.Style := bsClear; for i := 0 to Memo1.Lines.Count do Canvas.TextOut(200,200 + (i * Canvas.TextHeight(Memo1.Lines.Strings[i])), Memo1.Lines.Strings[i]); Canvas.Brush.Color := clBlack; Canvas.FrameRect(r); EndDoc; end; end;(Yes, that’s back in the D7 days when examples were still using with and not using try/finally statements for resource cleanup).
Actual cause and permanent fix
The printing problems are caused by various recent Windows updates part of MS16-098:
- KB3177725 MS16-098 may cause printer problems – Microsoft Community
- KB3176493 for Windows 10 and KB3177725 for Windows 7 broke printing in – Microsoft Community
Though MS16-098: Security update for Windows kernel-mode drivers: August 9, 2016 mentions the issue without a fix, KB3177725 in MS16-098: Description of the security update for Windows kernel-mode drivers: August 9, 2016 mentions both the issue and a permanent fix:
After you apply this security update and you print multiple documents in succession, the first two documents may print successfully. However, the third and subsequent documents may not print.
To resolve this issue, install update 3187022. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
3187022 Print functionality is broken after any of the MS16-098 security updates are installed
This article describes printing issues that occur after any of the security updates that are described in Microsoft Security Bulletin MS16-098 are installed in Windows. You can fix these issues by installing the update that is described in this article. Before you install this update, check out the Prerequisites section.
This update applies to the following operating systems:
- Windows Server 2012 R2
- Windows 8.1
- Windows RT 8.1
- Windows Server 2012
- Windows Server 2008 R2 Service Pack 1 (SP1)
- Windows 7 SP1
- Windows Server 2008 Service Pack 2 (SP2)
- Windows Vista SP2
No solution for Windows 10 yet…
Until you install the fix: workarounds
For your own code (Thanks Remy Lebau for your answer), add this code for your BeginDoc
call:
MyPrinter.Copies := MyPrinter.Copies;
You might want to keep including this in your code as you’re never sure when the end-users apply which Windows update.
For the Delphi IDE either:
- Press the “Setup…” button in the “Print Selection” dialog when printing source code, then “OK” in the “Print Setup” dialog:
- Uninstall the security updated marked in blue (Security Update for Microsoft Windows (KB3177725):
Security Update for Microsoft Windows (KB3177725)
–jeroen