views:

149

answers:

3

Hi, I'm writing a windows service in C# 3.0, when calling

            p = new System.Diagnostics.Process();
            p.StartInfo.FileName = aFileName;
            p.StartInfo.Verb = "Print";
            p.StartInfo.CreateNoWindow = true;
            p.Start();

The Start() doesn't start the process and doesn't print the indicated document passed in aFileName parameter.

How can I start a process from a Windows Service? I'm running Windows Vista OS.

+1  A: 

I believe it my be because starting the process would require the service to interact with the desktop which, by default, is not allowed in a service

James Conigliaro
A: 

I was once using a webpage to send uploaded files to a printer to convert them to pdf. This is what I used to get my process to run.

try
{
  processInfo = new System.Diagnostics.ProcessStartInfo();
  processInfo.CreateNoWindow = true;
  processInfo.FileName = aFileName;
  processInfo.Arguments = "";
  processInfo.UseShellExecute = true;
  processInfo.Verb = "Print";
  processInfo.WorkingDirectory = "c:\\temp";
  System.Diagnostics.Process process = new System.Diagnostics.Process();
  process.StartInfo = processInfo;
  process.Start();
  while (process.HasExited == false)
  {
  }
}
catch (Exception ex)
{  
  throw new Exception("Died in launch process:" + ex.Message, ex.InnerException);
}

A note on the while statement. It seems to me that the process would die at the end of my function, so I used the while to keep it alive until it finished.

Jack B Nimble
A: 

In short, you'll have to utilize the Windows API's to query the user's token then launch the process in the users' space. I don't have the time to work up or find example code but basically you'll need to utilize the following API calls:

GetActiveConsoleSessionId to get the active windows console session, WTSQueryUserToken to query the user's token, CreateEnvironmentBlock to create an EnvironmentBlock for that user then utilize CreateProcessAsUser to launch the process into the users's space.

There is probably an example out there (maybe on MSDN) but this should give you the starting calls you'll need.

TodK