Search code examples
windows-servicesactivereports

ActiveReports 7 throws invalid handle exception printing from Windows Service


I have and application that is running as a windows service on Server 2008. When I run it as an application and send a print job, the print job gets processed sucessfully. However, if I send the print job to the windows service instead, it throws "The Handle is invalid" exception.

Here is the error:

Printing Thread error: The handle is invalid. StackTrace:    at System.Drawing.Printing.StandardPrintController.OnStartPrint(PrintDocument document, PrintEventArgs e)   at System.Drawing.Printing.PrintController.Print(PrintDocument document)   at System.Drawing.Printing.PrintDocument.Print()   at GrapeCity.ActiveReports.Viewer.Win.Printing.PrintImpl.DocumentPrinter.<Print>b__1e()

The service is running under an administrator account which has admin permissions.

Any help would be appreciated!


Solution

  • A coule things to check:

    1. Make sure that the user account that the service is running under has the proper printer installed under his account and that it is set as the default printer.
    2. Make sure you're using the Print method in ActiveReports that explicitly does not show any UI.
    3. Try checking the "Allow service to interact with desktop" checkbox on the service's property page from the Services control panel applet. Although you may not be showing any UI, the printer driver may need that capability.

    Finally, just keep in mind that in general printing from a Windows Service is kind of flaky. Even Microsoft's documentation for System.Drawing.Printing has the following disclaimer:

    Classes within the System.Drawing.Printing namespace are not supported for use within a Windows service or ASP.NET application or service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.

    That doesn't mean it won't work, but that it is error-prone enough that Microsoft doesn't want to help us printing in this case. I suspect this is because the printer drivers from the manufacturers are not necessarily designed for printing from non-UI processes like windows services.