CloseHandle() ~ (Double Console Buffer)

Greetings,

I was interested in having two buffers that I could swap between on the windows console. Thankfully, after spending some time on msdn, I managed to do it.
The code follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <Windows.h>

int main (int argc, char** argv)
{
	

	HANDLE buffer1 = GetStdHandle( STD_OUTPUT_HANDLE );
	HANDLE buffer2 = CreateConsoleScreenBuffer( GENERIC_WRITE,
                                                    0,
			                            NULL,
	                                            CONSOLE_TEXTMODE_BUFFER,
						    NULL );

	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;
}


This is working as intended.

My question is about the function CloseHandle().

I wonder if I should also be calling it with buffer1 or if I shouldn't be calling it at all in the first place.

PS: System("PAUSE") is only being used because this is purely for test purposes.
Last edited on
Topic archived. No new replies allowed.