Little example to demonstrate capturing of console input.
To update your edit: put the capture functionallity into a class object and use a timer in your application window for cyclic update to the edit control.
Best regards.
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
#include <malloc.h>
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"gdi32.lib")
WNDPROC __wndsuper = 0;
LRESULT FAR PASCAL __wndproc(HWND h,unsigned int m,WPARAM w,LPARAM l)
{
switch(m)
{
case WM_CLOSE : DestroyWindow(h); break;
case WM_DESTROY: PostQuitMessage(0); break;
}
return CallWindowProc(__wndsuper,h,m,w,l);
}
HGLOBAL CatchConsole(const TCHAR* exe,const TCHAR* param=0);
int FAR PASCAL _tWinMain(HINSTANCE h,HINSTANCE p,LPTSTR c,int sw)
{
HWND hwnd;
HGLOBAL hmem;
MSG msg;
hmem = c && *c ? CatchConsole(c): CatchConsole(__TEXT("cmd.exe"),__TEXT("/c dir /s C:\\*.*"));
if(!hmem) return 1;
hwnd = CreateWindowEx(0,__TEXT("EDIT"),__TEXT("CatchConsole"),
WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_WANTRETURN,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,h,0);
if(IsWindow(hwnd))
{
#if _WIN64
__wndsuper = (WNDPROC)SetWindowLongPtr(hwnd,GWLP_WNDPROC,(LONG_PTR)__wndproc);
#else
__wndsuper = (WNDPROC)SetWindowLong(hwnd,GWL_WNDPROC,(LONG)__wndproc);
#endif
GlobalFree((HGLOBAL)SendMessage(hwnd,EM_GETHANDLE,0,0));
SendMessage(hwnd,EM_SETHANDLE,(WPARAM)hmem,0);
SendMessage(hwnd,WM_SETFONT,(WPARAM)GetStockObject(ANSI_FIXED_FONT),0);
SendMessage(hwnd,WM_SETICON,ICON_BIG,(LPARAM)LoadIcon(0,IDI_ASTERISK));
ShowWindow(hwnd,sw);
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
class CMemWrite {
public:
CMemWrite(){ _hmem=0;_seek=_size=0; }
~CMemWrite(){ if(_hmem) GlobalFree(_hmem); }
HGLOBAL Detach()
{
HGLOBAL h; return h=_hmem,_hmem=0,h;
}
void Write(const char* lp,const unsigned int cb)
{
if(Grow((1+cb)*sizeof(TCHAR)))
{
TCHAR* lpto = (TCHAR*)GlobalLock(_hmem);
unsigned int size;
lpto += _seek / sizeof(TCHAR);
size = (_size - _seek) / sizeof(TCHAR);
if(sizeof(TCHAR)==sizeof(short))
MultiByteToWideChar(CP_OEMCP,0,lp,cb,lpto,size);
else
memcpy(lpto,lp,cb);
lpto[cb] = 0;
_seek += cb*sizeof(TCHAR);
GlobalUnlock(_hmem);
}
}
protected:
int Grow(const unsigned int add)
{
if(_size>=(_seek+add)) return 1;
_size = 256 + ((_seek + add)<<1);
_hmem = _hmem ? GlobalReAlloc(_hmem,_size,GMEM_MOVEABLE) : GlobalAlloc(GMEM_MOVEABLE,_size);
return _hmem?1:0;
}
private:
HGLOBAL _hmem;
unsigned int _size;
unsigned int _seek;
};
HGLOBAL CatchConsole(const TCHAR* exe,const TCHAR* param)
{
CMemWrite write;
STARTUPINFO stari;
PROCESS_INFORMATION proci;
int ok;
TCHAR* command;
size_t le,lp;
SECURITY_ATTRIBUTES sa;
HANDLE hproc;
HANDLE hRead,
hWrite;
unsigned long avail;
unsigned long read;
char out[0x400];
if(!exe) return 0;
memset(&proci,0,sizeof(proci));
memset(&stari,0,sizeof(stari));
stari.cb = sizeof(stari);
stari.lpTitle = __TEXT("---");
stari.dwXCountChars = 128;
stari.dwYCountChars = 300;
stari.dwFlags = STARTF_USESHOWWINDOW|STARTF_USECOUNTCHARS;
stari.wShowWindow = SW_HIDE;
stari.hStdInput = 0;
stari.hStdOutput = 0;
stari.hStdError = 0;
hRead = hWrite = 0;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = 0;
sa.bInheritHandle = 1;
do
{
hproc = ::GetCurrentProcess();
if(!::CreatePipe(&hRead, &stari.hStdOutput, &sa, 0)) break;
if(!::DuplicateHandle(hproc,stari.hStdOutput,hproc,&stari.hStdError,0,1,DUPLICATE_SAME_ACCESS)) break;
if(!::CreatePipe(&stari.hStdInput, &hWrite, &sa, 0)) break;
stari.dwFlags |= STARTF_USESTDHANDLES;
le = _tcslen(exe);
lp = param?_tcslen(param):0;
command = (TCHAR*)malloc(sizeof(TCHAR)*(le+lp+32)); if(!command) break;
_tcscpy_s(command,le+lp+32,exe);
if(0<lp)
{
_tcscpy_s(command+le,lp+32,__TEXT(" "));
_tcscpy_s(command+le+1,lp+32-1,param);
}
ok = CreateProcess(0,command,0,0,1,IDLE_PRIORITY_CLASS,0,0,&stari,&proci);
free(command);
while(ok)
{
ok = WAIT_TIMEOUT==WaitForSingleObject(proci.hProcess,10);
for(avail=0;PeekNamedPipe(hRead,0,0,0,&avail,0) && avail;avail=0)
{
if(ReadFile(hRead,out,min(sizeof(out),avail),&read,0) && read)
{
write.Write(out,read);
}
}
}
} while(0);
if(stari.hStdOutput) CloseHandle(stari.hStdOutput);
if(stari.hStdInput ) CloseHandle(stari.hStdInput);
if(stari.hStdError ) CloseHandle(stari.hStdError);
if(hRead ) CloseHandle(hRead);
if(hWrite) CloseHandle(hWrite);
return write.Detach();
}