{"record":{"id":"8d5581bc1a633eb9","repo":"felixse/FluentTerminal","slug":"could-not-get-console-screen-buffer","errorCode":null,"errorMessage":"Could not get console screen buffer.","messagePattern":"Could not get console screen buffer\\.","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"critical","filePath":"FluentTerminal.SystemTray/Services/ConPty/Terminal.cs","lineNumber":153,"sourceCode":"        /// A helper method that opens a handle on the console's screen buffer, which will allow us to get its output,\n        /// even if STDOUT has been redirected (which Visual Studio does by default).\n        /// </summary>\n        /// <returns>A file handle to the console's screen buffer.</returns>\n        /// <remarks>This is described in more detail here: https://docs.microsoft.com/en-us/windows/console/console-handles </remarks>\n        private SafeFileHandle GetConsoleScreenBuffer()\n        {\n            IntPtr file = CreateFileW(\n                ConsoleOutPseudoFilename,\n                GENERIC_WRITE | GENERIC_READ,\n                FILE_SHARE_WRITE,\n                IntPtr.Zero,\n                OPEN_EXISTING,\n                FILE_ATTRIBUTE_NORMAL,\n                IntPtr.Zero);\n\n            if (file == new IntPtr(-1))\n            {\n                throw new Win32Exception(Marshal.GetLastWin32Error(), \"Could not get console screen buffer.\");\n            }\n\n            return new SafeFileHandle(file, true);\n        }\n\n        #region IDisposable Support\n\n        public void Dispose()\n        {\n            Dispose(true);\n            GC.SuppressFinalize(true);\n        }\n\n        private bool alreadyDisposed = false;\n\n        public void Dispose(bool disposeManaged)\n        {\n            if (alreadyDisposed)","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/felixse/FluentTerminal/blob/ba83ec485eea8d8ee65825711731300bee2e76ed/FluentTerminal.SystemTray/Services/ConPty/Terminal.cs#L135-L171","documentation":"Thrown by Terminal.GetConsoleScreenBuffer when CreateFileW on the console output pseudo-filename (CONOUT$) returns INVALID_HANDLE_VALUE (-1). This handle is needed to query/set console mode for VT processing even when STDOUT is redirected (e.g. by Visual Studio). A -1 return means the console screen buffer could not be opened.","triggerScenarios":"Constructing a Terminal when there is no real console attached (so CONOUT$ does not resolve), or when GENERIC_WRITE|GENERIC_READ access is denied on the console. The check `file == new IntPtr(-1)` trips.","commonSituations":"AllocConsole failed or was skipped yet the code reaches here; running under a debugger/host that redirected all console handles; service/session without an interactive desktop console.","solutions":["Ensure AllocConsole succeeded (error 13) before GetConsoleScreenBuffer is reached - the ctor orders these correctly, so an allocation failure is the usual upstream cause.","Inspect Win32Exception.NativeErrorCode (ERROR_INVALID_HANDLE / access denied).","Run the tray process in an interactive session with a genuine console.","Fall back to WinPty if ConPty cannot obtain the screen buffer."],"exampleFix":"// before\nif (file == new IntPtr(-1))\n    throw new Win32Exception(Marshal.GetLastWin32Error(), \"Could not get console screen buffer.\");\n\n// after\nint err = Marshal.GetLastWin32Error();\nthrow new Win32Exception(err, $\"Could not get console screen buffer (Win32 error {err}). Ensure a console is allocated.\");","handlingStrategy":"try-catch","validationCode":"// Ensure a console exists before opening CONOUT$.\nif (GetConsoleWindow() == IntPtr.Zero) AllocConsole();\nvar h = CreateFileW(ConsoleOutPseudoFilename, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);\nif (h == new IntPtr(-1)) throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());","typeGuard":null,"tryCatchPattern":"try { terminal = new Terminal(); }\ncatch (System.ComponentModel.Win32Exception ex) when (ex.Message.Contains(\"screen buffer\")) { logger.Error(ex, $\"CreateFileW(CONOUT$) failed (Win32 {ex.NativeErrorCode})\"); fallbackToWinPty(); }","preventionTips":["Ensure AllocConsole succeeded before opening the screen buffer.","Run in an interactive session with a real console.","Inspect NativeErrorCode (ERROR_INVALID_HANDLE / access denied).","Fall back to WinPty if ConPty cannot obtain the buffer."],"tags":["conpty","win32","console","createfile","native","terminal-backend"],"backgroundTag":null,"analyzedSha":"ba83ec485eea8d8ee65825711731300bee2e76ed","analyzedAt":"2026-08-13T21:23:15.467Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}