{"record":{"id":"122affb81397ae7a","repo":"tui-cs/Terminal.Gui","slug":"failed-to-get-screenbuffer-console-mode-error-cod","errorCode":null,"errorMessage":"Failed to get screenBuffer console mode, error code: {Marshal.GetLastWin32Error()}.","messagePattern":"Failed to get screenBuffer console mode, error code: (.+?)\\.","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"critical","filePath":"Terminal.Gui/Drivers/WindowsDriver/WindowsOutput.cs","lineNumber":120,"sourceCode":"        }\n\n        if (!OperatingSystem.IsWindows ())\n        {\n            return;\n        }\n\n        // Get the standard output handle which is the current screen buffer.\n        _outputHandle = GetStdHandle (STD_OUTPUT_HANDLE);\n        GetConsoleMode (_outputHandle, out uint mode);\n        IsLegacyConsole = (mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0;\n\n        if (IsLegacyConsole)\n        {\n            CreateScreenBuffer ();\n\n            if (!GetConsoleMode (_screenBuffer, out mode))\n            {\n                throw new ApplicationException ($\"Failed to get screenBuffer console mode, error code: {Marshal.GetLastWin32Error ()}.\");\n            }\n\n#pragma warning disable IDE1006 // Naming Styles\n            const uint ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002;\n#pragma warning restore IDE1006 // Naming Styles\n\n            mode &= ~ENABLE_WRAP_AT_EOL_OUTPUT; // Disable wrap\n\n            if (!SetConsoleMode (_screenBuffer, mode))\n            {\n                throw new ApplicationException ($\"Failed to set screenBuffer console mode, error code: {Marshal.GetLastWin32Error ()}.\");\n            }\n        }\n        else\n        {\n            if (Environment.GetEnvironmentVariable (\"VSAPPIDNAME\") is null)\n            {\n                //Enable alternative screen buffer.","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Drivers/WindowsDriver/WindowsOutput.cs#L102-L138","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run the app in a real interactive console (not with stdout redirected).","Force a modern console: set the Ansi (VT) driver instead of the legacy Windows driver.","Ensure a console is allocated (do not redirect stdout/stderr) and run on Windows 10+ where VT processing is available."],"exampleFix":"// before: legacy console auto-selected, init fails\nApplication.Create().Init();\n\n// after: request the Ansi/VT driver to avoid legacy screen-buffer path\nApplication.UseDriver = DriverRegistry.AniDriver; // or force via config\nApplication.Create().Init();","handlingStrategy":"fallback","validationCode":"// request the VT/Ansi driver before Init to bypass the legacy screen-buffer path\nApplication.UseDriver = \"AnsiDriver\";","typeGuard":null,"tryCatchPattern":"try { app.Init(); } catch (ApplicationException ex) { /* fall back to Ansi driver or report Win32 code */ }","preventionTips":["Run in a real interactive console; do not redirect stdout.","Prefer the Ansi (VT) driver over the legacy Windows driver.","Ensure the process has an allocated console on Windows 10+."],"tags":["windows","driver","console","win32","init","environment"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}