Reputation: 338
I have a C# console application that gets an HTML page via an API call and prints the document using SHDocVw.InternetExplorer & ExecWB. When I run the application directly (double click) everything runs as expected. However, when I run the application from a Windows Service the console application hangs waiting for the printer to respond.
I am running the service as an administrator, and from what I gather from the Task Manager, it is therefore also running the console application as an administrator. I included checks that the printer name is valid, and everything passes, so it seems that the application has access to the printer. The instance of IE loads the document without issue, so there is content to send to the printer. Switching OLECMDEXECOPT_DONTPROMPTUSER to OLECMDEXECOPT_PROMPTUSER gives the expected result when run directly, but nothing when run through the service.
In the service:
public void RunProgram(string exePath)
{
Process p = new Process();
p.StartInfo = new ProcessStartInfo(exePath);
p.Start();
p.WaitForExit();
}
In the printing function:
ie.Navigate(htmlFilename, ref missing, ref missing, ref missing, ref missing);
while (!documentLoaded && ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT) != OLECMDF.OLECMDF_ENABLED)
Thread.Sleep(100);
ie.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref missing, ref missing);
while (!documentPrinted)
{
PrintToErrorFile("waiting for printing to complete...");
Thread.Sleep(100);
}
When run directly, the printing function outputs 6 or so lines of "waiting..." as expected. When run from the service, the program runs as expected (API calls and all) and then the waiting message repeats ad infinitum.
Upvotes: 0
Views: 271
Reputation: 41
I've had issues with getting something similar to work. Although a windows form app using vb here's a few things to look at. In your code you have a check for documentLoaded and documentPrinted. I assume there is an event handler coded to fire on PrintTemplateTeardown and DocumentComplete? I also see your check for the print command being enabled just loops ad infinitum if it isn't. You might want to look into this. Finally, does ie display when it navigates to your web page? What happens when you try to manually print from this page may give you some further clues. My application runs OK as me but crashes explorer when run with a service account. I've got as far as identifying an rpc issue that I don't know how to fix or debug. (What do I need to look into to fix this printing issue)
Upvotes: 1