Advertisement
Guest User

VB.NET ConsoleReader

a guest
Oct 7th, 2013
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.52 KB | None | 0 0
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.ComponentModel
  4. Imports System.Runtime.InteropServices
  5. Imports System.Text
  6.  
  7. Namespace ConsoleReaderTest
  8.     Public Class ConsoleReader
  9.         Public Shared Function ReadFromBuffer(ByVal x As Short, ByVal y As Short, ByVal width As Short, ByVal height As Short) As IList(Of String)
  10.             Dim lpBuffer As IntPtr = Marshal.AllocHGlobal(CInt(((width * height) * Marshal.SizeOf(GetType(CHAR_INFO)))))
  11.             Dim list As New List(Of String)
  12.             Try
  13.                 Dim dwBufferCoord As New COORD
  14.                 Dim lpReadRegion As New SMALL_RECT With { _
  15.                     .Left = x, _
  16.                     .Top = y, _
  17.                     .Right = CShort(((x + width) - 1)), _
  18.                     .Bottom = CShort(((y + height) - 1)) _
  19.                 }
  20.                 Dim dwBufferSize As New COORD With { _
  21.                     .X = width, _
  22.                     .Y = height _
  23.                 }
  24.  
  25.                 If Not ConsoleReader.ReadConsoleOutput(ConsoleReader.GetStdHandle(-11), lpBuffer, dwBufferSize, dwBufferCoord, lpReadRegion) Then
  26.                     Throw New Win32Exception(Marshal.GetLastWin32Error)
  27.                 End If
  28.  
  29.                 Dim ptr As IntPtr = lpBuffer
  30.                 Dim i As Integer
  31.                 For i = 0 To height - 1
  32.                     Dim builder As New StringBuilder
  33.                     Dim j As Integer
  34.                     For j = 0 To width - 1
  35.                         Dim char_info As CHAR_INFO = DirectCast(Marshal.PtrToStructure(ptr, GetType(CHAR_INFO)), CHAR_INFO)
  36.                         Dim chars As Char() = Console.OutputEncoding.GetChars(char_info.charData)
  37.                         builder.Append(chars(0))
  38.                         ptr = (ptr + Marshal.SizeOf(GetType(CHAR_INFO)))
  39.                     Next j
  40.                     list.Add(builder.ToString)
  41.                 Next i
  42.  
  43.             Finally
  44.                 Marshal.FreeHGlobal(lpBuffer)
  45.             End Try
  46.             Return list
  47.         End Function
  48.  
  49.         <DllImport("kernel32.dll", SetLastError:=True)> _
  50.         Private Shared Function GetStdHandle(ByVal nStdHandle As Integer) As IntPtr
  51.         End Function
  52.  
  53.         <DllImport("kernel32.dll", SetLastError:=True)> _
  54.         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
  55.         End Function
  56.  
  57.         <StructLayout(LayoutKind.Sequential)> _
  58.         Private Structure CHAR_INFO
  59.             <MarshalAs(UnmanagedType.ByValArray, SizeConst:=2)> _
  60.             Public charData As Byte()
  61.             Public attributes As Short
  62.         End Structure
  63.  
  64.         <StructLayout(LayoutKind.Sequential)> _
  65.         Private Structure CONSOLE_SCREEN_BUFFER_INFO
  66.             Public dwSize As COORD
  67.             Public dwCursorPosition As COORD
  68.             Public wAttributes As Short
  69.             Public srWindow As SMALL_RECT
  70.             Public dwMaximumWindowSize As COORD
  71.         End Structure
  72.  
  73.         <StructLayout(LayoutKind.Sequential)> _
  74.         Private Structure COORD
  75.             Public X As Short
  76.             Public Y As Short
  77.         End Structure
  78.  
  79.         <StructLayout(LayoutKind.Sequential)> _
  80.         Private Structure SMALL_RECT
  81.             Public Left As Short
  82.             Public Top As Short
  83.             Public Right As Short
  84.             Public Bottom As Short
  85.         End Structure
  86.     End Class
  87. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement