Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2021 August Console-Docs pass #214

Merged
merged 16 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions docs/TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,20 @@
## [Console API Structures](console-structures.md)
### [CONSOLE_HISTORY_INFO structure](console-history-info.md)
### [CONSOLE_READCONSOLE_CONTROL structure](console-readconsole-control.md)
### [CONSOLE_SELECTION_INFO Structure](console-selection-info-str.md)
### [FOCUS_EVENT_RECORD structure](focus-event-record-str.md)
### [CONSOLE_SCREEN_BUFFER_INFOEX structure](console-screen-buffer-infoex.md)
### [CONSOLE_SELECTION_INFO structure](console-selection-info-str.md)
### [CONSOLE_CURSOR_INFO structure](console-cursor-info-str.md)
### [CONSOLE_FONT_INFO structure](console-font-info-str.md)
### [CONSOLE_FONT_INFOEX structure](console-font-infoex.md)
### [CONSOLE_SCREEN_BUFFER_INFO structure](console-screen-buffer-info-str.md)
### [CONSOLE_SCREEN_BUFFER_INFOEX structure](console-screen-buffer-infoex.md)
### [CHAR_INFO structure](char-info-str.md)
### [COORD structure](coord-str.md)
### [SMALL_RECT structure](small-rect-str.md)
### [INPUT_RECORD structure](input-record-str.md)
### [KEY_EVENT_RECORD structure](key-event-record-str.md)
### [MENU_EVENT_RECORD structure](menu-event-record-str.md)
### [MOUSE_EVENT_RECORD structure](mouse-event-record-str.md)
### [SMALL_RECT structure](small-rect-str.md)
### [FOCUS_EVENT_RECORD structure](focus-event-record-str.md)
### [WINDOW_BUFFER_SIZE_RECORD structure](window-buffer-size-record-str.md)

## [Console API Winevents](console-winevents.md)
2 changes: 1 addition & 1 deletion docs/char-info-str.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ api_type:
- HeaderDef
---

# `CHAR\_INFO structure`
# CHAR\_INFO structure
miniksa marked this conversation as resolved.
Show resolved Hide resolved

[!INCLUDE [not-recommended-banner](./includes/not-recommended-banner.md)]

Expand Down
2 changes: 1 addition & 1 deletion docs/clearing-the-screen.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ms.assetid: 2097cc53-13b9-4f29-9d2c-feea56a27cb8

# Clearing the Screen

There are four ways to clear the screen in a console application.
There are three ways to clear the screen in a console application.

## Example 1

Expand Down
4 changes: 3 additions & 1 deletion docs/console-control-handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ The function's *dwCtrlType* parameter identifies which control signal was receiv

A new thread is started inside the command-line client process to run the handler routines. More information on the timeout values and action of this thread can be found in the [**HandlerRoutine**](handlerroutine.md#remarks) function documentation.

For an example of a control handler function, see [Registering a Control Handler Function](registering-a-control-handler-function.md).
For an example of a control handler function, see [Registering a Control Handler Function](registering-a-control-handler-function.md).

Note that calling [**AttachConsole**](attachconsole.md), [**AllocConsole**](allocconsole.md), or [**FreeConsole**](freeconsole.md) will reset the table of control handlers in the client process to its initial state.
2 changes: 1 addition & 1 deletion docs/console-screen-buffers.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ An application can combine the foreground and background constants to achieve di

`FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE`

If no background constant is specified, the background is black, and if no foreground constant is specified, the text is black. For example, the following combination produces black text on a white background.
If no background constant is specified, the background is black, and if no foreground constant is specified, the text is black. For example, the following combination produces black text on a white background. Red, green, and blue are specified for the background which combines to a white background. No flag colors are specified for the foreground so it is black.

`BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED`

Expand Down
2 changes: 1 addition & 1 deletion docs/console-virtual-terminal-sequences.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ Note that these sequences are OSC “Operating system command” sequences, and

| Sequence | Description | Behavior |
|-------------------------------|---------------------------|----------------------------------------------------|
| ESC \] 0 ; <string> BEL | Set Icon and Window Title | Sets the console window’s title to <string>. |
| ESC \] 0 ; <string> BEL | Set Window Title | Sets the console window’s title to <string>. |
| ESC \] 2 ; <string> BEL | Set Window Title | Sets the console window’s title to <string>. |


Expand Down
2 changes: 1 addition & 1 deletion docs/creation-of-a-console.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Typically, a process uses [**AllocConsole**](allocconsole.md) to create a consol

A process can also create a console by specifying the **CREATE\_NEW\_CONSOLE** flag in a call to [**CreateProcess**](/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa). This method creates a new console that is accessible to the child process but not to the parent process. Separate consoles enable both parent and child processes to interact with the user without conflict. If this flag is not specified when a console process is created, both processes are attached to the same console, and there is no guarantee that the correct process will receive the input intended for it. Applications can prevent confusion by creating child processes that do not inherit handles of the input buffer, or by enabling only one child process at a time to inherit an input buffer handle while preventing the parent process from reading console input until the child has finished.

Creating a new console results in a new console window, as well as separate I/O screen buffers. The process associated with the new console uses the [**GetStdHandle**](getstdhandle.md) function to get the handles of the new console's input and screen buffers. These handles enable the process to access the console.
Creating a new console results in a new console window, as well as separate I/O buffers for [output to the screen](console-screen-buffers.md) and [input from the user](console-input-buffer.md). The process associated with the new console uses the [**GetStdHandle**](getstdhandle.md) function to get the handles of the new console's input and screen buffers. These handles enable the process to access the console.

When a process uses [**CreateProcess**](/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa), it can specify a [**STARTUPINFO**](/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfoa) structure, whose members control the characteristics of the first new console (if any) created for the child process. The **STARTUPINFO** structure specified in the call to **CreateProcess** affects a console created if the **CREATE\_NEW\_CONSOLE** flag is specified. It also affects a console created if the child process subsequently uses [**AllocConsole**](allocconsole.md). The following console characteristics can be specified:

Expand Down
23 changes: 20 additions & 3 deletions docs/getstdhandle.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,21 @@ The standard device. This parameter can be one of the following values.

| Value | Meaning |
|-|-|
| **STD_INPUT_HANDLE** (DWORD) -10 | The standard input device. Initially, this is the console input buffer, `CONIN$`. |
| **STD_OUTPUT_HANDLE** (DWORD) -11 | The standard output device. Initially, this is the active console screen buffer, `CONOUT$`. |
| **STD_ERROR_HANDLE** (DWORD) -12 | The standard error device. Initially, this is the active console screen buffer, `CONOUT$`. |
| **STD_INPUT_HANDLE** `((DWORD)-10)` | The standard input device. Initially, this is the console input buffer, `CONIN$`. |
| **STD_OUTPUT_HANDLE** `((DWORD)-11)` | The standard output device. Initially, this is the active console screen buffer, `CONOUT$`. |
| **STD_ERROR_HANDLE** `((DWORD)-12)` | The standard error device. Initially, this is the active console screen buffer, `CONOUT$`. |

> [!NOTE]
> The values for these constants are unsigned numbers, but are defined in the header files as a cast from a
signed number and take advantage of the C compiler rolling them over to just under the maximum 32-bit value. When interfacing with these handles in a language that does not parse the headers and is re-defining the constants, please be aware of this constraint. As an example, `((DWORD)-10)` is actually the unsigned number `4294967286`.

## Return value

If the function succeeds, the return value is a handle to the specified device, or a redirected handle set by a previous call to [**SetStdHandle**](setstdhandle.md). The handle has **GENERIC\_READ** and **GENERIC\_WRITE** access rights, unless the application has used **SetStdHandle** to set a standard handle with lesser access.

> [!TIP]
> It is not required to dispose of this handle with [**CloseHandle**](/windows/win32/api/handleapi/nf-handleapi-closehandle) when done. See [**Remarks**](#handle-disposal) for more information.

If the function fails, the return value is **INVALID\_HANDLE\_VALUE**. To get extended error information, call [**GetLastError**](/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror).

If an application does not have associated standard handles, such as a service running on an interactive desktop, and has not redirected them, the return value is **NULL**.
Expand All @@ -77,6 +84,16 @@ Some applications operate outside the boundaries of their declared subsystem; fo

Some applications may also vary their behavior on the type of inherited handle. Disambiguating the type between console, pipe, file, and others can be performed with [**GetFileType**](/windows/win32/api/fileapi/nf-fileapi-getfiletype).

### Handle disposal

It is not required to [**CloseHandle**](/windows/win32/api/handleapi/nf-handleapi-closehandle) when done with the handle retrieved from **GetStdHandle**. The returned value is simply a copy of the value stored in the process table. The process itself is generally considered the owner of these handles and their lifetime. Each handle is placed in the table on creation depending on the inheritance and launch specifics of the [**CreateProcess**](/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) call and will be freed when the process is destroyed.

Manual manipulation of the lifetime of these handles may be desirable for an application intentionally trying to replace them or block other parts of the process from using them. As a `HANDLE` can be cached by running code, that code will not necessarily pick up changes made via **SetStdHandle**. Closing the handle explicitly via **CloseHandle** will close it process-wide and the next usage of any cached reference will encounter an error.

Guidance for replacing a standard handle in the process table would be to get the existing `HANDLE` from the table with **GetStdHandle**, use **SetStdHandle** to place a new `HANDLE` in that is opened with **CreateFile** (or a similar function), then to close the retrieved handle.
miniksa marked this conversation as resolved.
Show resolved Hide resolved

There is no validation of the values stored as handles in the process table by either the **GetStdHandle** or **SetStdHandle** functions. Validation is performed at the time of the actual read/write operation such as **ReadFile** or **WriteFile**.

### Attach/detach behavior

When attaching to a new console, standard handles are always replaced with console handles unless **STARTF\_USESTDHANDLES** was specified during process creation.
Expand Down
6 changes: 3 additions & 3 deletions docs/includes/console-mode-flags.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
If the *hConsoleHandle* parameter is an input handle, the mode can be one or more of the following values. When a console is created, all input modes except **ENABLE\_WINDOW\_INPUT** are enabled by default.
If the *hConsoleHandle* parameter is an input handle, the mode can be one or more of the following values. When a console is created, all input modes except **ENABLE\_WINDOW\_INPUT** and **ENABLE\_VIRTUAL\_TERMINAL\_INPUT** are enabled by default.

| Value | Meaning |
|-|-|
| **ENABLE_ECHO_INPUT** 0x0004 | Characters read by the **[ReadFile](/windows/win32/api/fileapi/nf-fileapi-readfile)** or **[ReadConsole](../readconsole.md)** function are written to the active screen buffer as they are read. This mode can be used only if the **ENABLE_LINE_INPUT** mode is also enabled. |
| **ENABLE_ECHO_INPUT** 0x0004 | Characters read by the **[ReadFile](/windows/win32/api/fileapi/nf-fileapi-readfile)** or **[ReadConsole](../readconsole.md)** function are written to the active screen buffer as they are typed into the console. This mode can be used only if the **ENABLE_LINE_INPUT** mode is also enabled. |
| **ENABLE_INSERT_MODE** 0x0020 | When enabled, text entered in a console window will be inserted at the current cursor location and all text following that location will not be overwritten. When disabled, all following text will be overwritten. |
| **ENABLE_LINE_INPUT** 0x0002 | The **[ReadFile](/windows/win32/api/fileapi/nf-fileapi-readfile)** or **[ReadConsole](../readconsole.md)** function returns only when a carriage return character is read. If this mode is disabled, the functions return when one or more characters are available. |
| **ENABLE_MOUSE_INPUT** 0x0010 | If the mouse pointer is within the borders of the console window and the window has the keyboard focus, mouse events generated by mouse movement and button presses are placed in the input buffer. These events are discarded by **[ReadFile](/windows/win32/api/fileapi/nf-fileapi-readfile)** or **[ReadConsole](../readconsole.md)**, even when this mode is enabled. |
| **ENABLE_PROCESSED_INPUT** 0x0001 | CTRL+C is processed by the system and is not placed in the input buffer. If the input buffer is being read by **[ReadFile](/windows/win32/api/fileapi/nf-fileapi-readfile)** or **[ReadConsole](../readconsole.md)**, other control keys are processed by the system and are not returned in the **ReadFile** or **ReadConsole** buffer. If the **ENABLE_LINE_INPUT** mode is also enabled, backspace, carriage return, and line feed characters are handled by the system. |
| **ENABLE_QUICK_EDIT_MODE** 0x0040 | This flag enables the user to use the mouse to select and edit text.<br /><br />To enable this mode, use `ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS`. To disable this mode, use **ENABLE_EXTENDED_FLAGS** without this flag. |
| **ENABLE_QUICK_EDIT_MODE** 0x0040 | This flag enables the user to use the mouse to select and edit text. To enable this mode, use `ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS`. To disable this mode, use **ENABLE_EXTENDED_FLAGS** without this flag. |
| **ENABLE_WINDOW_INPUT** 0x0008 | User interactions that change the size of the console screen buffer are reported in the console's input buffer. Information about these events can be read from the input buffer by applications using the **[ReadConsoleInput](../readconsoleinput.md)** function, but not by those using **[ReadFile](/windows/win32/api/fileapi/nf-fileapi-readfile)** or **[ReadConsole](../readconsole.md)**. |
| **ENABLE_VIRTUAL_TERMINAL_INPUT** 0x0200 | Setting this flag directs the Virtual Terminal processing engine to convert user input received by the console window into **[Console Virtual Terminal Sequences](../console-virtual-terminal-sequences.md)** that can be retrieved by a supporting application through **[WriteFile](/windows/win32/api/fileapi/nf-fileapi-writefile)** or **[WriteConsole](../writeconsole.md)** functions.<br /><br />The typical usage of this flag is intended in conjunction with ENABLE_VIRTUAL_TERMINAL_PROCESSING on the output handle to connect to an application that communicates exclusively via virtual terminal sequences. |

Expand Down
2 changes: 1 addition & 1 deletion docs/input-and-output-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ms.assetid: 55a86d5d-d0b1-4d0c-b42f-7342809289ad

# Input and Output Methods

There are two different approaches to console I/O, the choice of which depends on how much flexibility and control an application needs. The high-level approach enables simple character stream I/O, but it limits access to a console's input and screen buffers. The low-level approach requires that developers write more code and choose among a greater range of functions, but it also gives an application more flexibility.
There are two different approaches to console I/O, the choice of which depends on how much flexibility and control an application needs. The high-level approach enables simple character stream I/O, but it limits access to a console's [input](console-input-buffer.md) and [screen](console-screen-buffers.md) buffers. The low-level approach requires that developers write more code and choose among a greater range of functions, but it also gives an application more flexibility.

> [!NOTE]
> The low-level approach is not recommended for new and ongoing development. Applications needing functionality from the low-level console I/O functions are encouraged to use **[virtual terminal sequences](console-virtual-terminal-sequences.md)** and explore our documentation on both **[classic functions versus virtual terminal](classic-vs-vt.md)** and **[the ecosystem roadmap](ecosystem-roadmap.md)**.
Expand Down
Loading