I am creating an application which will read the output from another application (which I did not write) which sends its output to a command prompt. I need to be able to write this information an "Edit" dialog within my GUI program. I have accomplished this using anonymous pipes and multi-threading (a worker thread for writing and another for reading). The program is initialized by CreateProcess(); Now, how can I get this to update the GUI in NEAR real-time. If there is a few second delay, that won't be an issue, but I cannot wait for the program to return 0; before the input is processed (which appears to be the current issue).
Here is some relevant code to how I am doing this. These are my callbacks from the threads.
DWORD WINAPI Arc_readPipe(LPVOID threadParam)
{
Arc_Redirect ar;
CHAR chBuf[BUFSIZE];
DWORD dwRead;
HANDLE hPipe = (HANDLE)threadParam;
HWND g1 = FindWindow("MyGUI",NULL);
HWND dlg = GetDlgItem(g1,IDO_WORLDOUT);
while(bRunThread)
{
while(TRUE)
{
if(!ReadFile(hPipe,chBuf,BUFSIZE,&dwRead,NULL) || !dwRead)
if(GetLastError() == ERROR_BROKEN_PIPE)
{
memset(chBuf,0,sizeof(chBuf));
break; // Normal exit
} else {
throw GetLastError();
}
chBuf[dwRead] = '\0';
//MessageBox(g1,"Read data","Read",MB_OK);
ar.appendText(dlg,chBuf);
}
}
return 1;
}
DWORD WINAPI Arc_writePipe(LPVOID threadParam)
{
Arc_Redirect ar;
DWORD dwWrote;
CHAR chBuf[BUFSIZE];
HANDLE hPipe = (HANDLE)threadParam;
HWND g1 = FindWindow("MyGUI",NULL);
HWND dlg = GetDlgItem(g1,IDO_WORLDOUT);
int nLength = GetWindowTextLength(GetDlgItem(g1,IDO_WORLDINPUT));
while(bRunThread)
{
if(GetDlgItemText(g1,IDO_WORLDINPUT,chBuf,BUFSIZE))
{
chBuf[sizeof(GetDlgItemText(g1,IDO_WORLDINPUT,NULL,NULL))] = '\0';
if(!WriteFile(hPipe,chBuf,sizeof(GetDlgItemText(g1,IDO_WORLDINPUT,NULL,NULL)),&dwWrote,NULL))
{
SetDlgItemText(g1,IDO_WORLDINPUT,NULL);
if(GetLastError() == ERROR_NO_DATA)
break; // Normal :)
else
MessageBox(g1,"Error: Could not WriteFile();","Error",MB_ICONERROR);
}
}
}
return 1;
}
Excuse the mess in the code; since I have been working on it, it's not as clean as it could be. bRunThread is a BOOL variable set in the global scope just to keep the thread looping. Any ideas?
Thanks for any help!