← Back to Pull requests

Use `NtQueryVolumeInformationFile` instead of `GetConsoleMode` to check whether a handle is a console #6483

Open
unknown opened this pull request with 438 lines diff across 5 files
View on GitHub
unknown unknown commented on Oct 09, 2026 16:17 UTC
Author

Earlier this year I looked into the Windows console in depth, and since it's open source, I had the opportunity to analyze how it's implemented.

Put simply, when program call GetConsoleMode (as well as any other console API, and when reading from or writing to the console handle), the following happens:

1. The app communicates with condrv.sys
2. condrv.sys communicates with conhost.exe (or openconsole.exe, which is the distributed version of conhost.exe) through a pipe-like file, where condrv.sys produces the following messages:
https://github.com/microsoft/terminal/blob/6676c6f938f7e98ad680f0cecedd3bafb6529734/dep/Console/condrv.h#L33-L40
3. conhost.exe processes these messages and write back

NtQueryVolumeInformationFile is not a console API; it merely queries the device (condrv.sys) info provided when the fs driver is registered (about 500 cycles), so it is much faster than GetConsoleMode.
This usage of NtQueryVolumeInformationFile has been supported since Windows 8, see <https://github.com/rust-lang/rust/pull/164170>.

[[print.fun] 10.2](https://eel.is/c++draft/iostream.format#print.fun-note-1) says that on Windows, GetConsoleMode is used to determine whether a handle refers to a console, so an LWG issue may be needed. However, since there is no observable difference, this patch does not need to wait for standards.

I just ran a simple test, and NtQueryVolumeInformationFile is 100 times faster than GetConsoleMode.

test program:

#include <iostream>
#include <chrono>

#include <windows.h>
#include <winternl.h>
#include <winioctl.h>

#pragma comment(lib, "ntdll.lib")

extern "C" {

    // Exported by ntdll.lib and can most efficiently query whether a HANDLE references a console.
    // Since the user mode header isn't provided, all needed declarations are declared manually.
    NTSYSAPI NTSTATUS NTAPI NtQueryVolumeInformationFile(
        HANDLE handle, PIO_STATUS_BLOCK ioStatus, PVOID information, ULONG length, ULONG informationClass);

} // extern "C"

// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_file_fs_device_information
typedef struct _FILE_FS_DEVICE_INFORMATION {
    DEVICE_TYPE DeviceType;
    ULONG Characteristics;
} FILE_FS_DEVICE_INFORMATION, * PFILE_FS_DEVICE_INFORMATION;

// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ne-wdm-_fsinfoclass
#define FileFsDeviceInformation 4

bool _Is_console_handle(HANDLE handle) {
    IO_STATUS_BLOCK iosb;
    FILE_FS_DEVICE_INFORMATION info;
    const auto status = NtQueryVolumeInformationFile(handle, &iosb, &info, sizeof(info), FileFsDeviceInformation);
    return (status >= 0) & (info.DeviceType == FILE_DEVICE_CONSOLE);
}

int main() {
    constexpr int N = 1000000;
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD mode;

    auto t0 = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < N; ++i)
        (void)_Is_console_handle(h);
    auto t1 = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < N; ++i)
        (void)GetConsoleMode(h, &mode);
    auto t2 = std::chrono::high_resolution_clock::now();

    auto ftNs = std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count() / N;
    auto cmNs = std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count() / N;

    std::cout << ftNs << " ns and " << cmNs << " ns\n";
}

I wrote a benchmark for std::print, but the results fluctuate a lot.

Diff: 438 lines (+328 / -110) across 5 files Reviewer: YexuanXiao Review duration: 76458m
All Vouch review confidence checks passed
Residual risk 0.29 = Change Risk (0.52) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 438 lines diff across 5 files
0.52
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
44%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.364
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #6483"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 44.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 52.3% (MEDIUM) · Higher = larger blast radius
52.3%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 44.7% · Lower = rushed or shallow review
44.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 28.9% · Safe to merge
28.9%
Repository