{"record":{"id":"af36888c86b14901","repo":"tui-cs/Terminal.Gui","slug":"failed-to-get-input-console-mode-error-code-get","errorCode":null,"errorMessage":"Failed to get input console mode, error code: {GetLastError()}.","messagePattern":"Failed to get input console mode, error code: (.+?)\\.","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"critical","filePath":"Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs","lineNumber":42,"sourceCode":"\n    // Standard handles.\n    private const int STD_ERROR_HANDLE = -12;\n    private const int STD_INPUT_HANDLE = -10;\n    private const int STD_OUTPUT_HANDLE = -11;\n\n    // Handles and original console modes.\n    private readonly nint _inputHandle;\n    private readonly uint _originalInputConsoleMode;\n    private readonly uint _originalOutputConsoleMode;\n    private readonly nint _outputHandle;\n\n    public NetWinVTConsole ()\n    {\n        _inputHandle = GetStdHandle (STD_INPUT_HANDLE);\n\n        if (!GetConsoleMode (_inputHandle, out uint mode))\n        {\n            throw new ApplicationException ($\"Failed to get input console mode, error code: {GetLastError ()}.\");\n        }\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        {","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs#L24-L60","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure stdin is connected to a real console -- do not pipe input into the application.","If running in CI or a headless container, allocate a pseudo-terminal (e.g., winpty, or use --tty in Docker).","If input redirection is intentional for testing, use the test harness (InputInjector / VirtualTimeProvider) rather than a live driver.","Defer NetWinVTConsole construction until a console is confirmed attached, or fall back to a non-VT input driver."],"exampleFix":"// before -- piped input causes the handle to be invalid\necho \"hello\" | myapp\n\n// after -- run interactively, or detect the missing console\nif (Console.IsInputRedirected) { /* use test harness or error out */ }\nusing IApplication app = Application.Create().Init();","handlingStrategy":"validation","validationCode":"if (OperatingSystem.IsWindows() && Console.IsInputRedirected) { Console.Error.WriteLine(\"stdin is redirected; cannot initialize VT console.\"); return; }","typeGuard":null,"tryCatchPattern":"try { using IApplication app = Application.Create().Init(); app.Run<MyWindow>(); }\ncatch (ApplicationException ex) when (ex.Message.Contains(\"input console mode\"))\n{ Console.Error.WriteLine($\"Console input setup failed. Connect stdin to a real console. {ex.Message}\"); }","preventionTips":["Do not pipe input into the application (e.g., 'echo | myapp').","Ensure stdin is connected to a real terminal in CI/headless environments (use a PTY).","For testing, use InputInjector instead of a live driver.","Check Console.IsInputRedirected before initialization."],"tags":["windows","console","driver","win32","input","initialization"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}