I have followed the example here for reading from a pipe, but ReadFile fails and GetLastError() shows me that the pipe is broken.
I have created and used (successfully) a pipe earlier in the program, but I closed all the handles and used entirely new variables for the new pipe just to be sure.
Any ideas why this doesn’t work?
HANDLE g_hChildStd_OUT_Rd2 = NULL;
HANDLE g_hChildStd_OUT_Wr2 = NULL;
SECURITY_ATTRIBUTES saAttr2;
STARTUPINFO si2;
PROCESS_INFORMATION pi2;
ZeroMemory( &si2, sizeof(si2) );
si2.cb = sizeof(si2);
ZeroMemory( &pi2, sizeof(pi2) );
//create pipe
saAttr2.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr2.bInheritHandle = TRUE;
saAttr2.lpSecurityDescriptor = NULL;
CreatePipe(&g_hChildStd_OUT_Rd2, &g_hChildStd_OUT_Wr2, &saAttr2, 0);
//create child process
bSuccess = FALSE;
memset(szCmdLine, 0, MAX_PATH);
sprintf(szCmdLine, "ffmpeg.exe -i output.mp3");
ZeroMemory( &pi2, sizeof(PROCESS_INFORMATION) );
ZeroMemory( &si2, sizeof(STARTUPINFO) );
si2.cb = sizeof(STARTUPINFO);
si2.hStdOutput = g_hChildStd_OUT_Wr2;
si2.dwFlags |= STARTF_USESTDHANDLES;
CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si2, &pi2);
//read from pipe
CloseHandle(g_hChildStd_OUT_Wr2);
memset(chBuf, 0, BUFSIZE);
for (;;)
{
bSuccess = ReadFile( g_hChildStd_OUT_Rd2, chBuf, BUFSIZE, &dwRead, NULL);
[bSuccess is 0 and GetLastError() returns error 109]
........
“Broken pipe” is a normal error when the other end closes the pipe. In your case, this either means there is no “other end”, or the other application hasn’t written anything to its
stdout.I’ve modified your code to be a compilable test case – there are two cases in which
ReadFilefails without having read any data for me:CreateProcessfailed. I’ve added anassertso that problem will be recognizable. It would of course result in a broken pipe.stderrto the pipe as well, and as myechoexample shows, this will receive both streams on the other end of the pipe now.Code: