tui-cs/Terminal.Gui · critical · ApplicationException

Failed to get screenBuffer console mode, error code: {Marsha

Error message

Failed to get screenBuffer console mode, error code: {Marshal.GetLastWin32Error()}.

What it means

Thrown during WindowsOutput initialization on a legacy (non-VT) console after CreateScreenBuffer succeeds but GetConsoleMode on the new screen buffer fails. It is an ApplicationException carrying the Win32 error code from Marshal.GetLastWin32Error(). This blocks driver startup, so the Terminal.Gui application cannot initialize its Windows console output.

Source

Thrown at Terminal.Gui/Drivers/WindowsDriver/WindowsOutput.cs:120

        }

        if (!OperatingSystem.IsWindows ())
        {
            return;
        }

        // Get the standard output handle which is the current screen buffer.
        _outputHandle = GetStdHandle (STD_OUTPUT_HANDLE);
        GetConsoleMode (_outputHandle, out uint mode);
        IsLegacyConsole = (mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0;

        if (IsLegacyConsole)
        {
            CreateScreenBuffer ();

            if (!GetConsoleMode (_screenBuffer, out mode))
            {
                throw new ApplicationException ($"Failed to get screenBuffer console mode, error code: {Marshal.GetLastWin32Error ()}.");
            }

#pragma warning disable IDE1006 // Naming Styles
            const uint ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002;
#pragma warning restore IDE1006 // Naming Styles

            mode &= ~ENABLE_WRAP_AT_EOL_OUTPUT; // Disable wrap

            if (!SetConsoleMode (_screenBuffer, mode))
            {
                throw new ApplicationException ($"Failed to set screenBuffer console mode, error code: {Marshal.GetLastWin32Error ()}.");
            }
        }
        else
        {
            if (Environment.GetEnvironmentVariable ("VSAPPIDNAME") is null)
            {
                //Enable alternative screen buffer.

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Run the app in a real interactive console (not with stdout redirected).
  2. Force a modern console: set the Ansi (VT) driver instead of the legacy Windows driver.
  3. Ensure a console is allocated (do not redirect stdout/stderr) and run on Windows 10+ where VT processing is available.

Example fix

// before: legacy console auto-selected, init fails
Application.Create().Init();

// after: request the Ansi/VT driver to avoid legacy screen-buffer path
Application.UseDriver = DriverRegistry.AniDriver; // or force via config
Application.Create().Init();
Defensive patterns

Strategy: fallback

Validate before calling

// request the VT/Ansi driver before Init to bypass the legacy screen-buffer path
Application.UseDriver = "AnsiDriver";

Try / catch

try { app.Init(); } catch (ApplicationException ex) { /* fall back to Ansi driver or report Win32 code */ }

Prevention

When it happens

Trigger: Initializing the Windows driver in an environment where the allocated console screen buffer cannot be queried for its mode: redirected stdout, a pseudo-console with restricted capabilities, headless/CI without a real console, or insufficient console permissions.

Common situations: Running under CI/build agents with redirected stdout, containers without an allocated console, RDP/SSH sessions with unusual console drivers, or older Windows builds with limited console APIs.

Related errors


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