title: Win32 ReadFile hangs when reading from pipe
body: <p>I am creating a child process, and reading its output. My code works fine when the child process creates output (<code>cmd /c echo Hello World</code>), however ReadFile will hang if process does not create output (<code>cmd /c echo Hello World &gt; output.txt</code>). I am only reading after the process has terminated.</p>
  
  <p>Am I doing something horribly wrong? Is there anyway to do this with synchronous mode, or do I have to use asynchronous mode? All of this is happening in a seperate thread, so I dont think asynchronous mode would offer any benefit to me, unless it is the only way to get this to work. Thanks a lot!</p>
  
  <pre><code>saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
  saAttr.bInheritHandle = TRUE; 
  saAttr.lpSecurityDescriptor = NULL; 
  
  CreatePipe(&amp;g_hChildStd_OUT_Rd, &amp;g_hChildStd_OUT_Wr, &amp;saAttr, 0);
  SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0);
  
  memset(&amp;piProcInfo, 0, sizeof(PROCESS_INFORMATION));
  memset(&amp;siStartInfo, 0, sizeof(STARTUPINFO));
  siStartInfo.cb = sizeof(STARTUPINFO); 
  siStartInfo.hStdError = g_hChildStd_OUT_Wr;
  siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
  siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
  CreateProcess(NULL, commandWideString, NULL, NULL, TRUE, 0, NULL, NULL, &amp;siStartInfo, &amp;piProcInfo);
  
  while(1)
  {
      GetExitCodeProcess(piProcInfo.hProcess, &amp;processExitCode);
      if(processExitCode != STILL_ACTIVE)
          break;
      else
          Sleep(1);
  }
  
  *output = (char *)calloc(32, sizeof(char));
  processOutputSize = 0;
  while(1)
  {
      bSuccess = ReadFile( g_hChildStd_OUT_Rd, processOutputTemp, 32, &amp;dwRead, NULL);
      if(!bSuccess || !dwRead)
          break;
      memcpy(*output + processOutputSize, processOutputTemp, dwRead);
      processOutputSize += dwRead;
      if(dwRead == 32)
          *output = (char *)realloc(*output, processOutputSize + 32);
      else
      {
          memset(*output + processOutputSize, 0, 1);
          break;
      }
  }
  CloseHandle(piProcInfo.hProcess);
  CloseHandle(piProcInfo.hThread);
  CloseHandle(g_hChildStd_OUT_Rd);
  CloseHandle(g_hChildStd_OUT_Wr);
  </code></pre>