NO

Author Topic: pipe output to richedit  (Read 344 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1612
pipe output to richedit
« on: January 09, 2017, 07:38:06 PM »
Here is one idea to pipe program output to richedit:
Code: [Select]
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
« Last Edit: January 20, 2017, 10:58:29 AM by TimoVJL »
May the source be with you

Offline jj2007

  • Member
  • *
  • Posts: 454
Re: pipe output to richedit
« Reply #1 on: January 12, 2017, 04:12:32 PM »
Works like a charm  :)

The MasmBasic equivalent: SetWin$ hMyEdit=Launch$("cmd.exe /c type Pipe2RichEdit.asc")  ;)
« Last Edit: January 12, 2017, 04:14:42 PM by jj2007 »