Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying cout/printf messages in non-console apps

Status
Not open for further replies.

rho

Programmer
Jan 15, 2001
2
AU
Hi guys,

I have a lib that was written for a console app that displays status messages via cout/printf commands. I now need to use this lib in a non-console app (ie a Win32 Application), but would still like to display the status messages. Has anyone done this before, or know how it could be done?

Thanks in advance,

Richard.
 
Yes, rho you will want to do the following (from Mozilla code):

// Users wants to make sure we have a console.
// Try to allocate one.
BOOL rc = ::AllocConsole();
if ( rc )
{

// Console allocated. Fix it up so that output works in
// all cases. See
// stdout
int hCrt = ::_open_osfhandle( (long)GetStdHandle( STD_OUTPUT_HANDLE ),
_O_TEXT );
cout.
if ( hCrt != -1 )
{
FILE *hf = ::_fdopen( hCrt, "w" );
if ( hf )
{
*stdout = *hf;
::fprintf( stdout, "stdout directed to dynamic console\n" );
}
}

// stderr
hCrt = ::_open_osfhandle( (long)::GetStdHandle( STD_ERROR_HANDLE ),
_O_TEXT );
if ( hCrt != -1 )
{
FILE *hf = ::_fdopen( hCrt, "w" );
if ( hf ) {
*stderr = *hf;
::fprintf( stderr, "stderr directed to dynamic console\n" );
}
}

// stdin?
/*Don't bother for now.
hCrt = ::_open_osfhandle( (long)::GetStdHandle( STD_INPUT_HANDLE ),
_O_TEXT );
if ( hCrt != -1 ) {
FILE *hf = ::_fdopen( hCrt, "r" );
if ( hf )
{
*stdin = *hf;
}

}
*/
}
else
{
// Failed. Probably because there already is one.
// There's little we can do, in any case.
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top