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.