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

Document the new 24H2 APIs #323

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 10 additions & 7 deletions docs/TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@
## [Console API Functions](console-functions.md)
### [AddConsoleAlias](addconsolealias.md)
### [AllocConsole](allocconsole.md)
### [AllocConsoleWithOptions](allocconsolewithoptions.md)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

### [AttachConsole](attachconsole.md)
### [ClosePseudoConsole](closepseudoconsole.md)
### [ConsoleControl](consolecontrol.md)
### [CreateConsoleScreenBuffer](createconsolescreenbuffer.md)
### [CreatePseudoConsole](createpseudoconsole.md)
### [ConsoleControl](consolecontrol.md)
### [FillConsoleOutputAttribute](fillconsoleoutputattribute.md)
### [FillConsoleOutputCharacter](fillconsoleoutputcharacter.md)
### [FlushConsoleInputBuffer](flushconsoleinputbuffer.md)
Expand Down Expand Up @@ -94,6 +95,7 @@
### [ReadConsoleOutput](readconsoleoutput.md)
### [ReadConsoleOutputAttribute](readconsoleoutputattribute.md)
### [ReadConsoleOutputCharacter](readconsoleoutputcharacter.md)
### [ReleasePseudoConsole](releasepseudoconsole.md)
### [ResizePseudoConsole](resizepseudoconsole.md)
### [ScrollConsoleScreenBuffer](scrollconsolescreenbuffer.md)
### [SetConsoleActiveScreenBuffer](setconsoleactivescreenbuffer.md)
Expand All @@ -119,22 +121,23 @@
### [WriteConsoleOutputCharacter](writeconsoleoutputcharacter.md)

## [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)
### [ALLOC_CONSOLE_OPTIONS](alloc-console-options.md)
### [CHAR_INFO structure](char-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_HISTORY_INFO structure](console-history-info.md)
### [CONSOLE_READCONSOLE_CONTROL structure](console-readconsole-control.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)
### [CONSOLE_SELECTION_INFO structure](console-selection-info-str.md)
### [COORD structure](coord-str.md)
### [SMALL_RECT structure](small-rect-str.md)
### [FOCUS_EVENT_RECORD structure](focus-event-record-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)
### [FOCUS_EVENT_RECORD structure](focus-event-record-str.md)
### [SMALL_RECT structure](small-rect-str.md)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sorted these alphabetically.

### [WINDOW_BUFFER_SIZE_RECORD structure](window-buffer-size-record-str.md)

## [Console API Winevents](console-winevents.md)
63 changes: 63 additions & 0 deletions docs/alloc-console-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
title: ALLOC_CONSOLE_OPTIONS structure
description: See reference information about the ALLOC_CONSOLE_OPTIONS structure, which contains extended information about a console screen buffer.
author: lhecker
ms.author: lhecker
ms.topic: article
keywords: console, character mode applications, command line applications, terminal applications, console api
f1_keywords:
- consoleapi/ALLOC_CONSOLE_OPTIONS
- ALLOC_CONSOLE_OPTIONS
- PALLOC_CONSOLE_OPTIONS
topic_type:
- apiref
api_name:
- ALLOC_CONSOLE_OPTIONS
api_location:
- WinCon.h
api_type:
- HeaderDef
---

# ALLOC\_CONSOLE\_OPTIONS structure

Controls how **AllocConsoleWithOptions** allocates a console window.

## Syntax

```C
typedef struct _ALLOC_CONSOLE_OPTIONS {
ALLOC_CONSOLE_MODE mode;
BOOL useShowWindow;
WORD showWindow;
} ALLOC_CONSOLE_OPTIONS, *PALLOC_CONSOLE_OPTIONS;
```

## Members

**mode**
This parameter can be one of the following values:

| Value | Meaning |
|-|-|
| **ALLOC\_CONSOLE\_MODE\_DEFAULT** 0 | Allocate a console session if one was requested by the parent process. |
| **ALLOC\_CONSOLE\_MODE\_NEW\_WINDOW** 1 | Allocate a console session with a window, even if this process was created with **CREATE\_NO\_CONSOLE** or **DETACHED\_PROCESS**. |
| **ALLOC\_CONSOLE\_MODE\_NO\_WINDOW** 2 | Allocate a console session without a window, even if this process was created with **CREATE\_NEW\_WINDOW** or **DETACHED\_PROCESS**. |

**useShowWindow**
Specifies whether the **showWindow** parameter should be used.

**showWindow**
If **useShowWindow** is **TRUE**, this specifies the **nCmdShow** used to show the console window. See [**ShowWindow**](/windows/win32/api/winuser/nf-winuser-showwindow) for more information.

## Requirements

|   |   |
|-|-|
| Minimum supported client | Windows 11 24H2 (build 26100) \[desktop apps only\] |
| Minimum supported server | n/a |
| Header | ConsoleApi.h (via WinCon.h, include Windows.h) |

## See also

[**AllocConsoleWithOptions**](allocconsolewithoptions.md)
2 changes: 2 additions & 0 deletions docs/allocconsole.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ This function is primarily used by a graphical user interface (GUI) application

[Consoles](consoles.md)

[**AllocConsoleWithOptions**](allocconsolewithoptions.md)

[**AttachConsole**](attachconsole.md)

[**CreateProcess**](/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa)
Expand Down
82 changes: 82 additions & 0 deletions docs/allocconsolewithoptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
title: AllocConsoleWithOptions function
description: See reference information about the AllocConsoleWithOptions function, which allocates a new console for the calling process.
author: lhecker
ms.author: lhecker
ms.topic: article
keywords: console, character mode applications, command line applications, terminal applications, console api, conpty, pseudoconsole
topic_type:
- apiref
api_name:
- AllocConsoleWithOptions
api_location:
- Kernel32.dll
- API-MS-Win-Core-Console-l1-2-2.dll
- KernelBase.dll
api_type:
- DllExport
---

# AllocConsoleWithOptions function

Optionally allocates a new console for the calling process, while allowing you to specify the visibility of the new console window.

## Syntax

```C
HRESULT WINAPI AllocConsoleWithOptions(
_In_opt_ PALLOC_CONSOLE_OPTIONS allocOptions,
_Out_opt_ PALLOC_CONSOLE_RESULT result
);
```

## Parameters

*allocOptions* \[in, optional\]
A [**ALLOC\_CONSOLE\_OPTIONS**](alloc-console-options.md) structure that controls how this function allocates a window.

*result* \[out, optional\]
Receives one of the following values:

| Value | Meaning |
|-|-|
| **ALLOC\_CONSOLE\_RESULT\_NO\_CONSOLE** 0 | No console was created, because **ALLOC\_CONSOLE\_MODE\_DEFAULT** was used and the parent process asked for none to be created. |
| **ALLOC\_CONSOLE\_RESULT\_NEW\_CONSOLE** 1 | A new console session was created as a result of this call. The resulting behavior is identical to [**AllocConsole**](allocconsole.md). |
| **ALLOC\_CONSOLE\_RESULT\_EXISTING\_CONSOLE** 2 | The process has attached itself to an existing console session, inherited by the parent process. The resulting behavior is identical to [**AttachConsole**](attachconsole.md). |

## Return value

Type: **HRESULT**

If this method succeeds, it returns **S_OK**. Otherwise, it returns an **HRESULT** error code.

## Remarks

Unlike with [**AllocConsole**](allocconsole.md) or [**AttachConsole**](attachconsole.md), calling this method when already connected to a console session does not result in an error.
The *result* parameter will be set to **ALLOC\_CONSOLE\_RESULT\_EXISTING\_CONSOLE** in that case.

A process can use the [**FreeConsole**](freeconsole.md) function to detach itself from its current console. A console is closed when the last process attached to it terminates or calls **FreeConsole**.

## Requirements

|   |   |
|-|-|
| Minimum supported client | Windows 11 24H2 (build 26100) \[desktop apps only\] |
| Minimum supported server | n/a |
| Header | ConsoleApi.h (via WinCon.h, include Windows.h) |
| Library | Kernel32.lib |
| DLL | Kernel32.dll |

## See also

[Console Functions](console-functions.md)

[Consoles](consoles.md)

[Console Allocation Policy](console-allocation-policy.md)

[**AllocConsole**](allocconsole.md)

[**AttachConsole**](attachconsole.md)

[**FreeConsole**](freeconsole.md)
2 changes: 2 additions & 0 deletions docs/attachconsole.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ To compile an application that uses this function, define **\_WIN32\_WINNT** as

[Consoles](consoles.md)

[**AllocConsoleWithOptions**](allocconsolewithoptions.md)

[**AllocConsole**](allocconsole.md)

[**FreeConsole**](freeconsole.md)
Expand Down
10 changes: 7 additions & 3 deletions docs/closepseudoconsole.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ api_type:

# ClosePseudoConsole function

Closes a pseudoconsole from the given handle.
Shuts down and releases resources associated with the given pseudoconsole.

## Syntax

Expand All @@ -40,9 +40,13 @@ A handle to an active pseudoconsole as opened by [CreatePseudoConsole](createpse

## Remarks

Upon closing a pseudoconsole, client applications attached to the session will be terminated as well.
Closing a pseudoconsole will send **CTRL_CLOSE_EVENT** to each client application that is still connected. Until the applications have disconnected they may continue writing more output. Because of this, your application is expected to either close the output pipe before calling **ClosePseudoConsole** or to continue reading from the pipe until after **ClosePseudoConsole** has returned.

A final painted frame may arrive on the `hOutput` handle originally provided to [CreatePseudoConsole](createpseudoconsole.md) when this API is called. It is expected that the caller will drain this information from the communication channel buffer and either present it or discard it. Failure to drain the buffer may cause the Close call to wait indefinitely until it is drained or the communication channels are broken another way.
> [!NOTE]
> Starting Windows 11 24H2 (build 26100) **ClosePseudoConsole** will return immediately to avoid accidental deadlocks. Earlier versions will wait indefinitely for the pseudoconsole to exit. If you need to know when all clients have disconnected, simply continue reading from the output pipe until it has been closed on you.

> [!WARNING]
> As a consequence of the above, failure to either close or drain the output pipe may cause **ClosePseudoConsole** to wait indefinitely in earlier versions of Windows. To avoid deadlocks on older versions, don't call **ClosePseudoConsole** on the same thread that you're reading the output pipe from, unless the output pipe was previously closed by you or closed on you by the pseudoconsole.

## Requirements

Expand Down
47 changes: 47 additions & 0 deletions docs/console-allocation-policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: Console Allocation Policy
description: Learn more about the Console Allocation Policy, allowing you to create hybrid graphical/console applications.
author: lhecker
ms.author: lhecker
ms.topic: conceptual
keywords: console, character mode applications, command line applications, terminal applications, console api
---

# Console Allocation Policy

> [!NOTE]
> This feature requires Windows 11 24H2 (build 26100) or later.

Most applications on Windows are either of the **IMAGE_SUBSYSTEM_WINDOWS_GUI** or **IMAGE_SUBSYSTEM_WINDOWS_CUI** type.
The former is a typical graphical, windowed application, whereas the latter is what's commonly called a console or terminal application.
When running an application marked as **IMAGE_SUBSYSTEM_WINDOWS_CUI** it'll be allocated a console, unless it's executed inside an existing console session.
Additionally, executing such an application inside a shell like CMD or PowerShell will block until the application has finished executing.
Neither of these are true for **IMAGE_SUBSYSTEM_WINDOWS_GUI** applications.
It'll neither be allocated a console, nor block execution inside a shell.

Now what if you want to write an application that seems like a graphical application when run from Explorer, but you can also write debug output to the console, if run inside an existing console session?
To achieve this, build your application as an **IMAGE_SUBSYSTEM_WINDOWS_CUI** one (for instance with **/SUBSYSTEM:CONSOLE** in MSVC) and add the following application manifest:

```xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<application>
<windowsSettings>
<consoleAllocationPolicy xmlns="http://schemas.microsoft.com/SMI/2024/WindowsSettings">detached</consoleAllocationPolicy>
</windowsSettings>
</application>
</assembly>
```

The **IMAGE_SUBSYSTEM_WINDOWS_CUI** type informs shells that they need to block until your application has finished executing, while the application manifest informs the operating system to skip allocating a console.

## Requirements

| &nbsp; | &nbsp; |
|-|-|
| Minimum supported client | Windows 11 24H2 (build 26100) \[desktop apps only\] |
| Minimum supported server | n/a |

## See also

[**GetNumberOfConsoleInputEvents**](allocconsolewithoptions.md)
4 changes: 3 additions & 1 deletion docs/console-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ The following functions are used to access a console.
|-|-|
| [**AddConsoleAlias**](addconsolealias.md) | Defines a console alias for the specified executable. |
| [**AllocConsole**](allocconsole.md) | Allocates a new console for the calling process. |
| [**AllocConsoleWithOptions**](allocconsolewithoptions.md) | Optionally allocates a new console for the calling process, while allowing you to specify the visibility of the new console window. |
| [**AttachConsole**](attachconsole.md) | Attaches the calling process to the console of the specified process. |
| [**ClosePseudoConsole**](closepseudoconsole.md) | Closes a pseudoconsole from the given handle. |
| [**CreatePseudoConsole**](createpseudoconsole.md) | Allocates a new pseudoconsole for the calling process. |
| [**CreateConsoleScreenBuffer**](createconsolescreenbuffer.md) | Creates a console screen buffer. |
| [**CreatePseudoConsole**](createpseudoconsole.md) | Allocates a new pseudoconsole for the calling process. |
| [**FillConsoleOutputAttribute**](fillconsoleoutputattribute.md) | Sets the text and background color attributes for a specified number of character cells. |
| [**FillConsoleOutputCharacter**](fillconsoleoutputcharacter.md) | Writes a character to the console screen buffer a specified number of times. |
| [**FlushConsoleInputBuffer**](flushconsoleinputbuffer.md) | Flushes the console input buffer. |
Expand Down Expand Up @@ -65,6 +66,7 @@ The following functions are used to access a console.
| [**ReadConsoleOutput**](readconsoleoutput.md) | Reads character and color attribute data from a rectangular block of character cells in a console screen buffer. |
| [**ReadConsoleOutputAttribute**](readconsoleoutputattribute.md) | Copies a specified number of foreground and background color attributes from consecutive cells of a console screen buffer. |
| [**ReadConsoleOutputCharacter**](readconsoleoutputcharacter.md) | Copies a number of characters from consecutive cells of a console screen buffer. |
| [**ReleasePseudoConsole**](releasepseudoconsole.md) | Relinquishes ownership of the `HPCON` handle to the pseudoconsole, allowing it to automatically exit once all clients have disconnected. |
| [**ResizePseudoConsole**](resizepseudoconsole.md) | Resizes the internal buffers for a pseudoconsole to the given size. |
| [**ScrollConsoleScreenBuffer**](scrollconsolescreenbuffer.md) | Moves a block of data in a screen buffer. |
| [**SetConsoleActiveScreenBuffer**](setconsoleactivescreenbuffer.md) | Sets the specified screen buffer to be the currently displayed console screen buffer. |
Expand Down
1 change: 1 addition & 0 deletions docs/console-structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ms.assetid: 60f59616-d42b-469a-acf1-c1b71e68f560

The following structures are used to access a console.

- [**ALLOC\_CONSOLE\_OPTIONS**](alloc-console-options.md)
- [**CHAR\_INFO**](char-info-str.md)
- [**CONSOLE\_CURSOR\_INFO**](console-cursor-info-str.md)
- [**CONSOLE\_FONT\_INFO**](console-font-info-str.md)
Expand Down
76 changes: 76 additions & 0 deletions docs/releasepseudoconsole.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
title: ReleasePseudoConsole function
description: See reference information about the ReleasePseudoConsole function, which allows the pseudoconsole to exit once all clients have disconnected.
author: lhecker
ms.author: lhecker
ms.topic: article
keywords: console, character mode applications, command line applications, terminal applications, console api, conpty, pseudoconsole
f1_keywords:
- consoleapi/ReleasePseudoConsole
- ReleasePseudoConsole
topic_type:
- apiref
api_name:
- ReleasePseudoConsole
api_location:
- Kernel32.dll
- API-MS-Win-Core-Console-l1-2-2.dll
- KernelBase.dll
api_type:
- DllExport
---

# ReleasePseudoConsole function

Relinquishes ownership of the `HPCON` handle to the pseudoconsole, allowing it to automatically exit once all clients have disconnected.

## Syntax

```C
HRESULT WINAPI ReleasePseudoConsole(
_In_ HPCON hPC
);
```

## Parameters

*hPC* \[in\]
A handle to an active pseudoconsole as opened by [CreatePseudoConsole](createpseudoconsole.md).

## Return value

Type: **HRESULT**

If this method succeeds, it returns **S_OK**. Otherwise, it returns an **HRESULT** error code.
The call is not expected to fail unless the **hPC** argument is invalid in which case it returns **E_INVALIDARG**.

## Remarks

Before this method was introduced, the initial pseudoconsole API was inherently flawed: **ClosePseudoConsole** would only exit until after the pseudoconsole has finished using the output pipe. This meant that the pseudoconsole effectively kept your application alive. The **HPCON** handle owned by your application on the other hand kept the pseudoconsole session alive until **ClosePseudoConsole** was called. This created a lifetime and ownership loop. The way applications were expected to break this loop up was by manually waiting for the spawned terminal processes to exit and then call **ClosePseudoConsole**. This made the API failure prone and resulted in shutdown deadlocks in various applications.

**ReleasePseudoConsole** solves this problem: It allows you to relinquish your half of the ownership. This in turn allows the pseudoconsole to automatically exit as soon as all clients have disconnected. All you need to do now is to read from or write to your output and input pipe handles until they return a failure. This indicates to you that all clients have disconnected and the the pseudoconsole is about to exit on its own.

> [!WARNING]
> **ReleasePseudoConsole** does not deallocate the memory associated with the **HPCON**. You must still call **ClosePseudoConsole** once you're done using the **HPCON** instance. See [ClosePseudoConsole](closepseudoconsole.md) for important information about its correct usage.

## Examples

For a full walkthrough on using this function to establish a pseudoconsole session, please see [Creating a Pseudoconsole Session](creating-a-pseudoconsole-session.md).

## Requirements

| &nbsp; | &nbsp; |
|-|-|
| Minimum supported client | Windows 11 24H2 (build 26100) \[desktop apps only\] |
| Minimum supported server | n/a |
| Header | ConsoleApi.h (via WinCon.h, include Windows.h) |
| Library | Kernel32.lib |
| DLL | Kernel32.dll |

## See also

[Pseudoconsoles](pseudoconsoles.md)

[Creating a Pseudoconsole Session](creating-a-pseudoconsole-session.md)

[**ClosePseudoConsole**](closepseudoconsole.md)