Ignore failure attempting to set console window title (#16948)

This commit is contained in:
Steve Lee
2022-03-30 09:49:40 +05:00
committed by GitHub
parent 93a0f9b8dc
commit 788ffde65c
@@ -2494,6 +2494,8 @@ namespace Microsoft.PowerShell
return consoleTitle.ToString();
}
private static bool s_dontsetConsoleWindowTitle;
/// <summary>
/// Wraps Win32 SetConsoleTitle.
/// </summary>
@@ -2505,12 +2507,27 @@ namespace Microsoft.PowerShell
/// </exception>
internal static void SetConsoleWindowTitle(string consoleTitle)
{
if (s_dontsetConsoleWindowTitle)
{
return;
}
bool result = NativeMethods.SetConsoleTitle(consoleTitle);
if (!result)
{
int err = Marshal.GetLastWin32Error();
// ERROR_GEN_FAILURE is returned if this api can't be used with the terminal
if (err == 0x1f)
{
tracer.WriteLine("Call to SetConsoleTitle failed: {0}", err);
s_dontsetConsoleWindowTitle = true;
// We ignore this specific error as the console can still continue to operate
return;
}
HostException e = CreateHostException(err, "SetConsoleWindowTitle",
ErrorCategory.ResourceUnavailable, ConsoleControlStrings.SetConsoleWindowTitleExceptionTemplate);
throw e;