Executing msbuild process through code gets exit code -1073741502

1

My application is executing MsBuild.exe using Process.Start(); running as another user. The process is running as a service. When executing the process instantly fails and returns an error code -1073741502.

  • I am executing my code as a service.
  • No matter what user, or permissions I grant this occurs (Even as Administrator).
  • The service user has both the Local Security Policy to Run as a service and Impersonate another user
  • No matter what my logging methods are not called. Does this means it's failing before it even starts?
  • Other executables have no problem executing in this manner.
  • When not executing my code as a service it executes successfully.
  • WTF is the negative error code 1073741502??(!!) Closest thing i've found is this.

Example code:

void Main(){
    var startInfo = new ProcessStartInfo
    {
        FileName = path,
        Arguments = args,
        WorkingDirectory = workingPath,
        CreateNoWindow = true,
        UseShellExecute = false,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        LoadUserProfile = true,
        Domain = System.Environment.MachineName,
        UserName = creds.Username,
        Password = generateSecureString(creds.Password)
    };
    var process = Process.Start(startInfo);
    process.OutputDataReceived += process_OutputDataReceived;
    process.ErrorDataReceived += process_OutputDataReceived;
    process.Exited += process_Exited;
    process.EnableRaisingEvents = true;
    process.WaitForExit();
}

internal void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine(e.Data);
}

void process_Exited(object sender, EventArgs e)
{
    var process = ((Process) sender);
    Console.WriteLine("Process has finished execution, exit code '{0}'.", process.ExitCode);
}

private SecureString generateSecureString(string password)
{
    var secure = new SecureString();
    foreach (var c in password.ToCharArray())
    {
        secure.AppendChar(c);
    }
    return secure;
}

Any help would be really appreciated. Seems to be a permissions/local security policy issue, but without knowing more it feels like i've reached the "definition of insanity" point of my troubleshooting where i'm just repeating the same actions expecting a different result.

When investigating the Event logs I see the following exception (vague as hell):

Faulting application name: msbuild.exe, version: 12.0.31101.0, time stamp: 0x545443d5
Faulting module name: KERNELBASE.dll, version: 6.3.9600.17668, time stamp: 0x54c846bb
Exception code: 0xc0000142
Fault offset: 0x0009e052 
Faulting process id: 0x3e8
Faulting application start time: 0x01d065cdac34cc77
Faulting application path: C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild.exe
Faulting module path: KERNELBASE.dll
Report Id: ecce8b9d-d1c0-11e4-80d7-00155d611ee6
Faulting package full name: 
Faulting package-relative application ID:
c#
msbuild
asked on Stack Overflow Mar 17, 2015 by Doug • edited May 23, 2017 by Community

1 Answer

1

According to this thread ( Why is this process crashing as soon as it is launched? ), starting a process from a service may result in the native CreateProcessWithLogonW API call which seems to be not working from a service. I just found this thread because I think I'm facing a similar issue but with the powershell start-process commandlet running in a service (which probably make use of CreateProcessWithLogonW internally.

answered on Stack Overflow Jun 22, 2015 by John-Philip • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0