title: Windows: How to stop buffering of redirected Stdout using CreateProcess
body: <p>I am using pipes to get redirected stdout output from a command line executable. Unfortunately I don't get any output until the process has completed. The executable outputs progress status as it runs and this is what I would like to parse.</p>
  
  <pre><code>BOOL RunCmd( char  *pCmd, 
               char  *pWorkingDir, 
               int    nWaitSecs, 
               BOOL   fRegImport, 
               DWORD *pdwExitCode )
  {
    BOOL                fSuccess = TRUE;
    STARTUPINFO         si;
    PROCESS_INFORMATION pi;
    SECURITY_ATTRIBUTES sFileSecurity;
    ZeroMemory( &amp;sFileSecurity, sizeof( sFileSecurity ) );
    sFileSecurity.nLength        = sizeof( sFileSecurity );
    sFileSecurity.bInheritHandle = TRUE;
  
    HANDLE hReadPipe  = NULL;
    HANDLE hWritePipe = NULL;
  
    fSuccess = CreatePipe( &amp;hReadPipe, &amp;hWritePipe, &amp;sFileSecurity, 0 );
    SetHandleInformation( hReadPipe, HANDLE_FLAG_INHERIT, 0 ); 
  
    ZeroMemory( &amp;si, sizeof(si) );
    ZeroMemory( &amp;pi, sizeof(pi) );
    si.cb          = sizeof( si );
    si.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.hStdOutput  = hWritePipe;
    si.hStdError   = hWritePipe;
    si.wShowWindow = SW_HIDE;
  
    int rc;
  
    // Start the child process. 
    rc = CreateProcess( NULL, // No module name (use command line). 
                        pCmd, // Command line. 
                        NULL,             // Process handle not inheritable. 
                        NULL,             // Thread handle not inheritable. 
                        TRUE,
                        CREATE_NO_WINDOW,
                        NULL,             // Use parent's environment block. 
                        pWorkingDir,      // Working folder
                        &amp;si,              // Pointer to STARTUPINFO structure.
                        &amp;pi );            // Pointer to PROCESS_INFORMATION structure.
  
  
    if( ! rc )
      return FALSE;
  
    // Wait until child process exits.
    DWORD dwWaitResult;
    DWORD dwTimeStart = ::GetTickCount();
    DWORD dwTimeNow;
  
    #define BUFSIZE 4096  
  
    DWORD dwRead = 0; 
    DWORD dwAvail;
    CHAR  chBuf[ BUFSIZE ];
    BOOL  bSuccess = TRUE;
  
    for( ;; )
    {
      dwTimeNow    = ::GetTickCount();
      dwWaitResult = ::WaitForSingleObject( pi.hProcess, ONE_SECOND );
      dwRead       = 0;
  
      for( dwAvail = 0; PeekNamedPipe( hReadPipe, 0, 0, 0, &amp;dwAvail, 0 ) &amp;&amp; dwAvail; dwAvail = 0 )
      {
        dwRead = 0;
        ReadFile( hReadPipe, chBuf, min( BUFSIZE, dwAvail ), &amp;dwRead, NULL );
  
        if( dwRead &gt; 0 )
        {
          FILE *op = fopen( "c:\\REDIR.OUT", "a" );
  
          if( op )
          {
            fwrite( chBuf, 1, dwRead, op );
            fclose( op );
          }
        }
      }
  
      if( dwWaitResult == WAIT_OBJECT_0 )
      {
        DWORD dwExitCode;
        GetExitCodeProcess( pi.hProcess, &amp;dwExitCode );
  
        if( pdwExitCode )
          (*pdwExitCode) = dwExitCode;
  
        break;
      }
  
      if( dwWaitResult == WAIT_TIMEOUT )
      {
        if( dwTimeNow - dwTimeStart &lt; (DWORD)( ONE_SECOND * nWaitSecs ) )
          continue;
        else
        {
          fSuccess = FALSE;
          break;
        }
      }
  
      fSuccess = FALSE;
      break;
    }
  
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    CloseHandle( hReadPipe );
    CloseHandle( hWritePipe );
  
    return fSuccess;
  }
  </code></pre>
  
  <p>The PeekNamedPipe() call is called every second and dwAvail is zero every time until the process completes.</p>
  
  <p>How do I get output from the process sooner? When running the process from the console, I see progress output as it goes. The process will be using "\r" in it's output to display a percentage at the start of the same line.</p>