C# – System.Diagnostics.Process.Start() cannot start process when called from Windows service

cnetwindows-services

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:

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.

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

Best Answer

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".