I could've sworn I've done this before, but I can't get it to work for the life of me this time round.
Code:
bool ConWnd::Write( const char* mesg )
{
if ( !consoleReady )
{
ELogging::S().LogAction( "Console not ready, allocating..." );
if ( !AllocConsole() )
{
ELogging::S().ActionFailed( "AllocConsole() failed" );
EReportBox( "ConWnd::Write: AllocConsole() failed" );
return false;
}
consoleHandle = CreateFile( "CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0 );
if ( consoleHandle == INVALID_HANDLE_VALUE )
{
ELogging::S().ActionFailed( "CreateFile() failed" );
EReportBox( "ConWnd::Write: CreateFile() failed" );
return false;
}
ELogging::S().ActionDone();
consoleReady = true;
}
//if ( !WriteFile(consoleHandle, mesg, static_cast<DWORD> (strlen(mesg)), 0, 0) )
//{
// DWORD eCode = GetLastError();
// ELogging::S().Log( Va("Writing to console failed (%d)", eCode) );
// EReportBox( "ConWnd::Write: Writing failed" );
// return false;
//}
if ( !SetStdHandle(STD_OUTPUT_HANDLE, consoleHandle) )
{
DWORD eCode = GetLastError();
ELogging::S().Log( Va("Error setting STD_OUTPUT_HANDLE to consoleHandle (%d)", eCode) );
EReportBox( "ConWnd::Write: Error setting output handle" );
return false;
}
std::cout << mesg;
return true;
}
I've gone through all the MSDN docs related to this and I'm *sure* I've done everything right.
Any takers?