Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy ReadFile() doesn't return 0 ? Program tries to read data from pipe forever
    text
    copied!<p>code:</p> <p>This is from much larger MS example ( <a href="http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx</a> ). And of course I tried the whole example before, that my snipped below is where I stripped it down to the problem. The thing is how I'm supposed to break out of this loop if all I get is <code>1</code>. How to do signal that there is no more data to read ?</p> <p>(Compiler is MiGW.)</p> <p>It looks like the MS example is broken, this is one of the comments there: <em>Parent process needs to close handles that it passes to child process after the CreateProcess() call. Not doing so will cause the parent to wait forever on ReadFile().</em></p> <pre><code>#include &lt;iostream&gt; #include &lt;windows.h&gt; using namespace std; int main() { cout &lt;&lt; "\n-&gt;Start of parent execution.\n"; SECURITY_ATTRIBUTES saAttr; saAttr.bInheritHandle = TRUE; saAttr.lpSecurityDescriptor = NULL; HANDLE g_hChildStd_OUT_Rd = NULL; HANDLE g_hChildStd_OUT_Wr = NULL; CreatePipe(&amp;g_hChildStd_OUT_Rd, &amp;g_hChildStd_OUT_Wr, &amp;saAttr, 0); STARTUPINFO siStartInfo; PROCESS_INFORMATION piProcInfo; ZeroMemory( &amp;siStartInfo, sizeof(STARTUPINFO) ); siStartInfo.hStdOutput = g_hChildStd_OUT_Wr; siStartInfo.dwFlags |= STARTF_USESTDHANDLES; CreateProcess(NULL, (char*)"cmd /c dir", // command line NULL, // process security attributes NULL, // primary thread security attributes TRUE, // handles are inherited 0, // creation flags NULL, // use parent's environment NULL, // use parent's current directory &amp;siStartInfo, // STARTUPINFO pointer &amp;piProcInfo); // receives PROCESS_INFORMATION CHAR chBuf[4096]; DWORD dwRead; // ~~~~~~this loop here~~~~~- how to break out of it ? while(true) { cout &lt;&lt; "ReadFile: " &lt;&lt; ReadFile( g_hChildStd_OUT_Rd, chBuf, 4096, &amp;dwRead, NULL) &lt;&lt; endl; } cout &lt;&lt; "\n-&gt;End of parent execution.\n"; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload