{"record":{"id":"91af5b5085c2b225","repo":"tui-cs/Terminal.Gui","slug":"failed-to-set-screenbuffer-console-mode-error-cod","errorCode":null,"errorMessage":"Failed to set screenBuffer console mode, error code: {Marshal.GetLastWin32Error()}.","messagePattern":"Failed to set screenBuffer console mode, error code: (.+?)\\.","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"critical","filePath":"Terminal.Gui/Drivers/WindowsDriver/WindowsOutput.cs","lineNumber":131,"sourceCode":"\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.\n                Console.Out.Write (EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);\n            }\n            else\n            {\n                _foreground = Console.ForegroundColor;\n                _background = Console.BackgroundColor;\n            }\n        }\n\n        GetSize ();\n    }","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Drivers/WindowsDriver/WindowsOutput.cs#L113-L149","documentation":"This error is thrown by WindowsOutput when the Windows console API SetConsoleMode fails on the newly-created screen buffer during driver initialization on a legacy console. The library creates its own screen buffer to bypass legacy console limitations and needs to disable line-wrap on it. If SetConsoleMode returns false, the Win32 error code is captured via Marshal.GetLastWin32Error and reported.","triggerScenarios":"Occurs exclusively in the WindowsOutput constructor (Terminal.Gui/Drivers/WindowsDriver/WindowsOutput.cs:131) when IsLegacyConsole is true (i.e., ENABLE_VIRTUAL_TERMINAL_PROCESSING is not set on the original console). After CreateScreenBuffer() and a successful GetConsoleMode, SetConsoleMode is called with the ENABLE_WRAP_AT_EOL_OUTPUT bit cleared. The throw fires only if that P/Invoke returns false.","commonSituations":"Running under a non-standard Windows console host (e.g., conhost with restricted mode), running inside an RDP/session where screen-buffer creation partially fails, antivirus or endpoint-protection intercepting console handles, or running on Windows Server Core where VT processing is unavailable and screen buffer setup is fragile.","solutions":["Verify the application is running in a real console window (not redirected to a pipe or file) and that ENABLE_VIRTUAL_TERMINAL_PROCESSING is available; on Windows 10 1709+ the legacy path is rarely needed.","Check for stdout redirection -- if stdout is redirected (e.g., 'myapp > out.txt'), the screen buffer handle may be invalid; run in a true terminal.","Update to a newer Windows version or enable the legacy console host fallback that supports CreateConsoleScreenBuffer + SetConsoleMode.","If unavoidable, switch to the AnsiDriver via DriverRegistry configuration so the legacy WindowsOutput codepath is not taken."],"exampleFix":"// before -- running with redirected stdout triggers legacy path failure\napp.Run<MyWindow>();\n\n// after -- ensure stdout is a real console; or select the ANSI driver explicitly\nApplication.ForceDriver = \"Ansi\";\nusing IApplication app = Application.Create().Init();\napp.Run<MyWindow>();","handlingStrategy":"validation","validationCode":"if (!OperatingSystem.IsWindows() || Console.IsOutputRedirected) { /* select AnsiDriver or skip live driver */ }\nif (!IsAttachedToTerminal) { /* use headless/test mode */ }","typeGuard":null,"tryCatchPattern":"try { using IApplication app = Application.Create().Init(); app.Run<MyWindow>(); }\ncatch (ApplicationException ex) when (ex.Message.Contains(\"screenBuffer console mode\"))\n{ Console.Error.WriteLine($\"Console mode setup failed (Win32). Use a real terminal. {ex.Message}\"); }","preventionTips":["Run the app in a real console window, not with redirected stdout.","On Windows, ensure VT processing is available (Windows 10 1709+) so the legacy screen-buffer codepath is avoided.","Use the AnsiDriver via Application.ForceDriver if the legacy WindowsOutput path is problematic.","Check Console.IsOutputRedirected before initializing the driver."],"tags":["windows","console","driver","win32","initialization"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}