Print from windows service

Printing from windows service on windows 8 fails

I have a windows service application written in C# .NET. This application is used to generate a report pdf by printing document to local software printer that generates PDFs. This works well on Windows XP and Windows 7. Unfortunately I found that on Windows 8 it fails. Then I found out that printing to any (even physical) printer on Windows 8 fails when I print from my service. What is missing in my program to work? I’m printing this way:

Service is set to ‘system account’ without ‘interacting with desktop’ (but I tried that too or to login as local user).

This results into exception on Windows 8. When using ‘Print ticket — Approach 1’:

Using ‘Print ticket — Approach 2’:

I would say that service is able to find those printers because when I have tried to print to non-existing printer and I got «invalid printer name» exception.

In case someone is interested in trying — here is my sample service application that tries to print simple document to printer selected in config file: http://bin.mypage.sk/FILES/PrintTestService.rar

Interesting. When I tried a different printing code there is no error:

Unfortunately this is a older GDI+ code using the System.Drawing.Graphics library which is not compatible with my code that produces paginated document in form of System.Windows.Media.Visual objects. So I can’t use it to print my document unless I’d like to spend two weeks of creating the pagination of my document from scratch.

There is discussion about this issue here: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/96e7fc10-3f08-4808-b748-692e30377293 There is a ‘workaround’ for it to use ‘anyCPU’ platform. This workaround really works (I tried it) but it is not usable in my case when my service needs to be x86. I have contacted MS support through our company to find a real solution.

3 Answers 3

At http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/fdcfa0fa-50aa-4a61-be79-5b4c8f65fbf7/ we see that this was reported to Microsoft and confirmed as a bug in Windows 8 and Windows Server 2012.

This bug is triggered when trying to print from a 32bit process in non-standard user session (like e.g. a service).

According to Microsoft, this a bug was resolved in Windows 8.1 and Windows Server 2012 R2. However, we could still reproduce it on Windows 8.1.

On the same site, a workaround is given by Microsoft. This workaround solved the problem for us on Windows 8.1. It probably also works on Windows 8 and Windows Server 2012.

The workaround goes as follows:

Open Regedit and go to HKEY_CLASSES_ROOT\CLSID

Check the value of the «AppID» Registry Entry. In our case this was

  • Now go to HKEY_CLASSES_ROOT\AppID (or the respective value you found on your system)
  • Under this registry key, delete the entries with the name «AccessPermission», «LaunchPermission» and «RunAs»
  • Since this is a bug in Windows, you cannot fix it in your code. The workaround might have side effects, but we haven’t seen any so far in our scenario.

    Solution: To wait for Microsoft to fix this bug. I have reported the bug (REG:113040910349062), they have confirmed it, but fix won’t be soon (in next months) and there is no date specified for it.

    Workaround that we use: Create a small application that handles the printing, compile it as ‘anyCPU’ application and run it from service application (which I can’t compile as ‘anyCPU’ — I need ‘x86’ because of its dependencies). This workaround was tested and works. But it is dirty + causes overhead + extra care.

    Читайте также:  Проигрыватель m2ts для windows

    Edit: Microsoft provides a hotfix that should fix this problem in Windows 8 : http://support.microsoft.com/kb/2872151/EN-US ( «You cannot print from a 32-bit WPF application in an x64-based version of Windows 8 or Windows Server 2012» )

    Edit (2013-11-04): we have tested the hotfix and it does not work on Windows 8 or Windows 8.1. Currently we are again in a phase of communicating with Microsoft.

    Edit (2014-beginning of the year): we have received a response from Microsoft that they don’t know how to fix this bug and the bug is closed. So only solution for us at the moment is having a small app exe compiled as AnyCPU which is called by our x86 service. This approach works, but causes slowdown and our product is also harder to maintain because of this.

    How to print PDF document from Windows Service

    I have windows service that must periodically print PDF document from server. My function is

    When I implement this in Windows Application it works, but when I implement in Windows service it does not work.

    Can you help me?

    3 Answers 3

    I solved problem with Session 0. I use this class:

    Now, my function look like:

    Also, ServiceProcessInstaller must have `Account set to «LocalSystem». When I created service I set to «Local service» with this user it does not work. I did not try with «Network service» or «User».

    I solved this problem with the registry edits found in this article: https://support.microsoft.com/en-us/kb/184291.

    Printing from Windows services (and also IIS) uses the SYSTEM account as I understand it. This account does not have access to the printer but these registry edits gives that SYSTEM account printing privileges.

    Here is a summary:

    1. export 3 keys («folders within regedit.exe») to .reg files:
      • HKEY_CURRENT_USER\Software\Microsoft\Windows NT\Current Version\Devices
      • HKEY_CURRENT_USER\Software\Microsoft\Windows NT\Current Version\PrinterPorts
      • HKEY_CURRENT_USER\Software\Microsoft\Windows NT\Current Version\Windows
    2. edit your 3 newly created reg files and change «HKEY_CURRENT_USER» to «HKEY_USERS\.DEFAULT»
    3. run your 3 reg files by double clicking them in explorer (this adds them to the registry)

    That’s all it takes. This helped me to print pdf files to a printer from PHP in IIS 7. My php script would work just fine so long as the server happened to have the Administrator logged in when the script was run. Now no one needs to be logged in and the script still works 🙂

    I hope this saves someone as much searching and tinkering as I put in to find it.

    Install Print and Document Services

    Applies To: Windows Server 2012

    Before installing Print and Document Services in Windows Server 2012, consider the Printing and Scanning scenarios that your organization will benefit from and what kind of advanced features are required. In addition to local installation of role services, Windows Server 2012 Server Manager supports remote installation and configuration of role services and can be used to install Print and Document Services on a remote computer in your organization. You can then remotely manage your print server from your local computer. Start the Print Management console on your local computer, and then add the remote print server to the console.

    The following sections outline the deployment scenarios for Print and Document Services and how to use Server Manager to install and perform the initial configuration of the features that make up a specific scenario.

    Deployment Scenarios

    Windows Server 2012 Print and Document Services consists of several different role services that can be installed individually to support basic functionality, or can be combined with other role services to provide more advanced features. When installing Print and Document Services features to support advanced functionality it is helpful to think of the scenarios that are enabled by those advanced features. Windows Server 2012 Server Manager will prompt the administrator to select the role services to be installed at the time of installation and to choose which role services are installed on each server in the deployment.

    Читайте также:  Сразу не выключается компьютер после завершения работы windows

    The Print Server role is used to create a Windows Print Server and includes the Print Management Console snap-in, which is useful for managing multiple printers or print servers and migrating printers to and from other Windows print servers.

    Internet Printing

    The Internet Printing service creates a web site where users can manage print jobs on a Print Server. It also enables users who have the Internet Printing Client installed to use a web browser to connect and print to shared printers by using the Internet Printing Protocol (IPP).

    LPD Service

    The Line Printer Daemon service enables UNIX-based computers or other computers using the Line Printer Remote (LPR) service to print to shared printers on a Windows Print Server.

    The LPD and LPR Services are deprecated starting with Windows Server 2012. Eventually, they will be completely removed from the product, but they are still available in this release. You should begin planning now to employ alternate methods for any applications, code, or usage that depend on these features. For more information about features or functionalities that have either been removed from the product in the current release or are planned for potential removal in subsequent releases, see Features Removed or Deprecated in Windows Server 2012.

    Installation Process

    The following section provides details of the new Server Manager user interface and how to use it to install the necessary role services needed for the chosen Print and Document role services.

    Installation UI/Wizard

    Installation of all Print and Document Services roles are performed using the new Server Manager user interface for Windows Server «8» Beta. This new interface provides both local and remote server management and uses a series of plugins to manage specific server functions, role services, and deployment types.

    The following is an example of the Server Manager Dashboard page, which is the first page that will be displayed when Server Manager is opened:

    Figure 1: Server Manager Dashboard

    Installing Print and Document Services

    Once the server that will host Print and Document services has been configured with initial settings, the role services can be installed using the Add Roles and Features method of Server Manager.

    To install Print and Document Services

    Open Server Manager and click All Servers in the navigation pane.

    Click Manage in the Menu Bar and then click Add Roles and Features.

    Server Manager prompts you to verify that the following prerequisites have been met: The Administrator account has a strong password Network settings, such as static IP addresses are configured The latest security updates from Windows Update are installed Ensure these prerequisites have been met before continuing the installation.

    Click Next, select Role or feature-based Installation, and then click Next.

    On the Select destination server page, select the server to install the Print and Document Services on. The default server is the local server. Then click Next.

    On the Select server roles page, select Print and Document Services. Click Add Features to add the features that are required for Print and Document Services, then click Next.

    Click Next on the Select features page.

    On the Print and Document Services page, review the notes for the administrator and then click Next.

    On the Select role services page, choose the role services that you wish to install. By default, Print Server is selected automatically. If you select Internet Printing you must also add the required features by clicking Add Features. Click Next.

    Читайте также:  Не открывается nvidia geforce experience windows 10

    If you added Internet Printing, verify the Web Server Role settings and click Next until the Confirm installation selections page is displayed. Click Install to install the required role services.

    The Installation progress page displays the status of the installation.

    Figure 2: Installation progress

    When installation has finished, restart the server to complete the installation and configuration. To see the status of each component installation, open the Task Details by clicking on the Notifications flag.

    Verifying Installation

    To verify installation of the Print and Document role services, open Server Manager and click Print Services in the navigation pane. If the role services were successfully installed, Server Manager should display the name of the server in the list of Servers, and the Print Spooler service displays as Running in the Services tile:

    Figure SEQ Figure \* ARABIC 6: Print and Document Services Installation Verification

    Figure 3: Print and Document Services Installation Verification

    The IIS Admin Service and the World Wide Web Publishing Service are required services for Internet Printing, so these services would also be present and a running on a server that has the Internet Printing role service installed.

    Installing on Server Core

    The Print Server role can be installed on Server Core installations, and Windows Server 2012 now provides XPS and v4 driver support on both Full Server and Server Core installations.

    To install the Print Server role on a Server Core server

    Server Core installations will start a command prompt by default. Type the following commands to start Windows PowerShell, load the Server Manager provider, and install the Print-Server role.

    Windows PowerShell commands

    Restart the server. Before you can remotely configure the server you must follow the steps in the Remote Management of Server Core Print Servers section of this article.

    Printers and drivers can also be installed using Windows PowerShell if the server is to be managed locally. For more information, see Print Management cmdlets in the Microsoft TechNet Library web site.

    Installing with Minimal Server Interface

    You can install a Print Server with the Minimal Server Interface, with the following limitations:

    Print Management Console email notifications will not work.

    Extended View functions will not work.

    Extended view includes printer web pages as well as viewing documents printing from the queue in Print Management console. However, you can still open the print queue to see jobs.

    If email notifications and Extended View functions are required, you should install the Server with a GUI option.

    For more information about server installations options, see Windows Server Installation Options.

    Uninstalling Role Services

    Uninstalling role services with Server Manager must be done manually and on one server at a time. There is no Scenario Based Uninstall that will uninstall role services from multiple remote computers using a single wizard, but role services can be uninstalled from a remote computer by running Server Manager on any server in the deployment.

    To uninstall Print and Document Services role services

    Open Server Manager and click All Servers in the navigation pane.

    Click Manage in the Menu Bar and then click Remove Roles and Features.

    On the Before you begin page, click Next.

    On the Select destination server page, select the server that you want to remove the role services from and then click Next.

    Select the role services that you would like to remove on the Remove server roles screen and then click Next.

    Select the Features that you would like to remove (if any) on the Remove features screen and then click Next.

    Click Remove on the Confirm removal selections page.

    Repeat steps 1-7 for each server that you wish to remove role services from.

    Оцените статью