views:

1517

answers:

6

Hello to all,

We have built an application that receives several files in different formats, pdf, tiff, jpgeg, doc, etc. After received, they are converted to tiff files using a third party printing driver which is installed locally on the server and set up as the default printer. In order to do that we open a System.Diagnostics.Process with the command line and arguments to print the file with the appropriate application.

Now the new version needs to be a Windows Service and so far everything is working fine, except the printing part. Whenever the process starts, it never raises an exception and everything seems to be working fine, but the file is never printed out. If I open Task Manager I can see that MS Paint was executed (in case of a jpeg file), but no output tiff file.

As a side note, the final file needs to be a tiff file because of another third party tool our customer uses and that is the only format it supports.

Any help will be greatly appreciated. Sergio Romero

The code we're using is as follows:

private const string PROCESS_COMMAND = "mspaint.exe";
private const string PROCESS_ARGUMENTS = @"""{0}""";

Process proc = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
string error = string.Empty;

startInfo.FileName = PROCESS_COMMAND;
startInfo.Arguments = string.Format(PROCESS_ARGUMENTS, fileFullPath);
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;

proc.EnableRaisingEvents = false;
proc.StartInfo = startInfo;

proc.Start();

using(StreamReader errorReader = proc.StandardError)
{
string standardError = string.Empty;
while((standardError = errorReader.ReadLine()) != null)
{
error += standardError + " ";
}
}
proc.WaitForExit();

A: 

I'm not sure about the part about MSPaint... but if your app works as a console app but not as a service, chances are that the server doesn't have permission to do something that your user account does.

You might want try having the service log on as you to rule out permissions issues.

bobwienholt
A: 

Check if the user used to install the service has the proper printing permissions AND/OR access to the files, i would also recomend using event logging

Oscar Cabrero
A: 

Does MSPaint open when you run this from a console app? If so, its probably because your service is running headless; it doesn't have rights to display UI. So, MSPaint basically bails as it can't open its UI up without erroring.

Why not just print it directly from .NET? You can do this from a service. There are some warnings about System.Printing not designed for use by a service, however. I'm not sure why, tho. I've done it without issues before...

Will
+3  A: 

First thing I'd suggest is to have the service run under the context of a specific user. Then log into the server as that user and make sure that the printer is installed, set as the default, etc.

Secondly, ditch the MS Paint solution to simplify things. You can load the image in .NET using System.Drawing.Image.FromFile(YourImageFilePath) and use PrintDocument to do the rest...

Create a PrintDocument object, define your settings (which printer to use, margins, etc.), add a handler for the document's PrintPage event which does something along the lines of e.Graphics.DrawImage(YourTiffImageObject, New Rectangle(0, 0, e.MarginBounds.Width, e.MarginBounds.Height)) to draw the TIFF image onto the page. Finally, you call your PrintDocument object's .Print method and away it goes.

This way, .NET is handling the printing -- not some random third-party app.

There are some minor code changes when you're dealing with more than one page at a time (primarily calling SelectActiveFrom to change the page on multi-page TIFFs and setting e.HasMorePages = True in the PrintPage event until you read the last page) but it's all fairly easily and well-documented.

UPDATE: Just for completeness, I guess I should add what others have already mentioned... Some applications may require desktop access to function properly. If you stick with MS Paint, you may need to enable 'Allow service to interact with desktop' in the service properties.

Kevin Fairchild
.Net doesn't support all flavors of TIFF.
Joel Lucsy
Joel, since he's converting them to .TIFF himself, that shouldn't be much of an issue.
Kevin Fairchild
A: 

We've run into all sorts of issues with Services trying to launch apps. Often it's security/credentials being used, or it can also be something like enabling 'Allow service to interact with desktop' as the app (in this case mspaint) might need that.

That being said, I agree with Kevin, ditch MSPaint and either print natively within .NET or if it's just a matter of conversion, convert using .NET. The other is to look into something a little more sophisticate then MSPaint with libraries such as LibTIFF or even things like like Ghostscript to handle the formats that may not be supported natively inside .NET such as PDF.

Douglas Anderson
A: 

What should I put in the PrintPage event?

Bryan