Exemple #1
0
        /// <summary>
        /// Opens the screen buffer.
        /// </summary>
        /// <returns>Returns a new <see cref="ConsoleInputBuffer"/> instance that references the
        /// console's input buffer.</returns>
        /// <remarks>This method allocates a new ConsoleInputBuffer instance.  Callers should
        /// call Dispose on the returned instance when they're done with it.</remarks>
        static public ConsoleInputBuffer GetInputBuffer()
        {
            IntPtr inHandle = WinApi.CreateFile("CONIN$",
                                                WinApi.GENERIC_READ | WinApi.GENERIC_WRITE, WinApi.FILE_SHARE_READ | WinApi.FILE_SHARE_WRITE,
                                                null,
                                                WinApi.OPEN_EXISTING,
                                                0,
                                                IntPtr.Zero);

            if (inHandle.ToInt32() == WinApi.INVALID_HANDLE_VALUE)
            {
                throw new IOException("Unable to open CONIN$", Marshal.GetLastWin32Error());
            }
            ConsoleInputBuffer inputBuffer = new ConsoleInputBuffer(inHandle);

            inputBuffer.ownsHandle = true;

            return(inputBuffer);
        }
Exemple #2
0
        /// <summary>
        /// Opens the currently active screen buffer.
        /// </summary>
        /// <returns>A new <see cref="ConsoleScreenBuffer" /> instance that references the currently active
        /// console screen buffer.</returns>
        /// <remarks>This method allocates a new ConsoleScreenBuffer instance.  Callers should
        /// call Dispose on the returned instance when they're done with it.</remarks>
        public static ConsoleScreenBuffer GetActiveScreenBuffer()
        {
            // CONOUT$ always references the current active screen buffer.
            // NOTE:  *MUST* specify GENERIC_READ | GENERIC_WRITE.  Otherwise
            // the console API calls will fail with Win32 error INVALID_HANDLE_VALUE.
            // Also must include the file sharing flags or CreateFile will fail.
            IntPtr outHandle = WinApi.CreateFile("CONOUT$",
                                                 WinApi.GENERIC_READ | WinApi.GENERIC_WRITE,
                                                 WinApi.FILE_SHARE_READ | WinApi.FILE_SHARE_WRITE,
                                                 null,
                                                 WinApi.OPEN_EXISTING,
                                                 0,
                                                 IntPtr.Zero);

            if (outHandle.ToInt32() == WinApi.INVALID_HANDLE_VALUE)
            {
                throw new IOException("Unable to open CONOUT$", Marshal.GetLastWin32Error());
            }

            return(new ConsoleScreenBuffer(outHandle)
            {
                ownsHandle = true
            });
        }