Exemplo n.º 1
0
 static extern bool ReadConsoleOutput(
     IntPtr hConsoleOutput,
     [Out] CHAR_INFO[,] lpBuffer,
     COORD dwBufferSize,
     COORD dwBufferCoord,
     ref SMALL_RECT lpReadRegion
     );
Exemplo n.º 2
0
        public static extern bool WriteConsoleOutput(
			IntPtr hConsoleOutput,
			/* This pointer is treated as the origin of a two-dimensional array of CHAR_INFO structures
			whose size is specified by the dwBufferSize parameter.*/
			[MarshalAs(UnmanagedType.LPArray), In] CHAR_INFO[] lpBuffer,
			COORD dwBufferSize,
			COORD dwBufferCoord,
			ref SMALL_RECT lpWriteRegion);
Exemplo n.º 3
0
        public static IEnumerable<string> ReadFromBuffer(short x, short y, short width, short height)
        {
            IntPtr buffer = Marshal.AllocHGlobal(width * height * Marshal.SizeOf(typeof(CHAR_INFO)));
            if (buffer == null)
                throw new OutOfMemoryException();

            try
            {
                COORD coord = new COORD();
                SMALL_RECT rc = new SMALL_RECT();
                rc.Left = x;
                rc.Top = y;
                rc.Right = (short)(x + width - 1);
                rc.Bottom = (short)(y + height - 1);

                COORD size = new COORD();
                size.X = width;
                size.Y = height;

                const int STD_OUTPUT_HANDLE = -11;
                if (!ReadConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE), buffer, size, coord, ref rc))
                {
                    // 'Not enough storage is available to process this command' may be raised for buffer size > 64K (see ReadConsoleOutput doc.)
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                IntPtr ptr = buffer;
                for (int h = 0; h < height; h++)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int w = 0; w < width; w++)
                    {
                        CHAR_INFO ci = (CHAR_INFO)Marshal.PtrToStructure(ptr, typeof(CHAR_INFO));
                        char[] chars = Console.OutputEncoding.GetChars(ci.charData);
                        sb.Append(chars[0]);
                        ptr += Marshal.SizeOf(typeof(CHAR_INFO));
                    }
                    yield return sb.ToString();
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }
Exemplo n.º 4
0
 internal static unsafe extern bool ReadConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO *pBuffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT readRegion);
Exemplo n.º 5
0
 internal static extern unsafe bool WriteConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO *buffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT writeRegion);
Exemplo n.º 6
0
 public static extern bool ReadConsoleOutput(
     IntPtr hConsoleOutput,
     IntPtr pBuffer,
     COORD dwBufferSize,
     COORD dwBufferCoord,
     ref SMALL_RECT lpReadRegion);
Exemplo n.º 7
0
 static extern bool WriteConsoleOutput(IntPtr hConsoleOutput, [In] CHAR_INFO[]
                                       lpBuffer, COORD dwBufferSize, COORD dwBufferCoord,
                                       ref SMALL_RECT lpReadRegion);
Exemplo n.º 8
0
 [DllImport("kernel32.dll", SetLastError = true)] static extern bool SetConsoleWindowInfo(IntPtr hConsoleOutput, bool bAbsolute, [In] ref SMALL_RECT lpConsoleWindow);
Exemplo n.º 9
0
 public static extern bool WriteConsoleOutputW(int Handle, CHAR_INFO[] inputBuffer, COORD inputBufferSize, COORD inputBufferStart, ref SMALL_RECT copyDest);
Exemplo n.º 10
0
 internal static unsafe extern bool ReadConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO* pBuffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT readRegion);
Exemplo n.º 11
0
 internal static extern bool SetConsoleWindowInfo(ConsoleOutputHandle outputHandle, bool absolute, [In] ref SMALL_RECT rect);
Exemplo n.º 12
0
 internal static extern bool ReadConsoleOutput(
     ConsoleOutputHandle consoleOutputHandle,
     [Out] CHAR_INFO[] charInfoBuffer,
     COORD buffersize,
     COORD offset,
     ref SMALL_RECT useRegion);
Exemplo n.º 13
0
 private static extern bool ReadConsoleOutput(IntPtr hConsoleOutput, IntPtr lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, ref SMALL_RECT lpReadRegion);
Exemplo n.º 14
0
 public static extern bool ReadConsoleOutput(SafeHandle hConsoleHandle, [Out] CHAR_INFO[] lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, ref SMALL_RECT lpReadRegion);
Exemplo n.º 15
0
 public static extern bool ScrollConsoleScreenBuffer(IntPtr hConsoleOutput,
                                                     ref SMALL_RECT lpScrollRectangle,
                                                     IntPtr lpClipRectangle,
                                                     COORD dwDestinationOrigin,
                                                     ref CHAR_INFO lpFill);
Exemplo n.º 16
0
        public string readvalue(ref System.IntPtr ptr, short a)
        {
            SMALL_RECT srctReadRect = new SMALL_RECT
            {
                Top = 0,
                Left = 0,
                Bottom = 1,
                Right = 80
            };
            CHAR_INFO[,] chiBuffer = new CHAR_INFO[2, 80]; // [2][80];10 lines,  with 50 characters

            COORD coordBufSize = new COORD
            {
                X = 80,
                Y = 2
            };
            COORD coordBufCoord = new COORD
            {
                X = 0,
                Y = 0
            };

            bool fSuccess;
            int i = 0;
            int j = 0;
            string chartostring = "start";
            string previousstring = "";

            short g = a;
            short h = (short)(g + 1);

            srctReadRect.Top = g;
            srctReadRect.Bottom = h;
            int count = 0;

            //System.Console.WriteLine(g + "." + h);
            while (count < 1)//Hunting:if it find 1 empty rows with text then it will stop reading
            {
                previousstring = chartostring;
                srctReadRect.Top = g;
                srctReadRect.Bottom = h;

                fSuccess = ReadConsoleOutput(ptr, chiBuffer, coordBufSize, coordBufCoord, ref srctReadRect);

                i = 0;
                j = 0;
                chartostring = "";
                while (j < coordBufSize.Y)
                {
                    while (i < coordBufSize.X)
                    {
                        if (chiBuffer[j, i].UnicodeChar != 0 && chiBuffer[j, i].UnicodeChar != 32)
                            chartostring += chiBuffer[j, i].UnicodeChar;
                        i++;
                    }
                    i = 0;
                    j++;
                }

                if (chartostring.Length == 0)//The character length is zero, reverse the top of the source rect
                {
                    count++;
                }
                else
                {
                    count = 0;
                }
                g += 1;
                h += 1;
            }
            return previousstring;
        }
        public void Refresh()
        {
            short w = BufferSize;
            short h = BufferSize;
            short x = 0;
            short y = 0;

            if (!_resizeFlag)
            {
                x = BufferSize - 1;
                y = x;
                short x2 = 0;
                short y2 = 0;
                for (short ix = 0; ix < BufferSize; ix++)
                {
                    for (short iy = 0; iy < BufferSize; iy++)
                    {
                        var ofs = iy * BufferSize + ix;
                        var xor = (_buffer[ofs].Attributes ^ _buffer2[ofs].Attributes) |
                                  (_buffer[ofs].Char ^ _buffer2[ofs].Char);
                        if (xor != 0)
                        {
                            x  = Math.Min(x, ix);
                            y  = Math.Min(y, iy);
                            x2 = Math.Max(x2, ix);
                            y2 = Math.Max(y2, iy);
                        }
                    }
                }
                if (x > x2)
                {
                    x = (short)(x2 + 1);
                }
                if (y > y2)
                {
                    y = (short)(y2 + 1);
                }
                w = (short)(x2 - x + 1);
                h = (short)(y2 - y + 1);
            }
            var info = new CONSOLE_SCREEN_BUFFER_INFO();

            GetConsoleScreenBufferInfo(_stdout, ref info);
            var rect = new SMALL_RECT
            {
                Left   = (short)(info.Window.Left + x),
                Top    = (short)(info.Window.Top + y),
                Right  = (short)(info.Window.Left + x + w - 1),
                Bottom = (short)(info.Window.Top + y + h - 1)
            };

            Memset(new IntPtr(_buffer2), 0, BufferSize * BufferSize);
            WriteConsoleOutputW(_stdout, new IntPtr(_buffer),
                                new COORD {
                X = BufferSize, Y = BufferSize
            },
                                new COORD {
                X = x, Y = y
            }, ref rect);
            if (!_resizeFlag)
            {
                var tbuf = _buffer2;
                _buffer2 = _buffer;
                _buffer  = tbuf;
            }
            _resizeFlag = false;
        }
Exemplo n.º 18
0
        public static bool InitConsole()
        {
            UnityEngine.Debug.Log("InitConsole");

            // if (Application.platform != RuntimePlatform.WindowsPlayer)
            //     return false;

            AllocConsole();

            HandlerRoutine consoleDelegete = new HandlerRoutine(RoutineHandler);

            SetConsoleCtrlHandler(consoleDelegete, true);


            StdOutHandle = GetStdHandle(unchecked ((uint)STD_OUTPUT_HANDLE));
            StdInHandle  = GetStdHandle(unchecked ((uint)STD_INPUT_HANDLE));

            // 允许控制台右键菜单
            uint dwConsoleMode = 0;

            GetConsoleMode(StdInHandle, out dwConsoleMode);
            SetConsoleMode(StdInHandle, ~ENABLE_MOUSE_INPUT & dwConsoleMode);

            // forbid close
            IntPtr hWnd  = GetConsoleWindow();
            IntPtr hMenu = GetSystemMenu(hWnd, 0);

            RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);

            COORD BufferSize = new COORD()
            {
                X = 120,
                Y = 320,
            };

            SMALL_RECT WinRect = new SMALL_RECT()
            {
                Left   = 0,
                Top    = 0,
                Right  = (short)(BufferSize.X - 1),
                Bottom = 30,
            };

            IntPtr pBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(BufferSize));

            Marshal.StructureToPtr(BufferSize, pBuffer, false);
            if (!SetConsoleScreenBufferSize(StdOutHandle, pBuffer))
            {
                //return false;
            }
            Marshal.FreeHGlobal(pBuffer);

            IntPtr pWinRect = Marshal.AllocHGlobal(Marshal.SizeOf(WinRect));

            Marshal.StructureToPtr(WinRect, pWinRect, false);
            if (!SetConsoleWindowInfo(StdOutHandle, true, pWinRect))
            {
                //return false;
            }
            Marshal.FreeHGlobal(pWinRect);

            ConsoleAlive = true;

            Thread th = new Thread(new ThreadStart(WorkingThread));

            th.Start();

            return(true);
        }
Exemplo n.º 19
0
    public static bool InitConsole()
    {
        if (Application.platform != RuntimePlatform.WindowsPlayer)
            return false;

        AllocConsole();
        SetConsoleCtrlHandler(null, true);

        StdOutHandle = GetStdHandle(unchecked((uint)STD_OUTPUT_HANDLE));
        StdInHandle = GetStdHandle(unchecked((uint)STD_INPUT_HANDLE));

        // 允许控制台右键菜单
        uint dwConsoleMode = 0;
        GetConsoleMode(StdInHandle, out dwConsoleMode);
        SetConsoleMode(StdInHandle, ~ENABLE_MOUSE_INPUT & dwConsoleMode);

        // forbid close
        IntPtr hWnd = GetConsoleWindow();
        IntPtr hMenu = GetSystemMenu(hWnd, 0);
        RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);

        COORD BufferSize = new COORD()
        {
            X = 120,
            Y = 320,
        };

        SMALL_RECT WinRect = new SMALL_RECT()
        {
            Left = 0,
            Top = 0,
            Right = (short)(BufferSize.X - 1),
            Bottom = 30,
        };

        IntPtr pBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(BufferSize));
        Marshal.StructureToPtr(BufferSize, pBuffer, false);
        Debug.Log(SetConsoleScreenBufferSize(StdOutHandle, pBuffer));
        Marshal.FreeHGlobal(pBuffer);

        IntPtr pWinRect = Marshal.AllocHGlobal(Marshal.SizeOf(WinRect));
        Marshal.StructureToPtr(WinRect, pWinRect, false);
        Debug.Log(SetConsoleWindowInfo(StdOutHandle, true, pWinRect));
        Marshal.FreeHGlobal(pWinRect);

        ConsoleAlive = true;

        Thread th = new Thread(new ThreadStart(WorkingThread));
        th.Start();

        return true;
    }
Exemplo n.º 20
0
 [DllImport("kernel32.dll", EntryPoint = "WriteConsoleOutputW", CharSet = CharSet.Unicode, SetLastError = true)] static extern bool WriteConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO[] lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, ref SMALL_RECT lpWriteRegion);
 private static extern bool SetConsoleWindowInfo(IntPtr hConsole, bool absolute, ref SMALL_RECT rect);
Exemplo n.º 22
0
 private static extern bool WriteConsoleOutput(
     IntPtr handleConsoleOutput,
     CHAR_INFO[] lpBuffer,
     COORD dwBufferSize,
     COORD dwBufferCoord,
     ref SMALL_RECT lpWriteRegion);
Exemplo n.º 23
0
 public static extern bool WriteConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO2[] lpBuffer, COORD dwBufferSize, COORD dwWriteCoord, ref SMALL_RECT lpWriteRegion);
Exemplo n.º 24
0
 public static extern bool SetConsoleWindowInfo(
     SafeFileHandle hConsoleOutput,
     bool bAbsolute,
     [In] ref SMALL_RECT lpConsoleWindow
     );
Exemplo n.º 25
0
 public static extern bool ReadConsoleOutput(SafeHandle hConsoleHandle, [Out] CHAR_INFO[] lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, ref SMALL_RECT lpReadRegion);
Exemplo n.º 26
0
 static extern bool SetConsoleWindowInfo(
     IntPtr hConsoleOutput,
     bool bAbsolute,
     [In] ref SMALL_RECT lpConsoleWindow
     );
Exemplo n.º 27
0
        public static void WriteOutput(CharInfo[,] chars, COORD bufferSize, COORD coord)
        {
            SMALL_RECT rect = new SMALL_RECT(coord, bufferSize);

            WinApi.WriteConsoleOutputW(bufferHandle, chars, bufferSize, new COORD(), ref rect);
        }
Exemplo n.º 28
0
        private void ReadConsoleBuffer()
        {
            var buffer = Marshal.AllocHGlobal(25 * 80 * Marshal.SizeOf(typeof(CHAR_INFO)));

            var rect = new SMALL_RECT { Left = 0, Top = 0, Right = 79, Bottom = 24 };

            ReadConsoleOutput(_screenBuffer, buffer, new COORD { X = 80, Y = 25 }, new COORD { X = 0, Y = 0 }, ref rect);

            var sb = new StringBuilder(80 * 25);
            var ptr = buffer;
            for (int i = 0; i < 80 * 25; i++)
            {
                var ci = (CHAR_INFO)Marshal.PtrToStructure(ptr, typeof(CHAR_INFO));
                sb.Append(ci.UnicodeChar);
                ptr += Marshal.SizeOf(typeof(CHAR_INFO));
            }
        }
Exemplo n.º 29
0
        public static void SetWindowSize(int width, int height, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string filePath = "")
        {
            SMALL_RECT info = new SMALL_RECT(0, 0, (short)width, (short)height);

            HandleError(!WinApi.SetConsoleWindowInfo(bufferHandle, true, ref info), lineNumber, filePath);
        }
Exemplo n.º 30
0
 public static extern bool WriteConsoleOutput(
     IntPtr hConsoleOutput,
     CHAR_INFO[] lpBuffer,
     COORD dwBufferSize,
     COORD dwBufferCoord,
     ref SMALL_RECT lpWriteRegion
     );
Exemplo n.º 31
0
 public static extern bool GetWindowRect(IntPtr hwnd, out SMALL_RECT lpRect);
Exemplo n.º 32
0
 private static extern bool ReadConsoleOutput(IntPtr hConsoleBuffer, [MarshalAs(UnmanagedType.LPArray), Out] CHAR_INFO[,] lpBuffer,
                                              COORD BufferSize, COORD BufferCoord, ref SMALL_RECT lpReadRegion);
Exemplo n.º 33
0
 public static extern bool GetWindowRect(IntPtr hwnd, out SMALL_RECT lpRect);
Exemplo n.º 34
0
 internal static unsafe extern bool SetConsoleWindowInfo(IntPtr hConsoleOutput, 
     bool absolute, SMALL_RECT* consoleWindow);
Exemplo n.º 35
0
        static void Box(List <string> list)
        {
            int internalBoxWidth  = Math.Min(Console.BufferWidth - 2, list.Max(e => e.Length));
            int boxWidth          = internalBoxWidth + 2;
            int internalBoxHeight = list.Count;
            int boxHeight         = internalBoxHeight + 2;

            var buffer = new CHAR_INFO[boxWidth * boxHeight];

            buffer[0].UnicodeChar            = '+';
            buffer[boxWidth - 1].UnicodeChar = '+';
            for (int i = 1; i < boxWidth - 1; i++)
            {
                buffer[i].UnicodeChar = '-';
            }
            for (int i = 0; i < list.Count; i++)
            {
                int rowStart = (i + 1) * boxWidth;
                buffer[rowStart++].UnicodeChar = '|';
                buffer[rowStart + internalBoxWidth].UnicodeChar = '|';

                string s = list[i];
                int    j;
                for (j = 0; j < s.Length; j++)
                {
                    buffer[rowStart + j].UnicodeChar = s[j];
                }
                for (; j < internalBoxWidth; j++)
                {
                    buffer[rowStart + j].UnicodeChar = ' ';
                }
            }
            int lastRowStart = (boxHeight - 1) * boxWidth;

            buffer[lastRowStart].UnicodeChar = '+';
            for (int i = 1; i < boxWidth - 1; i++)
            {
                buffer[i + lastRowStart].UnicodeChar = '-';
            }
            buffer[lastRowStart + boxWidth - 1].UnicodeChar = '+';

            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i].Attributes = (ushort)ConsoleColor.Blue |
                                       ((ushort)(ConsoleColor.DarkGreen) << 4);
                if (i % 2 != 0)
                {
                    buffer[i].Attributes |= 0xfff0;
                }
            }

            var handle     = NativeMethods.GetStdHandle((uint)StandardHandleId.Output);
            var bufferSize = new COORD {
                X = (short)boxWidth, Y = (short)boxHeight
            };
            var bufferCoord = new COORD {
                X = 0, Y = 0
            };
            var writeRegion = new SMALL_RECT {
                Top    = 1,
                Left   = 1,
                Bottom = (short)(1 + boxHeight),
                Right  = (short)(1 + boxWidth)
            };

            Console.WriteLine("some random stuff");
            Console.WriteLine("and more some random stuff");
            Console.WriteLine("lorem ipsum blah blah");
            Console.ReadKey();
            var saveBuffer = new CHAR_INFO[buffer.Length];

            NativeMethods.ReadConsoleOutput(handle, saveBuffer,
                                            bufferSize, bufferCoord, ref writeRegion);
            unsafe
            {
                fixed(CHAR_INFO *p = &buffer[0])
                fixed(CHAR_INFO * sp = &saveBuffer[0])
                {
                    NativeMethods.WriteConsoleOutput(handle, buffer,
                                                     bufferSize, bufferCoord, ref writeRegion);
                    Console.ReadKey();
                    NativeMethods.WriteConsoleOutput(handle, saveBuffer,
                                                     bufferSize, bufferCoord, ref writeRegion);
                }
            }
            Console.ReadKey();
        }
Exemplo n.º 36
0
 internal static unsafe extern bool WriteConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO* buffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT writeRegion);
Exemplo n.º 37
0
 internal static extern bool WriteConsoleOutput(
     IntPtr hConsoleOutput,
     [MarshalAs(UnmanagedType.LPArray), In] CHAR_INFO[] lpBuffer,
     COORD dwBufferSize,
     COORD dwBufferCoord,
     ref SMALL_RECT lpWriteRegion);