一个api把printf打印显示到控制台上

来源:互联网 发布:mac tracert 编辑:程序博客网 时间:2024/04/26 09:33
void CGlobalFunc::RedirectIOToConsole()
{
int hConHandle;


//long lStdHandle;
HANDLE lStdHandle;


CONSOLE_SCREEN_BUFFER_INFO coninfo;


FILE *fp;
// allocate a console for this app
bool br = AllocConsole() == TRUE;
if(!br)
return;
// set the screen buffer to be big enough to let us scroll text
br = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo) == TRUE;
assert( br );
coninfo.dwSize.Y = 5000;
br = SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize) == TRUE;
assert( br );
// redirect unbuffered STDOUT to the console
lStdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
assert( lStdHandle != INVALID_HANDLE_VALUE );
assert( lStdHandle != NULL );
hConHandle = _open_osfhandle((intptr_t)lStdHandle, _O_TEXT);
assert( hConHandle != -1 );
fp = _fdopen( hConHandle, "w" );
assert(fp);
*stdout = *fp;
int ir = setvbuf( stdout, NULL, _IONBF, 0 );
assert( ir ==0 );
// redirect unbuffered STDIN to the console
lStdHandle = GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle((intptr_t)lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
ir = setvbuf( stdin, NULL, _IONBF, 0 );
assert( ir ==0 );
// redirect unbuffered STDERR to the console
lStdHandle = GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle((intptr_t)lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stderr = *fp;
ir = setvbuf( stderr, NULL, _IONBF, 0 );
assert( ir ==0 );
// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog 
// point to console as well
ios::sync_with_stdio( true );

}


提供 夏思畅 

原创粉丝点击