Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System
- Imports System.Collections.Generic
- Imports System.ComponentModel
- Imports System.Runtime.InteropServices
- Imports System.Text
- Namespace ConsoleReaderTest
- Public Class ConsoleReader
- Public Shared Function ReadFromBuffer(ByVal x As Short, ByVal y As Short, ByVal width As Short, ByVal height As Short) As IList(Of String)
- Dim lpBuffer As IntPtr = Marshal.AllocHGlobal(CInt(((width * height) * Marshal.SizeOf(GetType(CHAR_INFO)))))
- Dim list As New List(Of String)
- Try
- Dim dwBufferCoord As New COORD
- Dim lpReadRegion As New SMALL_RECT With { _
- .Left = x, _
- .Top = y, _
- .Right = CShort(((x + width) - 1)), _
- .Bottom = CShort(((y + height) - 1)) _
- }
- Dim dwBufferSize As New COORD With { _
- .X = width, _
- .Y = height _
- }
- If Not ConsoleReader.ReadConsoleOutput(ConsoleReader.GetStdHandle(-11), lpBuffer, dwBufferSize, dwBufferCoord, lpReadRegion) Then
- Throw New Win32Exception(Marshal.GetLastWin32Error)
- End If
- Dim ptr As IntPtr = lpBuffer
- Dim i As Integer
- For i = 0 To height - 1
- Dim builder As New StringBuilder
- Dim j As Integer
- For j = 0 To width - 1
- Dim char_info As CHAR_INFO = DirectCast(Marshal.PtrToStructure(ptr, GetType(CHAR_INFO)), CHAR_INFO)
- Dim chars As Char() = Console.OutputEncoding.GetChars(char_info.charData)
- builder.Append(chars(0))
- ptr = (ptr + Marshal.SizeOf(GetType(CHAR_INFO)))
- Next j
- list.Add(builder.ToString)
- Next i
- Finally
- Marshal.FreeHGlobal(lpBuffer)
- End Try
- Return list
- End Function
- <DllImport("kernel32.dll", SetLastError:=True)> _
- Private Shared Function GetStdHandle(ByVal nStdHandle As Integer) As IntPtr
- End Function
- <DllImport("kernel32.dll", SetLastError:=True)> _
- Private Shared Function ReadConsoleOutput(ByVal hConsoleOutput As IntPtr, ByVal lpBuffer As IntPtr, ByVal dwBufferSize As COORD, ByVal dwBufferCoord As COORD, ByRef lpReadRegion As SMALL_RECT) As Boolean
- End Function
- <StructLayout(LayoutKind.Sequential)> _
- Private Structure CHAR_INFO
- <MarshalAs(UnmanagedType.ByValArray, SizeConst:=2)> _
- Public charData As Byte()
- Public attributes As Short
- End Structure
- <StructLayout(LayoutKind.Sequential)> _
- Private Structure CONSOLE_SCREEN_BUFFER_INFO
- Public dwSize As COORD
- Public dwCursorPosition As COORD
- Public wAttributes As Short
- Public srWindow As SMALL_RECT
- Public dwMaximumWindowSize As COORD
- End Structure
- <StructLayout(LayoutKind.Sequential)> _
- Private Structure COORD
- Public X As Short
- Public Y As Short
- End Structure
- <StructLayout(LayoutKind.Sequential)> _
- Private Structure SMALL_RECT
- Public Left As Short
- Public Top As Short
- Public Right As Short
- Public Bottom As Short
- End Structure
- End Class
- End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement