Here is one idea to pipe program output to richedit:
DWORD CALLBACK CopyStreamCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG FAR *pcb)
{
return !ReadFile((HANDLE)dwCookie, pbBuff, cb, (DWORD*)pcb, NULL);
}
int RunCmd(HWND hWndOut, TCHAR *szCmd)
{
HANDLE hRead, hWrite;
SECURITY_ATTRIBUTES sa;
STARTUPINFO si;
PROCESS_INFORMATION pi;
DWORD nRead, nWrite;
char szOut[260];
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hRead, &hWrite, &sa, 0)) {
nWrite = wsprintf(szOut, "Error: %d\n", GetLastError());
SendMessage(hWndOut, EM_REPLACESEL, 0, (LPARAM)szOut);
} else {
memset(&si, 0, sizeof(si));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.hStdOutput = hWrite;
si.hStdError = hWrite;
si.wShowWindow = SW_HIDE;
if (!CreateProcess(NULL, szCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
nWrite = wsprintf(szOut, "Error: %d\n", GetLastError());
SendMessage(hWndOut, EM_REPLACESEL, 0, (LPARAM)szOut);
} else {
EDITSTREAM eStream;
WaitForSingleObject(pi.hProcess, 1000);
DWORD dwTicks = GetTickCount();
SetWindowText(hStatus, "");
CloseHandle(hWrite);
eStream.dwCookie = (DWORD_PTR)hRead;
eStream.dwError = 0;
eStream.pfnCallback = CopyStreamCallback;
SendMessage(hWndOut, WM_SETTEXT, 0, 0);
while (TRUE) {
if (!PeekNamedPipe(hRead, NULL, 0, NULL, &nRead, NULL)) break;
if (nRead) {
SendMessage(hWndOut, EM_STREAMIN, (WPARAM) SF_TEXT, (LPARAM)&eStream);
} else {
DWORD nExit;
GetExitCodeProcess(pi.hProcess,&nExit);
if (nExit != STILL_ACTIVE) break;
}
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
dwTicks = GetTickCount() - dwTicks;
wsprintf(szOut, "\nTime: %u ticks", dwTicks);
SendMessage(hWndOut, EM_SETSEL, -1, -1);
SendMessage(hWndOut, EM_REPLACESEL, 0, (LPARAM)szOut);
SetWindowText(hStatus, szOut);
}
CloseHandle(hRead);
}
return 0;
}
Not very well tested, so fixes are welcome
EDIT: RunCc_a1.zip another small test program
EDIT: RunCc1_a1.zip another small test program updated
EDIT: RunCc2_a1.zip another small test program updated again