我有一个简单的程序(在C中)创建两个子进程,等待每个继承的管道,并将输出放在一个文件中.
一切顺利,除了在两个管道上的一些写入/读取周期之后,当小孩结束时,调用ReadFile块,等待管道上的数据.我使用以下模式:
- ...
- //create pipe1
- CreatePipe(&hReadDup,&hWrite,&saAttr,0);
- DuplicateHandle(GetCurrentProcess(),hReadDup,GetCurrentProcess(),&hRead,FALSE,DUPLICATE_SAME_ACCESS);
- CloseHandle(hReadDup);
- si.cb = sizeof(si);
- si.dwFlags = STARTF_USESTDHANDLES;
- si.hStdOutput = hWrite;
- CreateProcess( NULL,const_cast<LPWSTR>(cmd2.c_str()),//the command to execute
- NULL,NULL,TRUE,&si,//si.
- &pi
- );
- ...
- CloseHandle(hWrite); // EDIT: this was the operation not properly done!
- while(cont){
- ...
- cont = ReadFile(hRead,buf,50,&actual,NULL);
- ...
- }
- ...