views:

640

answers:

5

I have created a Delphi Service which prints TQuickReports. Everything works fine if compiled and run as a Windows Application. But when converted to operate as a service trying to create a form containing a TQuickRep component throws the exception.

This service runs fine on many other boxes but not this one in particular. Here are some details:

  • Using QuickReport version 4.07
  • Box is a Windows Server 2008 operating system.
  • Using Delphi 2007
  • Printer.Printers.Count is returning a positive value. In fact I can list out all of the printers.
  • I have tried running the service both using Local System Account and Logged on as an Admin.
+4  A: 

Is there a default printer set up in session 0? Remember that under Vista / Server 2008 / Windows 7, services run in a separate session. Whether or not the logged-in user has a default printer set is not relevant - it's a per-session setting and doesn't affect session 0.

Can you rewrite the code to gracefully handle that exception and pick a printer to use?

Mihai Limbășan
This is new to me. No, I was not aware there were "sessions". How do I get a handle to a specific "Session"?
M Schenkel
+2  A: 

You might give the user a way to select the printer for the service. The Windows service probably does not have a default printer set.

Set TQuickRep.PrinterSettings.PrinterIndex to set the printer number. Then, TQuickRep.Print to print the report.

Marcus Adams
In fact this is EXACTLY what we do. Essentially the printer is specified by name and then we have a routine which will scan the Printer.Printers array and return the integer. Problem is I can't even get to this point; I can't even create a TQuickRep instance.
M Schenkel
@M Schenkel, seems like a bug, but the work around is probably to set the default printer prior to creating your TQuickRep object.
Marcus Adams
Can you post the code around where the error happens? We have implemented a variation on the solution suggested by Marcus Adams for printing Quick Report reports via a Windows Service and do not see the error that you do on Windows 2008.
Scott W
A: 

Let me guess ... the printer reporting this error is either a Dell or a Lexmark ...

mrw
It has nothing to do with the printer; I am not even getting to the point of printers.
M Schenkel
+1  A: 

A colleague ended up finding the solution. I should have added these are "network" printers and not Local printers (at the time I didn't think this was related to the problem). So the service needed to be installed with "NetworkService" as the user account under the logon tab. From the Windows Help:

To specify that the service uses the Network Service account, click This account, and then type NT AUTHORITY\NetworkService

M Schenkel
A: 

If using terminal services 2008, same user for multiple sessions, you should look into the HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\SessionDefaultDevices\Session_ID instead of HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows.

Akko