从Windows服务调用时System.Diagnostics.Process.Start()无法启动程序无法启动、程序、System、Windows

2023-09-03 04:12:23 作者:指尖滑落的泪

我想从.NET Windows服务启动外部进程。在过去,我已经使用了的Process.Start()重载,是以可执行文件路径和命令行字符串。这个工程。但现在我想开始的过程,有它在特定用户的上下文中运行。所以我把这个版本的的开始()

I am trying to start an external process from a .NET Windows service. In the past I have used the Process.Start() overload that takes the executable path and a command line string. This works. But now I would like to start the process and have it run in the context of a particular user. So I call this version of Start()

public static Process Start(
    string fileName,
    string userName,
    SecureString password,
    string domain)

然而,当我调用该方法,我试图运行应用程序生成一个未处理的异常:

However, when I call the method, the application I am trying to run generates an unhandled exception:

应用程序未能正确初始化(0xc0000142)。单击确定以终止应用程序。

我曾试图启动不同的应用,他们都产生相同的异常。我已经运行了code中的Windows服务的外部应用程序正确启动。

I have tried to start different applications and they all generate the same exception. I have run the code outside of the Windows service and the application starts correctly.

那么,有没有办法让这个在Windows服务工作?

So is there a way to get this to work in a Windows service?

推荐答案

这是非常相似,这个问题的here.答案通常是由于与桌面和窗口站,其中,过程正在运行的安全性问题。请参见本文一个解释和一些示例code。

This is very similar to this question here. The answer is usually due to security issues with the desktop and window station in which the process is being run. See this article for an explanation and some sample code.