{"record":{"id":"08169501d049b05c","repo":"tui-cs/Terminal.Gui","slug":"failed-to-get-output-console-mode-error-code-ge","errorCode":null,"errorMessage":"Failed to get output console mode, error code: {GetLastError()}.","messagePattern":"Failed to get output console mode, error code: (.+?)\\.","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"critical","filePath":"Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs","lineNumber":61,"sourceCode":"        }\n\n        _originalInputConsoleMode = mode;\n\n        if ((mode & ENABLE_VIRTUAL_TERMINAL_INPUT) == 0)\n        {\n            mode |= ENABLE_VIRTUAL_TERMINAL_INPUT;\n\n            if (!SetConsoleMode (_inputHandle, mode))\n            {\n                throw new ApplicationException ($\"Failed to set input console mode, error code: {GetLastError ()}.\");\n            }\n        }\n\n        _outputHandle = GetStdHandle (STD_OUTPUT_HANDLE);\n\n        if (!GetConsoleMode (_outputHandle, out mode))\n        {\n            throw new ApplicationException ($\"Failed to get output console mode, error code: {GetLastError ()}.\");\n        }\n\n        _originalOutputConsoleMode = mode;\n\n        if ((mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0)\n        {\n            mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;\n\n            if (!SetConsoleMode (_outputHandle, mode))\n            {\n                throw new ApplicationException ($\"Failed to set output console mode, error code: {GetLastError ()}.\");\n            }\n        }\n    }\n\n    public void Cleanup ()\n    {\n        if (!FlushConsoleInputBuffer (_inputHandle))","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs#L43-L79","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before -- redirected stdout breaks GetConsoleMode\nmyapp | jq\n\n// after -- guard against redirection\nif (Console.IsOutputRedirected) { Console.Error.WriteLine(\"Run in a real terminal.\"); return; }\nusing IApplication app = Application.Create().Init();","handlingStrategy":"validation","validationCode":"if (OperatingSystem.IsWindows() && Console.IsOutputRedirected) { Console.Error.WriteLine(\"stdout is redirected; cannot query console mode.\"); return; }","typeGuard":null,"tryCatchPattern":"try { using IApplication app = Application.Create().Init(); app.Run<MyWindow>(); }\ncatch (ApplicationException ex) when (ex.Message.Contains(\"output console mode\") && ex.Message.Contains(\"Failed to get\"))\n{ Console.Error.WriteLine($\"Console output handle invalid. {ex.Message}\"); }","preventionTips":["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."],"tags":["windows","console","driver","win32","output","initialization"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}