tui-cs/Terminal.Gui · critical · ApplicationException
Failed to get output console mode, error code: {GetLastError
Error message
Failed to get output console mode, error code: {GetLastError()}. What it means
This error is thrown by the NetWinVTConsole constructor when GetConsoleMode fails on the standard output handle. After successfully configuring input mode, the constructor queries the output handle for its current mode to (a) store the original mode for later restoration and (b) check if ENABLE_VIRTUAL_TERMINAL_PROCESSING needs to be added. If the query fails, the library cannot proceed safely.
Source
Thrown at Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs:61
}
_originalInputConsoleMode = mode;
if ((mode & ENABLE_VIRTUAL_TERMINAL_INPUT) == 0)
{
mode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
if (!SetConsoleMode (_inputHandle, mode))
{
throw new ApplicationException ($"Failed to set input console mode, error code: {GetLastError ()}.");
}
}
_outputHandle = GetStdHandle (STD_OUTPUT_HANDLE);
if (!GetConsoleMode (_outputHandle, out mode))
{
throw new ApplicationException ($"Failed to get output console mode, error code: {GetLastError ()}.");
}
_originalOutputConsoleMode = mode;
if ((mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0)
{
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode (_outputHandle, mode))
{
throw new ApplicationException ($"Failed to set output console mode, error code: {GetLastError ()}.");
}
}
}
public void Cleanup ()
{
if (!FlushConsoleInputBuffer (_inputHandle))View on GitHub (pinned to 2e47b11478)
Solutions
- Ensure stdout is connected to a real console window -- avoid piping or redirecting output during a live UI session.
- If running in CI/headless, allocate a PTY or use the test harness instead of a live driver.
- If redirection is required, use the WindowsVTOutputHelper.TryEnable path (which gracefully returns false) by selecting a driver that handles redirection, or check Console.IsOutputRedirected before initializing.
- Defer NetWinVTConsole creation until a console is confirmed.
Example fix
// before -- redirected stdout breaks GetConsoleMode
myapp | jq
// after -- guard against redirection
if (Console.IsOutputRedirected) { Console.Error.WriteLine("Run in a real terminal."); return; }
using IApplication app = Application.Create().Init(); Defensive patterns
Strategy: validation
Validate before calling
if (OperatingSystem.IsWindows() && Console.IsOutputRedirected) { Console.Error.WriteLine("stdout is redirected; cannot query console mode."); return; } Try / catch
try { using IApplication app = Application.Create().Init(); app.Run<MyWindow>(); }
catch (ApplicationException ex) when (ex.Message.Contains("output console mode") && ex.Message.Contains("Failed to get"))
{ Console.Error.WriteLine($"Console output handle invalid. {ex.Message}"); } Prevention
- Do not redirect stdout (e.g., 'myapp > file.txt') when running a live TUI.
- Ensure stdout is a real console in CI/headless (use a PTY).
- Check Console.IsOutputRedirected before initializing the driver.
- Avoid closing the console window during initialization.
When it happens
Trigger: Thrown at NetWinVTConsole.cs:61 when GetConsoleMode(_outputHandle, out mode) returns false. The output handle comes from GetStdHandle(STD_OUTPUT_HANDLE). Failure means the handle is not a valid console output handle.
Common situations: Running with stdout redirected to a file, pipe, or another process (e.g., 'myapp | jq', 'myapp > log.txt'), running in a container or service without a console attached, or the output handle being closed before driver init.
Related errors
- Failed to set output console mode, error code: {GetLastError
- Failed to set screenBuffer console mode, error code: {Marsha
- Failed to get input console mode, error code: {GetLastError(
- Failed to set input console mode, error code: {GetLastError(
- Failed to set output console mode, error code: {GetLastError
AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13).
Data as JSON: /api/errors/08169501d049b05c.
Report an issue: GitHub.