コード:
これは、はるかに大きな MS の例 ( http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx ) からのものです。そしてもちろん、私は以前に例全体を試しました. 事は、私が得たのが1
. 読み取るデータがもうないことを通知するにはどうすればよいですか?
(コンパイラはMiGWです。)
MS の例が壊れているようです。これはそこにあるコメントの 1 つです:親プロセスは、CreateProcess() 呼び出しの後に子プロセスに渡すハンドルを閉じる必要があります。そうしないと、親が ReadFile() を永遠に待機することになります。
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
cout << "\n->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(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0);
STARTUPINFO siStartInfo;
PROCESS_INFORMATION piProcInfo;
ZeroMemory( &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
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
CHAR chBuf[4096];
DWORD dwRead;
// ~~~~~~this loop here~~~~~- how to break out of it ?
while(true) {
cout << "ReadFile: " << ReadFile( g_hChildStd_OUT_Rd, chBuf, 4096, &dwRead, NULL) << endl;
}
cout << "\n->End of parent execution.\n";
}