C# 从 Windows 服务调用时 System.Diagnostics.Process.Start() 无法启动进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/456155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Diagnostics.Process.Start() cannot start process when called from Windows service
提问by jmatthias
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()
我正在尝试从 .NET Windows 服务启动外部进程。过去,我使用了Process.Start()
采用可执行路径和命令行字符串的重载。这有效。但现在我想开始这个过程并让它在特定用户的上下文中运行。所以我称这个版本为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:
但是,当我调用该方法时,我尝试运行的应用程序会生成一个未处理的异常:
The application failed to initialize properly (0xc0000142). Click on OK to terminate the application.
The application failed to initialize properly (0xc0000142). Click on OK to terminate the application.
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?
那么有没有办法让它在 Windows 服务中工作?
回答by Otávio Décio
Use Filemon and see if it is trying to open a config file and not finding it. I once had this error due to a malformed config.
使用 Filemon 并查看它是否正在尝试打开配置文件而未找到它。由于格式错误的配置,我曾经遇到过这个错误。
回答by Igal Serban
Maybe the user has to have, "logon as a service" security right. This is done with the "local security policy" application. And/or "logon as a batch job".
也许用户必须拥有“作为服务登录”的安全权限。这是通过“本地安全策略”应用程序完成的。和/或“作为批处理作业登录”。
回答by configurator
This is just a shot in the dark, but perhaps you can try to run the Windows Service in Interactive mode. If that works, though, this can't be done in Windows Vista (because of Session 0 Isolation).
这只是黑暗中的一个镜头,但也许您可以尝试以交互模式运行 Windows 服务。但是,如果这样做有效,则无法在 Windows Vista 中完成(因为会话 0 隔离)。
回答by Stephen Martin
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 articlefor an explanation and some sample code.