I create a pipe, run the program - the result is a sign. How to check that the pipe does not need to read - hangs on reading the pipe. Is it possible to find out before reading whether there is information in a pipe, or is the pipe empty?

I create a process

STARTUPINFOA si = {sizeof(si),0}; PROCESS_INFORMATION pi = {0,}; OVERLAPPED ov = {0,}; SECURITY_ATTRIBUTES sa = { sizeof(sa), 0 }; sa.bInheritHandle = 1; char usr[4]; // Данные HANDLE waits[2]; // Список ожидающих хандлов HANDLE pIn; HANDLE pOut; CreatePipe(&pOut,&si.hStdOutput,&sa,1); CreatePipe(&si.hStdInput,&pIn,&sa,1024); ov.hEvent = CreateEventA(0,1,0,0); si.hStdError = si.hStdOutput; si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW ; si.wShowWindow = SW_HIDE; waits[0] = pOut; int anti = 1000; bool working = true; bool read = true; int i=0; CreateProcessA(0,// какая нибудь консольная "c:\\windows\\Microsoft.NET\\Framework\\v2.0.50727\\MSBuild.exe" ,0,0,1,0,"\0\0\0\0",".",&si,&pi); waits[1] = pi.hProcess; 

Now poll

  while (working) { dw = -1; if (GetExitCodeThread(pi.hThread,&dw)) if (dw != 259) break; // Подобрано switch (WaitForMultipleObjects(2,waits,0,100)){ case 0: if (read) { ReadFile(pOut,usr,1,&dw,&ov); // Тут виснет if ((dw == 0) &&(GetLastError() == ERROR_IO_PENDING )) read = false; } else { dw = 0; GetOverlappedResult(pOut,&ov,&dw,0); if (dw != 0 ) read = true; } } 

Thought asynchronous reading will save the situation - but no - it does not work. The reading itself works well - I get all the bytes that sent the console application to the console. But the "end of the parcel" can not be determined.

Microsoft ms-example

  • This is a blocking operation, if there is no pipe on the other side, or there is nothing to read, it hangs and waits. The solution path is a separate stream for the pipes, or abandon them and switch to the socket. - NewView 5:27 pm
  • But can console I / O be easily forwarded through a socket? - nick_n_a

0