Design Pattern Examples
Overview of object-oriented design patterns
enablevtmode.c
Go to the documentation of this file.
1
5
6#ifdef _MSC_VER
7#include <windows.h>
8#endif
9
10#include <stdlib.h>
11#include <stdio.h>
12
13#include "enablevtmode.h"
14
15#ifdef _MSC_VER
16DWORD INVALID_MODE = 0xffffffff;
17DWORD dwOriginalOutMode = 0xffffffff;
18HANDLE hOut = INVALID_HANDLE_VALUE;
19#endif
20
24static void _restoreVTMode(void)
25{
26#ifdef _MSC_VER
27 if (dwOriginalOutMode != INVALID_MODE && hOut != INVALID_HANDLE_VALUE)
28 {
29 SetConsoleMode(hOut, dwOriginalOutMode);
30 }
31
32#endif
33}
34
35
37// enableVTMode()
39void enableVTMode(void)
40{
41 atexit(_restoreVTMode);
42
43#ifdef _MSC_VER
44 hOut = GetStdHandle(STD_OUTPUT_HANDLE);
45 if (hOut != INVALID_HANDLE_VALUE)
46 {
47 if (!GetConsoleMode(hOut, &dwOriginalOutMode))
48 {
49 dwOriginalOutMode = INVALID_MODE;
50 DWORD lastError = GetLastError();
51 printf("Failed to get the Console output's original mode (code = 0x%x).\n", lastError);
52 }
53 else
54 {
55 DWORD dwRequestedOutModes = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
56 DWORD dwOutMode = dwOriginalOutMode | dwRequestedOutModes;
57 if (!SetConsoleMode(hOut, dwOutMode))
58 {
59 DWORD lastError = GetLastError();
60 printf("Failed to enable the Console output's virtual terminal mode (code = 0x%x).\n", lastError);
61 }
62 }
63 }
64 else
65 {
66 printf("Failed to get the Console's output handle.\n");
67 }
68#endif
69}
void enableVTMode(void)
Enables the virtual terminal processing mode on the current Windows Console. When the program ends,...
Definition: enablevtmode.c:39
static void _restoreVTMode(void)
Called by atexit() on program termination to restore the video mode.
Definition: enablevtmode.c:24
Declaration of the enableVTMode() function for configuring standard input to support the virtual term...
int INVALID_HANDLE_VALUE
Windows indicator of an invalid handle.