title: What's the correct way to use win32file.ReadFile to get the output from a pipe?
body: <p>I'm using the <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow noreferrer">pywin32</a> extensions to access the win32 API under Python. I'm new at doing Windows programming in Python -- I'm a POSIX guy -- so I may be doing things in a bone-headed manner.</p>
  
  <p>I'm trying to use the <a href="http://docs.activestate.com/activepython/2.5/pywin32/win32file__ReadFile_meth.html" rel="nofollow noreferrer">win32file.ReadFile</a> function properly, and I'm having some trouble interpreting the possible result codes.</p>
  
  <p>I'm calling the function like this:</p>
  
  <pre><code>result, data = win32file.ReadFile(child_stdout_r, 4096, None)
  </code></pre>
  
  <p>I'm reading the output from a child process that I launch. I get good data, but I'm concerned that there may be more data in the pipe than 4096 characters. (And I'd rather do this right, instead of just picking an arbitrarily large buffer size.)</p>
  
  <p>In the case where there's more than 4096 characters to read, I would need to run win32file.ReadFile multiple times until I exhaust the pipe. To find out whether I need to run ReadFile multiple times, I need to interpret the result code.</p>
  
  <p>The <a href="http://docs.activestate.com/activepython/2.5/pywin32/win32file__ReadFile_meth.html" rel="nofollow noreferrer">ActiveState docs</a> say that:</p>
  
  <blockquote>
    <p>The result is a tuple of (hr, string/PyOVERLAPPEDReadBuffer), where hr may be 0, ERROR_MORE_DATA or ERROR_IO_PENDING.</p>
  </blockquote>
  
  <p>Since I'm setting the overlapped value to None in the function call, I think I don't need to worry about any PyOVERLAPPEDReadBuffer stuff. (And since I'm getting valid data, I think I'm right.)</p>
  
  <p>I have two problems with the hr result variable:</p>
  
  <ol>
  <li>I can't find the values of the constants ERROR_MORE_DATA or ERROR_IO_PENDING anywhere.</li>
  <li>The ActiveState docs seem to imply that 0 is success and the constants (whatever they are) indicate failure. The <a href="http://msdn.microsoft.com/en-us/library/aa365467(VS.85).aspx" rel="nofollow noreferrer">Microsoft docs</a> state that 0 indicates failure, non-zero indicates success, and you need to run GetLastError to find out more.</li>
  </ol>
  
  <p>What's the correct way to do this?</p>
  
  <p><strong>EDITED TO ADD:</strong> I'm not using subprocess because I need to add the child process to a job object I create. The goal is to have all child processes die immediately if the parent process dies. By adding the child process to the job object, the child process will be terminated when the last handle to the job object is closed. The handle, held by the parent, will be closed when the parent exits. All of this, as far as I can tell, precludes me from using subprocess.</p>