在Windows Vista中从Windows服务启动新进程

时间:2020-12-27 20:40:17

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

我正在拨打电话时在C#3.0中编写一个Windows服务

            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.

Start()不会启动进程,也不会打印aFileName参数中传递的指示文档。

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

如何从Windows服务启动进程?我正在运行Windows Vista操作系统。

3 个解决方案

#1


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

我相信这是因为启动流程需要服务与桌面交互,默认情况下,不允许在服务中使用

#2


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.

我曾经使用网页将上传的文件发送到打印机,将它们转换为pdf。这就是我过去运行的过程。

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.

关于while语句的注释。在我看来,这个过程会在我的函数结束时死掉,所以我用while来保持它活着直到它完成。

#3


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:

简而言之,您必须利用Windows API来查询用户的令牌,然后在用户空间中启动该过程。我没有时间处理或查找示例代码,但基本上您需要使用以下API调用:

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.

GetActiveConsoleSessionId用于获取活动的Windows控制台会话,WTSQueryUserToken用于查询用户的令牌,CreateEnvironmentBlock为该用户创建EnvironmentBlock,然后利用CreateProcessAsUser将进程启动到用户的空间。

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

可能有一个例子(可能在MSDN上),但这应该给你你需要的起始电话。

#1


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

我相信这是因为启动流程需要服务与桌面交互,默认情况下,不允许在服务中使用

#2


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.

我曾经使用网页将上传的文件发送到打印机,将它们转换为pdf。这就是我过去运行的过程。

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.

关于while语句的注释。在我看来,这个过程会在我的函数结束时死掉,所以我用while来保持它活着直到它完成。

#3


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:

简而言之,您必须利用Windows API来查询用户的令牌,然后在用户空间中启动该过程。我没有时间处理或查找示例代码,但基本上您需要使用以下API调用:

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.

GetActiveConsoleSessionId用于获取活动的Windows控制台会话,WTSQueryUserToken用于查询用户的令牌,CreateEnvironmentBlock为该用户创建EnvironmentBlock,然后利用CreateProcessAsUser将进程启动到用户的空间。

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

可能有一个例子(可能在MSDN上),但这应该给你你需要的起始电话。