3

I would like to print from a Windows Service in C# using Visual Studio 2010. So far, I have only been partially successful by using the code found here:

Print html document from Windows Service without print dialog

The current problem I am experiencing is two fold, which I have only discovered by allowing the service to interact with the desktop:

1) When the URL is entered in as the HTML location, it will request credentials for that location (a network shared IIS web server that I DO NOT have the ability to alter in any way, which requests a username and password to access the information).

2) After I enter credentials, the default printer is ALWAYS the XPS printer, no matter what I do to SetDefaultPrinter (two different ways I have found to do this). I have discovered this is because the printer I want to print to is also a network printer, which the Local System account cannot access (apparently). I have not found a way around this problem.

I would like to get around both of these problems by entering the credentials needed programmatically (this is a network share credential set) and then print to a network printer. In other desktop applications I have successfully printed to each of these network printers even though I do not have each one installed.

Attempting to log in using my credentials instead of the Local System account does not solve either problem. I am using Windows XP.

Assuming I get this to work, I will also need to install the service on another server, so if it is necessary to preinstall each printer I will need to know what steps to take to ensure the Local System account has access to them. The server I will install on will likely be a Windows Server 2003 or 2008, but will have the same network access as the development machine.

EDIT: I attempted a solution of having it Log on as other accounts, all of which should have credentials (and, in fact, if those credentials are entered when it is running as Local System with desktop interaction, those credentials are accepted). Each user that this has been attempted with has had the default printer set to the printer I am trying to test print to, but no print occurs. The only time I know for sure it is trying to use the XPS printer is when it is logged on as Local System with desktop interaction.

Community
  • 1
  • 1
TomT64
  • 76
  • 1
  • 5
  • Running Internet Explorer as Local System can only end in pain and bloodshed. – Luke Apr 24 '12 at 20:08
  • Luke - is there another way to get the data, as Local System, and still print to a network printer? So far, running it as an authenticated user does not allow printing. – TomT64 Apr 25 '12 at 12:53

2 Answers2

2

Don't run the service as Local System. It doesn't have the rights you need to access network resources. Create a user and run the service under that, if you're in a domain, make sure it's a domain user and not a local one. You will need to log on as that user and install the printers you want to use on the computer where your service runs. Also, if the IIS application is using Windows Authentication, see if the account you're running the service under can be granted permission to access the web application.

JamieSee
  • 12,696
  • 2
  • 31
  • 47
  • I tried the different user approach, with existing users that have proper credentials, and it still did not print. There is no way for me to choose the interact with desktop option, if I log in as a different user, to see if I am experiencing the same two problems. – TomT64 Apr 24 '12 at 17:50
  • What version of Windows is this running on? There are some indications that ShellExecute from a service could be problematic in Vista and later. See this thread http://social.msdn.microsoft.com/Forums/en-US/windowscompatibility/thread/716cb8fa-c2ca-45ba-ad2e-b71b32b2129b/ – JamieSee Apr 24 '12 at 21:55
  • I am using Windows XP for the development, and the service may later be deployed on Windows Server 2003 or 2008. – TomT64 Apr 25 '12 at 01:55
  • Your answer made my day! I needed to login as the account running the service (in my case, a Windows Service) and install the printer. It worked like magic. Thank you. – Nelson Reis May 06 '15 at 11:52
0

I had this issue, this trick solved it

Go to services ---> Double click the required service ---> proceed to logon tab

Supply the Log-in credentials from which printer was installed.

See screen shot below.

enter image description here

Edwin O.
  • 4,998
  • 41
  • 44