{"record":{"id":"d3542c68ac63268a","repo":"felixse/FluentTerminal","slug":"could-not-get-console-mode","errorCode":null,"errorMessage":"Could not get console mode.","messagePattern":"Could not get console mode\\.","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"critical","filePath":"FluentTerminal.SystemTray/Services/ConPty/Terminal.cs","lineNumber":72,"sourceCode":"\n            var windowHandle = GetConsoleWindow();\n            ShowWindow(windowHandle, SW_HIDE);\n\n            // And enable VT processing for our process's console.\n            EnableVirtualTerminalSequenceProcessing();\n        }\n\n        ~Terminal()\n        {\n            Dispose(false);\n        }\n\n        private void EnableVirtualTerminalSequenceProcessing()\n        {\n            SafeFileHandle screenBuffer = GetConsoleScreenBuffer();\n            if (!GetConsoleMode(screenBuffer, out uint outConsoleMode))\n            {\n                throw new Win32Exception(Marshal.GetLastWin32Error(), $\"Could not get console mode.\");\n            }\n            outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;\n\n            if (!SetConsoleMode(screenBuffer, outConsoleMode))\n            {\n                throw new Win32Exception(Marshal.GetLastWin32Error(), $\"Could not enable virtual terminal processing.\");\n            }\n        }\n\n        /// <summary>\n        /// Start the psuedoconsole and run the process as shown in \n        /// https://docs.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session#creating-the-pseudoconsole\n        /// </summary>\n        /// <param name=\"command\">the command to run, e.g. cmd.exe</param>\n        /// <param name=\"consoleHeight\">The height (in characters) to start the pseudoconsole with. Defaults to 80.</param>\n        /// <param name=\"consoleWidth\">The width (in characters) to start the pseudoconsole with. Defaults to 30.</param>\n        public void Start(string command, string directory, string environment, int consoleWidth = 80, int consoleHeight = 30)\n        {","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/felixse/FluentTerminal/blob/ba83ec485eea8d8ee65825711731300bee2e76ed/FluentTerminal.SystemTray/Services/ConPty/Terminal.cs#L54-L90","documentation":"Thrown by Terminal.EnableVirtualTerminalSequenceProcessing when GetConsoleMode() returns false for the console screen-buffer handle. Before enabling VT processing the code queries the current console mode flags; if that query fails the Win32 error is wrapped and thrown, aborting terminal startup.","triggerScenarios":"Calling EnableVirtualTerminalSequenceProcessing (from the Terminal ctor) when the screen-buffer handle obtained from GetConsoleScreenBuffer is invalid or already closed, so GetConsoleMode cannot read the mode.","commonSituations":"Console screen buffer was closed/invalidated between allocation and mode query; running in an environment where the CONOUT$ handle is not backed by a real console (remote/service sessions); handle became stale.","solutions":["Inspect Win32Exception.NativeErrorCode (e.g. ERROR_INVALID_HANDLE) to confirm a stale/invalid handle.","Ensure GetConsoleScreenBuffer succeeded and the handle is fresh before querying mode.","Run the tray process in an interactive desktop session with a real console.","Fall back to the WinPty backend if ConPty cannot initialize on this Windows build."],"exampleFix":"// before\nif (!GetConsoleMode(screenBuffer, out uint outConsoleMode))\n    throw new Win32Exception(Marshal.GetLastWin32Error(), $\"Could not get console mode.\");\n\n// after\nint err = Marshal.GetLastWin32Error();\nthrow new Win32Exception(err, $\"Could not get console mode (Win32 error {err}).\");","handlingStrategy":"try-catch","validationCode":"// Verify the screen-buffer handle is valid before querying mode.\nvar sb = GetConsoleScreenBuffer();\nif (sb.IsInvalid || sb.IsClosed) throw new InvalidOperationException(\"console screen buffer handle invalid\");","typeGuard":null,"tryCatchPattern":"try { terminal = new Terminal(); }\ncatch (System.ComponentModel.Win32Exception ex) when (ex.Message.Contains(\"console mode\")) { logger.Error(ex, $\"GetConsoleMode failed (Win32 {ex.NativeErrorCode})\"); fallbackToWinPty(); }","preventionTips":["Ensure the console is freshly allocated before querying mode.","Run in an interactive session with a real console.","Check NativeErrorCode (ERROR_INVALID_HANDLE) to confirm a stale handle.","Use the WinPty backend as a fallback."],"tags":["conpty","win32","console","native","terminal-backend"],"backgroundTag":null,"analyzedSha":"ba83ec485eea8d8ee65825711731300bee2e76ed","analyzedAt":"2026-08-13T21:23:15.467Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}