I run a command to cmd.exe with CreateProcess()
. The command itself has an endless output, so I use a function that I modified from this answer to get partial outputs into a string.
#define BUFSIZE 4096
HANDLE g_hChildStd_OUT_Rd = NULL;
std::string ReadFromPipe(PROCESS_INFORMATION piProcInfo) {
DWORD dwRead;
CHAR chBuf[BUFSIZE];
ReadFile( g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
std::string s(chBuf, dwRead);
return s;
}
But this code creates some problems.
Firstly, every time I call it, it may freeze the program while it waits for the output to buffer to 4096 bytes.
Secondly, it will always only get the next 4096 bytes in the output queue. (even if the current output is a lot larger)
What I would like would be to call the function and get all the data that was outputed in the meanwhile and also be able to set a minimum number of bytes to get (instead of the buffer). If the minimum number of bytes is not available yet, I would prefer it skips ReadFile()
entirely and just return false
. (instead of freezing the application)
Would this be possible?