错误代码 0xc0000142 从 windows 服务启动进程

Error Code 0xc0000142 starting process from windows service

从 Windows 服务启动进程时出现问题

 _________
|         |           * Process 1
| Service |  -------> * Process ...
|_________|           * Process n

服务使用的代码如下:

    ProcessStartInfo startInfo = new ProcessStartInfo(executablePath, commandlineArgs);
    startInfo.WorkingDirectory = instancePath;
    startInfo.UserName = userB;
    startInfo.Password = passwordSecureString;
    startInfo.Domain = domain;
    startInfo.UseShellExecute = false;

    Process process = new Process
    {
      StartInfo = startInfo,
      EnableRaisingEvents = true,
    };

    process.Start();

现在,我有两个帐户 - 为简单起见,假设为 A 和 B。服务在第一个帐户下 运行,并使用第二个帐户启动进程。

下面的table应该表示在哪个帐户星座中发生的事情。


 _______________
|   |  A  |  B  |      
|===+=====+=====|
| A | OK  |  X  |
|---+-----+-----|
| B |  X  |  OK |
|___|_____|_____|

...这让我假设用户更改导致了问题。

发生异常的详细信息:

在阅读了错误代码 142 之后,这是我目前所做的尝试:

任何人都可以解决这个问题吗?

Stephen Martin 已经解释了这里发生的事情,所以如果您遇到原始问题中描述的问题,您可能会对以下博客 post 感兴趣:

http://asprosys.blogspot.de/2009/03/perils-and-pitfalls-of-launching.html

在这里,斯蒂芬解释说

Oops again, where is the process? Check the event log (or you may have received an Application Error pop-up). There should be an entry for Application Error that says that your process was the faulting application, either user32.dll or kernel32.dll was the faulting module and the exception was: 0xC0000142. There may be some minor variation in this but basically it is saying that your application could not initialize. The reason for this is that on initialization, before any application code is run, all processes are attached to a Window Station and all threads are attached to a Desktop but the user you are launching under does not have permission to access the Window Station and Desktop in which your process is being launched, ergo it can't initialize. The security descriptors for the Window Station and Desktop must be adjusted to give AllAccess permission to the user the process is being launched under. This is a devil to do directly in .Net, so you might find the security wrapper classes here useful

如果您不喜欢使用 Stephen 的库,您还可以查看提供的其他解决方案here(我还没有测试过)。