0

I have a Windows service that should print out HTML text.
The OS is 7, with .NET 4.5.1 and IE 11.

The code:

    private static void PrintOnStaThread(string text)
    {
        using (WebBrowser webBrowser = new WebBrowser())
        {
            webBrowser.Navigate("about:blank");
            webBrowser.Document.OpenNew(false);

            while (webBrowser.DocumentText != text && webBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                webBrowser.Document.Write(text);
                webBrowser.Refresh();
                Application.DoEvents();
            }

            InternetExplorer internetExplorer = (InternetExplorer)webBrowser.ActiveXInstance;
            internetExplorer.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, Win32.PRINT_WAITFORCOMPLETION);
        }
    }

The problem:
When the service is logged using the Local System account the ExecWB function does nothing and the thread is exiting without any exception. When the service is logged using the Administrator credentials the ExecWB function works perfectly, but I don't want to use this account.

Any comments will be welcome.

toy4fun
  • 839
  • 2
  • 14
  • 33
  • Don't use WinForms within a Windows Service. The problem is most likely caused by that. Use a different way to print HTML. If you must, see [Print html document from Windows Service in C# without print dialog](http://stackoverflow.com/questions/416314/print-html-document-from-windows-service-in-c-sharp-without-print-dialog), but that still uses the `WebBrowser` control. See also [Print html document from Windows Service without print dialog](http://stackoverflow.com/questions/419412/print-html-document-from-windows-service-without-print-dialog). – CodeCaster Oct 03 '14 at 13:01
  • I've know and used the first example in my code. I don't really understand what do you mean by "Don't usee WinForms". I don't think I am. – toy4fun Oct 03 '14 at 13:43

1 Answers1

0

It is possible, but you need to configure your service to be "Interactive".

For further information, have a look to this document http://msdn.microsoft.com/en-us/library/windows/desktop/ms683502(v=vs.85).aspx

FZysset
  • 15
  • 1
  • 3