I have server process, its allocate console and redirect output and input to that console. With ShellExecute this server process spawn some clients, these client know server ProcessID. So, i try AttachConsole with next class:
Console::Console(DWORD dwProcessId)
{
if (dwProcessId) {
AttachConsole(dwProcessId);
}
else
AllocConsole();
CONSOLE_SCREEN_BUFFER_INFO coninfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
coninfo.dwSize.Y = 500;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
int hConHandle;
long lStdHandle;
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
FILE *fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );
std::ios::sync_with_stdio();
}
Console::~Console()
{
FreeConsole();
}
But it doesnt work, it even erase output to console from server process. Hmm.. May only one process can output to console. Is it possible to send output to console from many processes?
You should open
CONOUT$usingCreateFile. Attaching to a console does not change the starndard handles that your process has inherited.