creating a pipe for stdin could be useful. maybe creating one for stdout
and stderr which is always read too.
start.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
start.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
start.hStdError = GetStdHandle(STD_ERROR_HANDLE);
HANDLE hpStdInput[2] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
HANDLE hpStdOut[2] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
if (start.hStdInput == INVALID_HANDLE_VALUE)
{
CreatePipe(&hpStdInput[0], &hpStdInput[1], NULL, 0);
start.hStdInput = hpStdInput[0];
}
if (start.hStdOutput == INVALID_HANDLE_VALUE || start.hStdError == INVALID_HANDLE_VALUE)
{
CreatePipe(&hpStdOutput[0], &hpStdOutput[1], NULL, 0);
if (start.hStdOutput == INVALID_HANDLE_VALUE)
start.hStdOutput = hpStdOutput[1];
if (start.hStdError == INVALID_HANDLE_VALUE)
start.hStdError = hpStdOutput[1];
}
sec_attrs.nLength = sizeof (sec_attrs);
sec_attrs.lpSecurityDescriptor = NULL;
sec_attrs.bInheritHandle = FALSE;
if (CreateProcess (NULL, cmdline, &sec_attrs, NULL, TRUE, 0,
NULL, NULL, &start, &child))
{
if (wait_for_child)
{
if (hpStdOutput[0] != INVALID_HANDLE_VALUE)
{
HANDLE handles[2] = { child.hProcess, hpStdOutput[0] };
while (WaitForMultipleObjects (2, handles, FALSE, INFINITE) == WAIT_OBJECT_0 + 1)
{
char buffer[1024];
DWORD dwRead;
ReadFile (hpStdOutput[0], buffer, 1024, &dwRead, NULL);
}
} else {
WaitForSingleObject (child.hProcess, INFINITE);
}
GetExitCodeProcess (child.hProcess, &retval);
}
CloseHandle (child.hThread);
CloseHandle (child.hProcess);
}