cancel
Showing results for 
Search instead for 
Did you mean: 

PrintToPrinter doesn't work when printing from a windows service in a virtual machine

Former Member
0 Kudos

I have created a window service that will process reports and then call the PrintToPrinter to print them on a specified printer. The service prints the reports if the service runs on a local machine but it doesn't print the reports when it runs on a window virtual machine. FYI, It doesn't throw any exceptions. I've been searching for reasons/solutions but haven't found anything. Please advise. Below is the code that I use.

public void PrintReports(string printer)

{

     try

     {

          GetReportDocument().PrintOptions.PrinterName = printer;

          GetReportDocument().PrintToPrinter(new PrinterSettings { PrinterName = printer }, new PageSettings(), false);

     }

     catch(Exception e)

     {

          ................

     }

}

Accepted Solutions (1)

Accepted Solutions (1)

former_member560462
Participant
0 Kudos

I had some problems with this myself.  As Don said, my issue had to do with the system account not knowing about the printers.  I handled it at runtime by writing a piece of code that copied a registry tree hierarchy then invoked it as follows.  Params are from and to.

// Network

copyRegistryTree(HKEY_CURRENT_USER, "Printers\\Connections",

                 HKEY_USERS, ".Default\\Printers\\Connections");

// Local

copyRegistryTree(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices",

                 HKEY_USERS, ".Default\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices");

copyRegistryTree(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows NT\\CurrentVersion\\PrinterPorts",

                 HKEY_USERS, ".Default\\Software\\Microsoft\\Windows NT\\CurrentVersion\\PrinterPorts");

copyRegistryTree(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows",

                 HKEY_USERS, ".Default\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows");

Answers (1)

Answers (1)

DellSC
Active Contributor
0 Kudos

What OS is the web service running on?  On newer versions of Windows Server, you MUST have the printer defined and the drivers installed on the server in order to be able to print to it.  So, this may not be a Crystal issue, it may be an OS issue.

-Dell

Former Member
0 Kudos

It is a window service runs on Windows 7 x64 OS virtual machine (vmware). I tried calling the same PrintReports function from a winform application (in a virtual machine too) and it prints report just fine.

DellSC
Active Contributor
0 Kudos

What type of account is being used to run the webservice?  If it's a local (to the machine) account, then the webservice may not have access to any network printers.  Try running it under an account that has access to the network (you can use your own credentials for testing) and see whether that helps.

-Dell

0 Kudos

Hi Cody,

Dell is correct, You must install the printers locally, RDC causes huge delays when printing if it uses the (redirected 2), also labelled by MS as "Easy Print".

For app's running as a service it needs to get the printer info from the Admin/System account.

If you run your service under your local user account I suspect it would work.

To add your User Printers to the System account see the MS KBA on how to:

//For printers exposed to System account as per MS Kbase

http://support.microsoft.com/default.aspx?scid=kb;en-us;184291

Be aware if you make changes to the printers this location must also be manually updated with the changes.

And usual warning about modifying the registry manually.

Don