Question

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!

Was it helpful?

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.

OTHER TIPS

Roussul

It could be a permission or printer access problem. To check whether it is related to ActiveReports or not, can you create a separate app, implement system.printing.drawing and run it as a service, just as you have it with ActiveReports and let us know.

I had this issue in a ASP.NET application. The root cause was that "Enable 32-bit applications" had been set in the IIS application pool that the website was running in.

It appears the print function tried to connect to a 32 bit driver rather than the 64 bit drivers that were installed.

Maybe in a .NET Windows app the "Target CPU" setting could also cause a similar issue? ie if you target x86 and try and run your app on x64 it will only connect to 32bit printer drivers?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top