Hi,

I'm having trouble on synchronization between the CreateProcess output and reading of the redirected pipe.

The problem is, I do not know after the WaitForSingleObject (in the while loop) whether the process
has written all the data to the pipe or not. If the process has written everything I hang on the second readfile.
I've tried it with INFINITE, but in such case with big output, the process stops (reason pipe buffer is full).
For information: Processes are for examples "ipconfig /displaydns" (short running, big output) or "defrag d: /a" (long running, small output)

Code:
	try {
		b_success = CreateProcess(nullptr,
			const_cast<LPWSTR>(command_w.c_str()),		// command line 
			nullptr,									// process security attributes 
			nullptr,									// primary thread security attributes 
			TRUE,										// handles are inherited 
			CREATE_NO_WINDOW,							// creation flags 
			nullptr,									// use parent's environment 
			nullptr,									// use parent's current directory 
			&si_start_info,								// STARTUPINFO pointer 
			&pi_proc_info);								// receives PROCESS_INFORMATION 
	}
	catch (std::exception&)	{return false;}
	if (!b_success) {return false;}
	
	auto wait_state = WaitForSingleObject(pi_proc_info.hProcess, 2000);

	DWORD dw_read;
	char ch_buf[BUFSIZE];
	auto b_success = FALSE;
	while (wait_state == WAIT_TIMEOUT) {
		ZeroMemory(&ch_buf, sizeof(ch_buf));
		try {
			b_success = ReadFile(g_hChildStd_OUT_Rd, ch_buf, BUFSIZE - 1, &dw_read, nullptr);
			if ((b_success) && (dw_read > 0)) {
				result += std::string(ch_buf);
			}
		}
		catch (std::exception&)
		{
			return false;
		}
		wait_state = WaitForSingleObject(pi_proc_info.hProcess, 2000);
	}
	if (wait_state == WAIT_OBJECT_0) {
		ZeroMemory(&ch_buf, sizeof(ch_buf));
		try {
			b_success = ReadFile(g_hChildStd_OUT_Rd, ch_buf, BUFSIZE - 1, &dw_read, nullptr);
			if ((b_success) && (dw_read > 0)) {
				result += std::string(ch_buf);
			}
		}
		catch (std::exception&)
		{
			return false;
		}
	}
Is the chosen solution completely wrong or did I miss something?

Thanks in advance.