I have some difficulties getting my pipes work. I have the following code:
/* Set security attributes */
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if (CreatePipe(&Rread, &Rwrite, &sa, 0) == 0 || SetHandleInformation(Rread, HANDLE_FLAG_INHERIT, 0) == 0 || CreatePipe(&Wread, &Wwrite, &sa, 0) == 0 || SetHandleInformation(Wwrite, HANDLE_FLAG_INHERIT, 0) == 0)
{
/* Error */
}
/* Set process information */
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = Rwrite;
si.hStdError = Rwrite;
if (CreateProcess(NULL, argsCasted->cmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi) == 0)
{
/* Error */
}
for (;;)
{
PeekNamedPipe(Rread, NULL, 0, &a, NULL, NULL);
if (a > 0)
{
/* Write output somewhere... */
}
if (a == 0 && GetExitCodeProcess(pi.hProcess, &c) != 0 && c != STILL_ACTIVE) break;
Sleep(50);
}
/* CloseHandles... */
/* Free stuff... */
Now when I add si.hStdInput = Wread; (so that I can send input to the process), PeekNamedPipe() blocks.
I simplified the code a lot because it is part of a large multi-threaded application, which is too large to post here. If anyone needs more details from me to solve this problem, please post it here and I’ll add the requested details.
Thanks in advance
Jori.
PeekNamedPipewill block if there is no data in the pipe to read. You will have to make use of asynchronous/non-blocking I/O.Refer : asynchronous IO