-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Fix -WindowStyle Hidden console window flash #27111
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
base: master
Are you sure you want to change the base?
Changes from all commits
5dfe5ae
4b4a8ef
1a86fe6
b2a7b30
86735ad
87933d3
6214139
add7938
4025927
6b44701
8b2913a
29560fa
76e8650
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,15 @@ public static int Start([MarshalAs(UnmanagedType.LPArray, ArraySubType = Unmanag | |
| { | ||
| ArgumentNullException.ThrowIfNull(args); | ||
|
|
||
| #if !UNIX | ||
| // On Windows with consoleAllocationPolicy=detached in the manifest, | ||
| // no console is auto-allocated by the OS. We must allocate one ourselves | ||
| // before anything touches CONOUT$/CONIN$ handles. | ||
| // On older Windows the manifest element is ignored and the OS auto-allocates | ||
| // a console, so GetConsoleWindow() != 0 and EarlyConsoleInit is a no-op. | ||
| EarlyConsoleInit(args); | ||
| #endif | ||
|
|
||
| #if DEBUG | ||
| if (args.Length > 0 && !string.IsNullOrEmpty(args[0]) && args[0]!.Equals("-isswait", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
|
|
@@ -120,5 +129,95 @@ public static int Start([MarshalAs(UnmanagedType.LPArray, ArraySubType = Unmanag | |
|
|
||
| return exitCode; | ||
| } | ||
|
|
||
| #if !UNIX | ||
| /// <summary> | ||
| /// Allocates a console early in startup to support consoleAllocationPolicy=detached. | ||
| /// On newer Windows (with the detached policy active), the OS does not auto-allocate | ||
| /// a console for CUI apps. On older Windows the manifest is ignored, so the OS | ||
| /// auto-allocates a console and GetConsoleWindow() returns non-zero (early return). | ||
| /// </summary> | ||
| private static void EarlyConsoleInit(string[] args) | ||
| { | ||
|
jshigetomi marked this conversation as resolved.
|
||
| nint existingConsole = Interop.Windows.GetConsoleWindow(); | ||
| if (existingConsole != nint.Zero) | ||
|
Comment on lines
+142
to
+143
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dustin L. Howett (@DHowett) If the process is started by If so, |
||
| { | ||
| // Console already exists (inherited from parent or auto-allocated on older Windows). | ||
| // Leave -WindowStyle handling to the existing SetConsoleMode code path. | ||
| return; | ||
| } | ||
|
|
||
| // No console exists (GetConsoleWindow() == 0). This means either: | ||
| // (a) The detached manifest policy is active (newer Windows), or | ||
| // (b) DETACHED_PROCESS — no console at all, or | ||
| // (c) CREATE_NO_WINDOW — console session exists but no window. | ||
| // | ||
| // For (c), AllocConsoleWithOptions returns ExistingConsole (no-op). | ||
| // For (a) and (b), behavior depends on the mode: | ||
| // Default mode: allocates if the parent would have given us a console | ||
| // on prior Windows versions, returns NoConsole for DETACHED_PROCESS. | ||
| // NoWindow mode: always creates a console session (overrides DETACHED). | ||
| // | ||
| // When -WindowStyle Hidden is specified, we intentionally use NoWindow | ||
| // even though it overrides DETACHED_PROCESS — the user explicitly asked | ||
| // for invisible PowerShell with working I/O, and the alternative (no | ||
| // console, crashing on stdin/stdout access) is strictly worse. | ||
| // | ||
| // If the API is not available (older Windows), TryAlloc* returns false. | ||
| // On older Windows the manifest is ignored and the OS auto-allocates | ||
| // a console, so GetConsoleWindow() would have returned non-zero above. | ||
| // The only older-Windows path here is DETACHED_PROCESS, where the | ||
| // existing behavior is no console I/O; we preserve that by not | ||
| // falling back to plain AllocConsole() (per DHowett's guidance). | ||
| if (EarlyCheckForHiddenWindowStyle(args)) | ||
| { | ||
| Interop.Windows.TryAllocConsoleNoWindow(); | ||
| } | ||
| else | ||
| { | ||
| Interop.Windows.TryAllocConsoleDefault(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Minimal early scan for -WindowStyle Hidden in command line args. | ||
| /// Matches any unambiguous prefix of "windowstyle" starting from "w" | ||
| /// (e.g. -w, -wi, -win, ..., -windowstyle, --windowstyle) followed by "hidden". | ||
| /// This is a best-effort check that runs before the full parser. False positives | ||
| /// (e.g. a hypothetical future -w parameter) are acceptable because the worst case | ||
| /// is allocating a hidden console that the full parser would later show. | ||
| /// </summary> | ||
| private static bool EarlyCheckForHiddenWindowStyle(string[] args) | ||
| { | ||
| for (int i = 0; i < args.Length - 1; i++) | ||
| { | ||
| string arg = args[i]; | ||
| if (arg.Length >= 2 && (arg[0] == '-' || arg[0] == '/')) | ||
| { | ||
| int start = 1; | ||
|
|
||
| // Strip second dash for --windowstyle (matches full parser behavior). | ||
| if (arg.Length >= 3 && arg[0] == '-' && arg[1] == '-') | ||
| { | ||
| start = 2; | ||
| } | ||
|
|
||
| ReadOnlySpan<char> key = arg.AsSpan(start); | ||
| if (key.Length >= 1 | ||
| && key.Length <= "windowstyle".Length | ||
| && "windowstyle".AsSpan().StartsWith(key, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| if (args[i + 1].Equals("hidden", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| #endif | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static unsafe partial class Windows | ||
| { | ||
| // Set once the API is found to be missing (Windows builds before AllocConsoleWithOptions | ||
| // shipped) so subsequent calls skip the P/Invoke and the resulting exception. | ||
| private static bool s_allocConsoleWithOptionsNotAvailable; | ||
|
|
||
| /// <summary>Console allocation mode for AllocConsoleWithOptions.</summary> | ||
| internal enum AllocConsoleMode : int | ||
| { | ||
| /// <summary>Allocate only if the parent process requested it.</summary> | ||
| Default = 0, | ||
|
|
||
| /// <summary>Force allocation of a console with a visible window.</summary> | ||
| NewWindow = 1, | ||
|
|
||
| /// <summary>Allocate console I/O handles without creating a visible window.</summary> | ||
| NoWindow = 2, | ||
| } | ||
|
|
||
| /// <summary>Result of an AllocConsoleWithOptions call.</summary> | ||
| internal enum AllocConsoleResult : int | ||
| { | ||
| /// <summary>No console was allocated.</summary> | ||
| NoConsole = 0, | ||
|
|
||
| /// <summary>A new console session was created.</summary> | ||
| NewConsole = 1, | ||
|
|
||
| /// <summary>An existing console session was attached.</summary> | ||
| ExistingConsole = 2, | ||
| } | ||
|
|
||
| /// <summary>Options struct passed to AllocConsoleWithOptions.</summary> | ||
| [StructLayout(LayoutKind.Sequential)] | ||
| internal struct AllocConsoleOptions | ||
| { | ||
| /// <summary>The allocation mode (Default, NewWindow, or NoWindow).</summary> | ||
| public AllocConsoleMode Mode; | ||
|
|
||
| /// <summary>If non-zero, the ShowWindow field is used as the initial show state.</summary> | ||
| public int UseShowWindow; | ||
|
|
||
| /// <summary>The initial show state (e.g. SW_HIDE) when UseShowWindow is set.</summary> | ||
| public ushort ShowWindow; | ||
| } | ||
|
|
||
| [LibraryImport("kernel32.dll")] | ||
| internal static partial int AllocConsoleWithOptions( | ||
| ref AllocConsoleOptions allocOptions, | ||
| out AllocConsoleResult result); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to allocate a console without a visible window using AllocConsoleWithOptions. | ||
| /// Returns false if the API is not available (older Windows), the call fails, | ||
| /// or no console was allocated (e.g. process was started with DETACHED_PROCESS). | ||
| /// </summary> | ||
| internal static bool TryAllocConsoleNoWindow() | ||
| { | ||
| return TryAllocConsoleWithMode(AllocConsoleMode.NoWindow); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Attempts to allocate a console using AllocConsoleWithOptions with Default mode. | ||
| /// Default mode respects DETACHED_PROCESS from the parent's CreateProcess call: | ||
| /// it returns NoConsole if the parent intended this process to run without a console, | ||
| /// whereas plain AllocConsole() would override that and force-create a console. | ||
| /// Returns false if the API is not available (older Windows), the call fails, | ||
| /// or no console was allocated. | ||
| /// </summary> | ||
| internal static bool TryAllocConsoleDefault() | ||
| { | ||
| return TryAllocConsoleWithMode(AllocConsoleMode.Default); | ||
| } | ||
|
|
||
| private static bool TryAllocConsoleWithMode(AllocConsoleMode mode) | ||
| { | ||
| if (s_allocConsoleWithOptionsNotAvailable) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var options = new AllocConsoleOptions | ||
| { | ||
| Mode = mode, | ||
| UseShowWindow = 0, | ||
| ShowWindow = 0, | ||
| }; | ||
|
|
||
| int hr = AllocConsoleWithOptions(ref options, out AllocConsoleResult result); | ||
| return hr >= 0 && result != AllocConsoleResult.NoConsole; | ||
| } | ||
| catch (EntryPointNotFoundException) | ||
| { | ||
| s_allocConsoleWithOptionsNotAvailable = true; | ||
| return false; | ||
|
SufficientDaikon marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.