-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
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) | ||
| { | ||
|
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. Can we check on the build number of Windows here to simply do nothing for build prior to 26100? Let PowerShell fall back to the original code path for 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. Version checks are always the wrong choice. APIs can be backported--and this one very nearly was!--to earlier versions. The only correct way to handle this is to check whether it is available either by calling it and having it fail or by looking it up at runtime. |
||
| 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. @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,101 @@ | ||
| // 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 | ||
| { | ||
| /// <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) | ||
| { | ||
| 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) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2489,30 +2489,32 @@ internal static bool AllocateHiddenConsole() | |
| // save the foreground window since allocating a console window might remove focus from it | ||
| IntPtr savedForeground = Interop.Windows.GetForegroundWindow(); | ||
|
|
||
| // Since there is no console window, allocate and then hide it... | ||
| // Suppress the PreFAST warning about not using Marshal.GetLastWin32Error() to | ||
| // get the error code. | ||
| Interop.Windows.AllocConsole(); | ||
| hwnd = Interop.Windows.GetConsoleWindow(); | ||
| // Try AllocConsoleWithOptions with NoWindow mode first to avoid the flash | ||
| // that the AllocConsole() + ShowWindow(SW_HIDE) pattern causes. | ||
| bool allocated = Interop.Windows.TryAllocConsoleNoWindow(); | ||
|
|
||
| bool returnValue; | ||
| if (hwnd == nint.Zero) | ||
| if (!allocated) | ||
| { | ||
| returnValue = false; | ||
| } | ||
| else | ||
| { | ||
| returnValue = true; | ||
| // Fallback for older Windows: allocate and then hide. | ||
| Interop.Windows.AllocConsole(); | ||
| hwnd = Interop.Windows.GetConsoleWindow(); | ||
| if (hwnd == nint.Zero) | ||
|
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. The new code doesn't try restoring the foreground windows before return. Could it be possible that the foreground window focus changes even if |
||
| { | ||
| return false; | ||
| } | ||
|
|
||
| Interop.Windows.ShowWindow(hwnd, Interop.Windows.SW_HIDE); | ||
| AlwaysCaptureApplicationIO = true; | ||
| } | ||
|
|
||
| AlwaysCaptureApplicationIO = true; | ||
|
|
||
| // Restore foreground window if focus changed during console allocation. | ||
| if (savedForeground != nint.Zero && Interop.Windows.GetForegroundWindow() != savedForeground) | ||
| { | ||
| Interop.Windows.SetForegroundWindow(savedForeground); | ||
| } | ||
|
|
||
| return returnValue; | ||
| return true; | ||
|
Comment on lines
+2492
to
+2517
|
||
| #endif | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
xmlns="urn:schemas-microsoft-com:asm.v3"attribute required for the<application>node? We have thexmlns="urn:schemas-microsoft-com:asm.v1"at the<assembly>node above.