Printing rendered HTML from Windows Service

Chris 1 Reputation point
2021-02-23T16:03:22.883+00:00

I am attempting to maintain an automated html printing service and we are migrating to Server 2016. During this migrating, the automated printer service has stopped functioning.

The original version used the 'old standard' of creating an InternetExplorer interface, and then controlling it remotely using the OLECMD method. I've tried including the code, but every time I do I get an Azure firewall error when trying to save the question. It's easy enough to find though, comes up whenever you search "Print html from windows service."

While executing on Server 2003 or Windows 10, this works just fine. But the moment I put it on Server 2016, it reaches the 'Print' OLECMD and never comes back. No error is thrown, it simply goes to sleep waiting for the teardown to happen, and that never does.

Things I have already tried and have not worked...

  • Giving the Windows Service permission to interact with the desktop
  • Changing the user the Service runs under to both system and administrative accounts--none worked
  • Going into Component Services and giving blanket access/control/launch/activation permissions to everything (planned to narrow it once I knew what it needed) related to Internet Explorer
  • Taking ownership of everything related to IE in the Windows Registry
  • Replacing the OLECMD logic with a Windows Forms Browser control and trying to remote control that. It works while in a console app, but fails once put into a Windows Service
  • Doing the same as above, but spawning the Browser Control off in an STA apartment stated thread
  • Attempting all three of my printing options (OLECMD, Forms, and Threaded Forms) in a Scheduled Task
  • Making use of Foxxit PDF Reader's ability to run headless (this was axed by leadership/legal before I got the chance to try it)

I have seen advice around the net advising that the "recommended" way of having a hands-off printing service is to run it as a tray application, but that is an unacceptable solution in our environment. Unless I'm missing something big, you can't activate a tray application unless someone has logged in--and given the sheer volume of printing that we handle, we need a dedicated server handling this. Which means that nobody works on that server, and so nobody is reliably logged into it. Likewise, just using Windows 10 is unacceptable in the environment we're working with.

Ultimately, the problem seems to stem from Microsoft's increasing levels of Session 0 Isolation...and it appears that, unless I'm missing something that I can't find documented anywhere on the net, that the old OLECMD system has been locked out of being able to command a Print operation while in session 0.

So, is there something I am missing that I couldn't find anywhere else on the internet that makes this work? Or is there some other recommended way of having something that prints rendered HTML in an entirely hands-off fashion?

Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,365 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,191 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jenny Feng 14,146 Reputation points
    2021-02-24T07:43:42.963+00:00

    Hi,
    I did research on this question, but I feel sorry that this question is hard to answer.
    Windows server community usually discussing windows server built-in functions and related issue, for printing html question you'd better open a support ticket with Microsoft for deep research:
    https://support.microsoft.com/en-gb/gp/contactus81?forceorigin=esmc&Audience=Commercial&wa=wsignin1.0

    Hope above information can help you.

    ============================================
    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Cheong00 3,471 Reputation points
    2021-03-09T03:53:08.62+00:00

    Try the following and see if it works for you:

    1. Run the service as a standard user, with target printer configured.
    2. In your service, run LoadUserProfile() API before printing. Remember to call UnloadUserProfile() after finish or the user profile can be corrupted. Strongly recommended to put it in your finally statement.

    Here's example on how to call it in VB.NET, which should be easy to convert to C# once you copied the function/struct signature included in P/Invoke website. There are other examples in C# but I choose this one mainly because it uses WindowsIdentity.GetCurrent.Token directly instead of LogonUser() in most example, and this is more suitable for you. Just fill the PROFILEINFO as instructed and call it and you should be fine.