title: Redirecting CMD.exe output and input in c through pipes
body: <p>Im trying to print the output and input of cmd commands recieved to stdout just like a normal cmd.exe would do.
  I could use the function <code>_popen</code> but when i start a program like Python or Powershell whith it, it doesnt work. <strong>So i Need the Output of the child process and be able to send commands to the child process.</strong></p>
  
  <p>there is a simmilar question <a href="https://stackoverflow.com/questions/6722005/redirect-cmd-exe-output-to-a-pipe">here</a></p>
  
  <p>So i modified the code from <a href="https://msdn.microsoft.com/de-de/library/windows/desktop/ms682499(v=vs.85).aspx" rel="nofollow noreferrer">this</a> link to look like this:</p>
  
  <pre><code>void WriteToPipe(char* command){
      DWORD dwRead, dwWritten;
      BOOL bSuccess = FALSE;
  
      bSuccess = WriteFile(g_hChildStd_IN_Wr, command, strlen(command), &amp;dwWritten, NULL);
  
      // Close the pipe handle so the child process stops reading. 
  
      if (!CloseHandle(g_hChildStd_IN_Wr))
          ErrorExit(TEXT("StdInWr CloseHandle"));
  }
  
  void ReadFromPipe(void){
      DWORD dwRead, dwWritten;
      CHAR chBuf[BUFSIZE];
      BOOL bSuccess = FALSE;
      HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  
      bSuccess = ReadFile(g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &amp;dwRead, NULL);
      bSuccess = WriteFile(hParentStdOut, chBuf, dwRead, &amp;dwWritten, NULL);
      //bSuccess = PeekNamedPipe(g_hChildStd_OUT_Rd, NULL, BUFSIZE, &amp;dwRead, &amp;dwTotalAvailBytes, &amp;dwBytesLeft);
  }
  </code></pre>
  
  <p>the main loop looks like this:</p>
  
  <pre><code>CreateChildProcess("C:\\Python27\\python.exe");
  char input_buffer[100] = { 0 };
  while (1){
      fgets(input_buffer, 99, stdin);
      WriteToPipe(input_buffer);
      ReadFromPipe();
  }
  </code></pre>
  
  <p>everything else (from the code) stayed the same.</p>
  
  <p>Now my Problem is, i want to enter multiple commands to the same process, but there is a CloseHandle fucntion in WriteToPipe, after the handle is closed i cant enter more commands.</p>
  
  <p><strong>How to get a valid HANDLE to write more than 1 command to the process ?</strong></p>