title: How to get the compiling errors returned from cl.exe
body: <p>For educational purposes at the university, I am developing a modular WCF C# application. The client app sends a source, the server takes care of compiling, testing and returning a result to the client.</p>
  
  <p>One of the modules of the server does the compiling job. It consumes the source and produces an EXE to be used by the other module(s).</p>
  
  <p>My problem is: when invoking <code>cl.exe</code> for the case when the given source is written in C++, I manage to compile it, but the compiling module cannot receive the error messages properly from the child process, that runs <code>cmd.exe</code>, which then starts <code>cl.exe</code>. The source code and an example of what actually happens say more than a million words, so here they are:</p>
  
  <pre><code>public static string clPath = @"E:\path_to_project\Client\clTo4kaEXE\";
  string sourceCode = "//given source";
  using (StreamWriter sw = new StreamWriter(clPath + exeName + ".cpp"))
  {
      sw.Write(sourceCode);
      sw.Flush();
  }
  
  Process clTo4kaEXE = new Process();
  clTo4kaEXE.StartInfo.FileName = clPath + "cmd.exe";
  clTo4kaEXE.StartInfo.WorkingDirectory = clPath;
  clTo4kaEXE.StartInfo.UseShellExecute = false;
  clTo4kaEXE.StartInfo.RedirectStandardOutput = true;
  clTo4kaEXE.StartInfo.RedirectStandardError = true;
  clTo4kaEXE.StartInfo.RedirectStandardInput = true;
  clTo4kaEXE.StartInfo.Arguments = "%comspec% /k \"\"e:\\vs2010\\VC\\vcvarsall.bat\"\" x86"; 
  
  clTo4kaEXE.Start();
  clTo4kaEXE.StandardInput.WriteLine("cl /EHsc " + exeName + ".cpp");
  StreamReader clStandardOutput = clTo4kaEXE.StandardOutput;
  StreamReader clErrorOutput = clTo4kaEXE.StandardError;
  string clStdOutput = "";
  string temp = "";
  while(true)
  {
      //Debugger.Launch(); // breakpoint
      temp = clStandardOutput.ReadLine();
      Console.WriteLine("STD TEMP = {0}", temp);
      clStdOutput += temp;
      //if (temp == null /*|| temp == "" */|| clStandardOutput.EndOfStream)
      //{
      //    break;
      //}
      if (clStandardOutput.Peek() == -1 &amp;&amp; temp == "")
      {
          break;
      }
  }
  
  string clErrOutput = "";
  temp = "";
  while (true)
  {
      temp = clErrorOutput.ReadLine();
      Console.WriteLine("ERROR TEMP = {0}", temp);
      clErrOutput += temp;
      //if (temp == null || temp == "" || clErrorOutput.EndOfStream)
      //{
      //    break;
      //}
      if (clErrorOutput.Peek() == -1 &amp;&amp; temp == "")
      {
          break;
      }
  }
  
  clTo4kaEXE.Close();
  
  Console.WriteLine("[Modul_Compile] cl.exe returned on its standard output: {0}\n", clStdOutput);
  Console.WriteLine("[Modul_Compile] cl.exe returned on its error output: {0}\n", clErrOutput);
  </code></pre>
  
  <p>When there is an error in the source, for example missing ';' somewhere, then here is what i see in the console:</p>
  
  <p><a href="https://i.stack.imgur.com/ucjJo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ucjJo.png" alt="Console contents screenshot"></a></p>
  
  <p>Then I decided to run Visual Studio Command Prompt giving it the same source code and here is what i get:</p>
  
  <p><a href="https://i.stack.imgur.com/lezKA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lezKA.png" alt="enter image description here"></a></p>
  
  <p>Remarks:</p>
  
  <ol>
  <li><p><code>clStdOutput= clStandardOutput.ReadToEnd();</code> used instead of that <code>while(true){}</code> causes the client app's window to "freeze" and the compiling module doesn't receive anything from its child process.</p></li>
  <li><p>I am pretty surprised that <code>cl.exe</code> prints the messages "Microsoft (R) 32-bit C/C++ Optimizing..." and "Copyright (C)... All rights reserved." to its Error output - the stream, from which I expect to receive the compiling error.</p></li>
  <li><p>I searched the net for any info, found some clues, which helped me get anything from the child process. Now the next step is to get what i need to.</p></li>
  <li><p>I couldn't find a way to start the VS Command Prompt, because it's actually a shortcut, not pointing to an exe, so I couldn't benefit from it.</p></li>
  </ol>
  
  <p>Any help would be appreciated! Thanks! :)</p>