title: Calling G++ from C# Program
body: <p>I've made a C++ code editor in C# and now am trying to invoke <code>g++</code> to compile the source files. So I've copied the TDM-GCC installation besides my program and wrote a small batch script to call that.</p>
  
  <pre class="lang-sh prettyprint-override"><code>@echo off
  @set PATH="%~dp0TDM-GCC-32\bin\";%PATH%
  call "%~dp0TDM-GCC-32\mingwvars.bat"
  cd %1
  "%~dp0TDM-GCC-32\bin\g++.exe" %2 -o %3
  </code></pre>
  
  <p>And from C# code, I'm trying to call this script by using CMD like in this script of code.</p>
  
  <pre><code>string fileName   = Path.GetFileName(CurrentFile);
  string exeName    = Path.GetFileNameWithoutExtension(CurrentFile) + ".exe";
  string workingDir = Directory.GetParent(CurrentFile) + "";
  string compile    = Directory.GetParent(Application.ExecutablePath) + "\\compile.cmd";
  
  File.Delete(Path.Combine(workingDir, exeName));
  
  StartProcess(true, "cmd", "/c", "\"" + compile + "\"", workingDir, fileName, exeName);
  </code></pre>
  
  <p>And here is the <code>StartProcess</code> method:</p>
  
  <pre><code>void StartProcess(bool hidden, string command, params string[] args)
  {
      ProcessStartInfo pStartInfo = new ProcessStartInfo();
  
      pStartInfo.FileName = command;
      pStartInfo.Arguments = string.Join(" ", args);
      pStartInfo.UseShellExecute = false;
  
      if (hidden)
      {
          pStartInfo.RedirectStandardError  = true;
          pStartInfo.RedirectStandardOutput = true;
          pStartInfo.CreateNoWindow = true;
      }
  
      Process proc = new Process();
      proc.StartInfo = pStartInfo;
  
      proc.Start();
  
      logBox.Clear();
  
      if (hidden)
      {
          while (!proc.StandardError.EndOfStream)
          {
              logBox.AppendText(GetTimestamp() + "  Error: " + proc.StandardError.ReadLine() + Environment.NewLine);
          }
      }
  }
  </code></pre>
  
  <p><strong>Update: 26-9-14</strong></p>
  
  <p>I doubted whether the batch file is ever called, because if I call it from cmd, it simply works. So I tried to <code>echo</code> to the standard error from the batch file like this:</p>
  
  <pre><code>echo %PATH% 1&gt;&amp;2
  </code></pre>
  
  <p>And I can see that the GCC's bin folder is in the path too, but for some reason, the exe is not getting created. But sometimes, the script works, and the exe gets created. </p>
  
  <p>But whenever I execute this, there will be nothing in the LOG and also, no executable is being created. I know that the TDM-GCC's bin folder must be in the PATH, but that is what the second line of the batch script needs to do. Dunno what this is and why this error.</p>
  
  <p><strong>End of update</strong></p>
  
  <p>Any suggestions on how can I get it working?</p>
  
  <p>Thanks.</p>