Skip to content

Commit 1a86fe6

Browse files
Use AllocConsoleWithOptions(Default) for normal launches
Per DHowett's feedback: plain AllocConsole() overrides DETACHED_PROCESS from the parent's CreateProcess call. AllocConsoleWithOptions with Default mode respects it. Extract shared TryAllocConsoleWithMode() and add TryAllocConsoleDefault() alongside TryAllocConsoleNoWindow(). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4b4a8ef commit 1a86fe6

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ private static void EarlyConsoleInit(string[] args)
173173
else
174174
{
175175
// Normal interactive launch: allocate a visible console.
176-
Interop.Windows.AllocConsole();
176+
// Use AllocConsoleWithOptions(Default) when available — it respects
177+
// DETACHED_PROCESS from the parent's CreateProcess call, whereas
178+
// plain AllocConsole() would override it and force-create a console.
179+
if (!Interop.Windows.TryAllocConsoleDefault())
180+
{
181+
Interop.Windows.AllocConsole();
182+
}
177183
}
178184
}
179185

src/System.Management.Automation/engine/Interop/Windows/AllocConsoleWithOptions.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,28 @@ internal static partial int AllocConsoleWithOptions(
6060
/// Returns false if the API is not available (older Windows) or the call fails.
6161
/// </summary>
6262
internal static bool TryAllocConsoleNoWindow()
63+
{
64+
return TryAllocConsoleWithMode(AllocConsoleMode.NoWindow);
65+
}
66+
67+
/// <summary>
68+
/// Attempts to allocate a console using AllocConsoleWithOptions with Default mode.
69+
/// Default mode respects DETACHED_PROCESS from the parent's CreateProcess call,
70+
/// whereas plain AllocConsole() would override it and force-create a console.
71+
/// Returns false if the API is not available (older Windows) or the call fails.
72+
/// </summary>
73+
internal static bool TryAllocConsoleDefault()
74+
{
75+
return TryAllocConsoleWithMode(AllocConsoleMode.Default);
76+
}
77+
78+
private static bool TryAllocConsoleWithMode(AllocConsoleMode mode)
6379
{
6480
try
6581
{
6682
var options = new AllocConsoleOptions
6783
{
68-
Mode = AllocConsoleMode.NoWindow,
84+
Mode = mode,
6985
UseShowWindow = 0,
7086
ShowWindow = 0,
7187
};

0 commit comments

Comments
 (0)