Skip to content

Use NtQueryVolumeInformationFile instead of GetFileType to check if a handle references a console - #356

Merged
Leonard Hecker (lhecker) merged 2 commits into
MicrosoftDocs:mainfrom
YexuanXiao:patch-1
Oct 16, 2026
Merged

Leonard Hecker (lhecker) merged 2 commits into
MicrosoftDocs:mainfrom
YexuanXiao:patch-1

Conversation

@YexuanXiao

@YexuanXiao Yexuan Xiao (YexuanXiao) commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

GetFileType returns FILE_TYPE_CHAR for both handles to a console and handles to NUL, which may cause errors. See the following link for details and discussion: microsoft/STL#6483 (comment) and rust-lang/rust#164170.

Many programs also use GetConsoleMode (including STL and UCRT), which is inefficient compared to NtQueryVolumeInformationFile, so I also add a suggestion in the note of GetConsoleMode. GetConsoleMode can be 100 times slower than NtQueryVolumeInformationFile because inter-process communication is required, resulting in console output throughput being only half of the optimal.

FILE_DEVICE_CONSOLE is available starting with Windows 8.

@learn-build-service-prod

Copy link
Copy Markdown
Contributor

Learn Build status updates of commit 40e5da0:

💡 Validation status: suggestions

File Status Preview URL Details
docs/console-handles.md 💡Suggestion View Details

docs/console-handles.md

  • Line 5, Column 12: [Suggestion: ms-author-invalid - See documentation] Invalid value for 'ms.author', 'miniksa' is not a valid Microsoft alias.

For more details, please refer to the build report.

Note: Your PR may contain errors or warnings or suggestions unrelated to the files you changed. This happens when external dependencies like GitHub alias, Microsoft alias, cross repo links are updated. Please use these instructions to resolve them.

@learn-build-service-prod

Copy link
Copy Markdown
Contributor

PoliCheck Scan Report

The following report lists PoliCheck issues in PR files. Before you merge the PR, you must fix all severity-1 and severity-2 issues. The AI Review Details column lists suggestions for either removing or replacing the terms. If you find a false positive result, mention it in a PR comment and include this text: #policheck-false-positive. This feedback helps reduce false positives in future scans.

✅ No issues found

More information about PoliCheck

Information: PoliCheck | Severity Guidance | Term
For any questions: Try searching the learn.microsoft.com contributor guides or post your question in the Learn support channel.

@YexuanXiao

Yexuan Xiao (YexuanXiao) commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Dustin L. Howett (@DHowett) Leonard Hecker (@lhecker) Could you take a look? This problem affects not only the STL but also the UCRT, which also uses the inefficient GetConsoleMode.

@learn-build-service-prod

Copy link
Copy Markdown
Contributor

Learn Build status updates of commit a713593:

💡 Validation status: suggestions

File Status Preview URL Details
docs/console-handles.md 💡Suggestion View Details
docs/getconsolemode.md 💡Suggestion View Details

docs/console-handles.md

  • Line 5, Column 12: [Suggestion: ms-author-invalid - See documentation] Invalid value for 'ms.author', 'miniksa' is not a valid Microsoft alias.

docs/getconsolemode.md

  • Line 5, Column 12: [Suggestion: ms-author-invalid - See documentation] Invalid value for 'ms.author', 'miniksa' is not a valid Microsoft alias.

For more details, please refer to the build report.

Note: Your PR may contain errors or warnings or suggestions unrelated to the files you changed. This happens when external dependencies like GitHub alias, Microsoft alias, cross repo links are updated. Please use these instructions to resolve them.

@learn-build-service-prod

Copy link
Copy Markdown
Contributor

PoliCheck Scan Report

The following report lists PoliCheck issues in PR files. Before you merge the PR, you must fix all severity-1 and severity-2 issues. The AI Review Details column lists suggestions for either removing or replacing the terms. If you find a false positive result, mention it in a PR comment and include this text: #policheck-false-positive. This feedback helps reduce false positives in future scans.

✅ No issues found

More information about PoliCheck

Information: PoliCheck | Severity Guidance | Term
For any questions: Try searching the learn.microsoft.com contributor guides or post your question in the Learn support channel.

@lhecker Leonard Hecker (lhecker) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry for being late! I read this on my phone after waking up one day and then promptly forgot about it lol. I think everyone knows this personally. 😄

@lhecker
Leonard Hecker (lhecker) merged commit 457d942 into MicrosoftDocs:main Oct 16, 2026
3 checks passed
Martin Storsjö (mstorsjo) pushed a commit to llvm/llvm-project that referenced this pull request Nov 27, 2026
Windows 8+ makes it possible to use `NtQueryVolumeInformationFile` to
determine whether a handle is a console. `GetConsoleMode` can be 100
times slower than `NtQueryVolumeInformationFile` because inter-process
communication is required. I also contacted the Windows Console team to
clarify the availability of the API; see the relevant discussion at
microsoft/STL#6483,
MicrosoftDocs/Console-Docs#356, and
rust-lang/rust#164170.
llvm-sync Bot pushed a commit to arm/arm-toolchain that referenced this pull request Nov 27, 2026
Windows 8+ makes it possible to use `NtQueryVolumeInformationFile` to
determine whether a handle is a console. `GetConsoleMode` can be 100
times slower than `NtQueryVolumeInformationFile` because inter-process
communication is required. I also contacted the Windows Console team to
clarify the availability of the API; see the relevant discussion at
microsoft/STL#6483,
MicrosoftDocs/Console-Docs#356, and
rust-lang/rust#164170.
llvm-upstreamsync Bot pushed a commit to qualcomm/cpullvm-toolchain that referenced this pull request Nov 27, 2026
Windows 8+ makes it possible to use `NtQueryVolumeInformationFile` to
determine whether a handle is a console. `GetConsoleMode` can be 100
times slower than `NtQueryVolumeInformationFile` because inter-process
communication is required. I also contacted the Windows Console team to
clarify the availability of the API; see the relevant discussion at
microsoft/STL#6483,
MicrosoftDocs/Console-Docs#356, and
rust-lang/rust#164170.
Victor Vianna (victorvianna) pushed a commit to victorvianna/libcxx that referenced this pull request Nov 27, 2026
Windows 8+ makes it possible to use `NtQueryVolumeInformationFile` to
determine whether a handle is a console. `GetConsoleMode` can be 100
times slower than `NtQueryVolumeInformationFile` because inter-process
communication is required. I also contacted the Windows Console team to
clarify the availability of the API; see the relevant discussion at
microsoft/STL#6483,
MicrosoftDocs/Console-Docs#356, and
rust-lang/rust#164170.

NOKEYCHECK=True
GitOrigin-RevId: a865665559b2144da44a580a0fb8517d7c458fed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants