Skip to content

Commit

Permalink
Cannot use GetConsoleWindow to test if a console is attached
Browse files Browse the repository at this point in the history
GetConsoleWindow returns NULL for an invisible console window
(e.g. created with CREATE_NO_WINDOW).
  • Loading branch information
asmichi committed Jun 29, 2024
1 parent 862ef76 commit 158017b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/ChildProcess/Interop/Windows/Kernel32.Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Asmichi.Interop.Windows
{
internal static partial class Kernel32
{
[DllImport(DllName)]
public static extern IntPtr GetConsoleWindow();
[DllImport(DllName, SetLastError = true)]
public static extern int GetConsoleCP();

[DllImport(DllName, SetLastError = true)]
public static extern bool GetConsoleMode([In] SafeHandle hConsoleHandle, [Out] out int lpMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ private static bool IsConsoleHandle(SafeFileHandle handle)
return Kernel32.GetConsoleMode(handle, out var _);
}

public bool HasConsoleWindow() => Kernel32.GetConsoleWindow() != IntPtr.Zero;
public bool HasConsoleWindow() => Kernel32.GetConsoleCP() != 0;
}
}

0 comments on commit 158017b

Please sign in to comment.