title: Executing msbuild process through code gets exit code -1073741502
body: <p>My application is executing MsBuild.exe using <code>Process.Start();</code> running as another user. The process is running as a service. When executing the process instantly fails and returns an error code <code>-1073741502</code>.</p>
  
  <ul>
  <li>I am executing my code as a service.</li>
  <li>No matter what user, or permissions I grant this occurs (Even as Administrator).</li>
  <li>The service user has both the Local Security Policy to <code>Run as a service</code> and <code>Impersonate another user</code></li>
  <li>No matter what my logging methods are not called. Does this means it's failing before it even starts?</li>
  <li>Other executables have no problem executing in this manner.</li>
  <li>When not executing my code as a service it executes successfully.</li>
  <li>WTF is the negative error code <code>1073741502</code>??(!!) Closest thing i've found is <a href="https://stackoverflow.com/questions/5366965/windows-sdk-c-sharp-debugging-process-exiting-with-error-code-1073741502">this</a>.</li>
  </ul>
  
  <p>Example code:</p>
  
  <pre><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;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>When investigating the Event logs I see the following exception (vague as hell):</p>
  
  <pre><code>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:
  </code></pre>