Ejemplo n.º 1
0
        public static void WriteConsole(string data)
        {
            using (SafeHandle hConsoleInput = ConsoleInterface.getConsoleInputHandle())
            {
                // we need 2x the number of characters because we need to simulate key press and key release events for each character
                NativeInterface.INPUT_RECORD[] inputRecords = new NativeInterface.INPUT_RECORD[data.Length * 2];
                for (int i = 0; i < data.Length; i++)
                {
                    NativeInterface.KEY_EVENT_RECORD keyPressEvent = default(NativeInterface.KEY_EVENT_RECORD);
                    keyPressEvent.bKeyDown     = true;
                    keyPressEvent.wRepeatCount = 1;
                    keyPressEvent.UnicodeChar  = data[i];

                    NativeInterface.KEY_EVENT_RECORD keyReleaseEvent = keyPressEvent; // same values for all fields as the key press event, but with the bKeyDown field as "false"
                    keyReleaseEvent.bKeyDown = false;

                    inputRecords[i * 2].EventType     = NativeInterface.KEY_EVENT;
                    inputRecords[i * 2].Event         = keyPressEvent;
                    inputRecords[i * 2 + 1].EventType = NativeInterface.KEY_EVENT;
                    inputRecords[i * 2 + 1].Event     = keyReleaseEvent;
                }

                int recordsWritten;
                ConsoleInterface.callWin32Func(() => NativeInterface.WriteConsoleInput(hConsoleInput, inputRecords, inputRecords.Length, out recordsWritten));
            }
        }
Ejemplo n.º 2
0
        public static char[,] ReadConsoleRaw()
        {
            NativeInterface.CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo = ConsoleInterface.getScreenBufferInfo();

            NativeInterface.SMALL_RECT readRegion = default(NativeInterface.SMALL_RECT);
            readRegion.Right  = screenBufferInfo.dwSize.X;
            readRegion.Bottom = (short)(screenBufferInfo.dwCursorPosition.Y + 1); // read all lines from the top of the screen buffer to the current cursor position (inclusive)

            NativeInterface.CHAR_INFO[] charInfos = new NativeInterface.CHAR_INFO[screenBufferInfo.dwSize.X * screenBufferInfo.dwSize.Y];
            using (SafeHandle hConsoleOutput = ConsoleInterface.getConsoleOutputHandle())
            {
                ConsoleInterface.callWin32Func(() => NativeInterface.ReadConsoleOutput(hConsoleOutput, charInfos, screenBufferInfo.dwSize, default(NativeInterface.COORD), ref readRegion));
            }

            char[,] output = new char[screenBufferInfo.dwSize.X, readRegion.Bottom];
            for (int y = 0; y < readRegion.Bottom; y++)
            {
                for (int x = 0; x < screenBufferInfo.dwSize.X; x++)
                {
                    output[x, y] = charInfos[y * screenBufferInfo.dwSize.X + x].UnicodeChar;
                }
            }

            return(output);
        }
Ejemplo n.º 3
0
 private static NativeInterface.CONSOLE_SCREEN_BUFFER_INFO getScreenBufferInfo()
 {
     using (SafeHandle hConsoleOutput = ConsoleInterface.getConsoleOutputHandle())
     {
         NativeInterface.CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo = default(NativeInterface.CONSOLE_SCREEN_BUFFER_INFO);
         ConsoleInterface.callWin32Func(() => NativeInterface.GetConsoleScreenBufferInfo(hConsoleOutput, out screenBufferInfo));
         return(screenBufferInfo);
     }
 }
Ejemplo n.º 4
0
        public static void ClearConsole()
        {
            using (SafeHandle hConsoleOutput = ConsoleInterface.getConsoleOutputHandle())
            {
                // get dimensions of the screen buffer
                NativeInterface.CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo = ConsoleInterface.getScreenBufferInfo();

                int charsToWrite = screenBufferInfo.dwSize.X * screenBufferInfo.dwSize.Y;
                int charsWritten;
                ConsoleInterface.callWin32Func(() => NativeInterface.FillConsoleOutputCharacter(hConsoleOutput, ConsoleInterface.SPACE_CHAR, charsToWrite, default(NativeInterface.COORD), out charsWritten));
                ConsoleInterface.callWin32Func(() => NativeInterface.SetConsoleCursorPosition(hConsoleOutput, default(NativeInterface.COORD)));
            }
        }
Ejemplo n.º 5
0
        public static void AttachToConsole(int processId, TimeSpan timeout)
        {
            ConsoleInterface.callWin32Func(() => NativeInterface.FreeConsole());
            DateTime startTime = DateTime.Now;

            do
            {
                if (NativeInterface.AttachConsole(processId))
                {
                    return;
                }
            } while (startTime + timeout > DateTime.Now);
            throw new TimeoutException();
        }