Did I forget something? (Double Console Buffer for Windows)
I tried to make / test a double buffer for the console on Windows using windows.h
.
After some research on msdn, I came up with the following:
`
//Free current console
if ( FreeConsole() == 0 ) return GetLastError();
//Get clean console
if ( AllocConsole() == 0 ) return GetLastError();
HANDLE buffer1 = GetStdHandle( STD_OUTPUT_HANDLE );
HANDLE buffer2 = CreateConsoleScreenBuffer( GENERIC_WRITE,
0,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL );
COORD begin;
begin.X = 0;
begin.Y = 0;
SetConsoleCursorPosition(buffer1, begin);
DWORD writen;
WriteConsole( buffer1,
L"Milk\n",
5,
&writen,
NULL );
WriteConsole( buffer2,
L"Melk\n",
5,
&writen,
NULL );
system("PAUSE");
SetConsoleActiveScreenBuffer( buffer2 );
WriteConsole( buffer2,
L"Malk\n",
5,
&writen,
NULL );
WriteConsole( buffer1,
L"Mulk\n",
5,
&writen,
NULL );
system("PAUSE");
SetConsoleActiveScreenBuffer( buffer1 );
system("PAUSE");
CloseHandle( buffer2 );
return 0; //End of main
Fortunately, this works as intended.
At first, the screen output looks like this:
Milk
Then this:
Melk
Malk
And the conclusion:
Milk
Mulk
And I have a few questions about this:
1) Do you really need to use FreeConsole()
and AllocConsole()
?
2) Should I use CloseHandle()
buffer1 for too? Or should I not be using it for buffer2?
3) Anything that you consider important to indicate.
PS: This is my first question and I hope I am not breaking any of the guidelines.
PS²: I only used it system("PAUSE")
because it was a test and you shouldn't worry about me using it in real software.
Found the answers if anyone is interested:
1) I learned that it doesn't matter. However, if you execute your program from cmd to make sure another console exits.
2) CloseHandle()
should only be used for the console screen buffer. The standard output is processed automatically.