tui-cs/Terminal.Gui · critical · ApplicationException

Failed to get input console mode, error code: {GetLastError(

Error message

Failed to get input console mode, error code: {GetLastError()}.

What it means

This error is thrown by the NetWinVTConsole constructor when the Windows API GetConsoleMode fails on the standard input handle. NetWinVTConsole is responsible for enabling Virtual Terminal Input mode on Windows. The error indicates the console input handle could not be queried for its current mode, meaning VT input processing cannot be safely configured or restored later.

Source

Thrown at Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs:42

    // Standard handles.
    private const int STD_ERROR_HANDLE = -12;
    private const int STD_INPUT_HANDLE = -10;
    private const int STD_OUTPUT_HANDLE = -11;

    // Handles and original console modes.
    private readonly nint _inputHandle;
    private readonly uint _originalInputConsoleMode;
    private readonly uint _originalOutputConsoleMode;
    private readonly nint _outputHandle;

    public NetWinVTConsole ()
    {
        _inputHandle = GetStdHandle (STD_INPUT_HANDLE);

        if (!GetConsoleMode (_inputHandle, out uint mode))
        {
            throw new ApplicationException ($"Failed to get input console mode, error code: {GetLastError ()}.");
        }

        _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))
        {

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Ensure stdin is connected to a real console -- do not pipe input into the application.
  2. If running in CI or a headless container, allocate a pseudo-terminal (e.g., winpty, or use --tty in Docker).
  3. If input redirection is intentional for testing, use the test harness (InputInjector / VirtualTimeProvider) rather than a live driver.
  4. Defer NetWinVTConsole construction until a console is confirmed attached, or fall back to a non-VT input driver.

Example fix

// before -- piped input causes the handle to be invalid
echo "hello" | myapp

// after -- run interactively, or detect the missing console
if (Console.IsInputRedirected) { /* use test harness or error out */ }
using IApplication app = Application.Create().Init();
Defensive patterns

Strategy: validation

Validate before calling

if (OperatingSystem.IsWindows() && Console.IsInputRedirected) { Console.Error.WriteLine("stdin is redirected; cannot initialize VT console."); return; }

Try / catch

try { using IApplication app = Application.Create().Init(); app.Run<MyWindow>(); }
catch (ApplicationException ex) when (ex.Message.Contains("input console mode"))
{ Console.Error.WriteLine($"Console input setup failed. Connect stdin to a real console. {ex.Message}"); }

Prevention

When it happens

Trigger: Thrown at NetWinVTConsole.cs:42 during construction when GetConsoleMode(_inputHandle, out mode) returns false. The input handle is obtained from GetStdHandle(STD_INPUT_HANDLE). The throw fires if that handle is invalid, not a console handle, or the call otherwise fails.

Common situations: Running with stdin redirected from a file or pipe (e.g., 'echo foo | myapp'), running in an environment without a console (e.g., certain CI/CD systems, Docker containers without TTY, or Windows Service contexts), or when stdin handle has been closed before driver initialization.

Related errors


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/af36888c86b14901. Report an issue: GitHub.