2017年11月
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
無料ブログはココログ

« コンソールウィンドウの表示を変更するコマンドをVB.NETで作る | トップページ | コマンドプロンプトのコンソールログを採取するコマンドをVB.NETで作る(その2) »

2008年8月29日 (金)

コマンドプロンプトのコンソールログを採取するコマンドをVB.NETで作る

コンソールスクリーンバッファの中身を標準出力に取り出します。

vbc concopy.vb

Public Class Class1

Private Structure COORD
Dim x As Short
Dim y As Short
End Structure

Private Structure SMALL_RECT
Dim Left As Short
Dim Top As Short
Dim Right As Short
Dim Bottom As Short
End Structure

Private Structure CONSOLE_SCREEN_BUFFER_INFO
Dim dwSize As COORD
Dim dwCursorPosition As COORD
Dim wAttributes As Short
Dim srWindow As SMALL_RECT
Dim dwMaximumWindowSize As COORD
End Structure

Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Integer) As Integer
Private Declare Function GetConsoleScreenBufferInfo Lib "kernel32" (ByVal hConsoleOutput As Integer, ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer
Private Declare Function ReadConsoleOutputCharacter Lib "kernel32" Alias "ReadConsoleOutputCharacterA" (ByVal hConsoleOutput As Integer, ByVal lpCharacter As String, ByVal nLength As Integer, ByVal dwReadCoord As COORD, ByRef lpNumberOfCharsRead As Integer) As Integer

Private Const STD_INPUT_HANDLE As Integer = -10
Private Const STD_OUTPUT_HANDLE As Integer = -11
Private Const STD_ERROR_HANDLE As Integer = -12

Public Shared Sub Main()
Try
  Dim hConsoleOutput As Integer = GetStdHandle(STD_ERROR_HANDLE)
  Dim ConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO
  GetConsoleScreenBufferInfo(hConsoleOutput, ConsoleScreenBufferInfo)
  For y As Integer = 0 To ConsoleScreenBufferInfo.dwCursorPosition.y-1
    Dim ConsoleText As String = New String(vbNullChar, ConsoleScreenBufferInfo.dwSize.x)
    Dim nLength As Integer =  ConsoleScreenBufferInfo.dwSize.x
    Dim dwReadCoord As COORD
    dwReadCoord.x = 0
    dwReadCoord.y = y
    Dim NumberOfCharsRead As Integer
    ReadConsoleOutputCharacter(hConsoleOutput, ConsoleText, nLength, dwReadCoord, NumberOfCharsRead)
    Console.WriteLine(ConsoleText.TrimEnd(" "c,vbNullChar))
  Next
Catch
  Console.Error.WriteLine("Source" & vbTab & vbTab & Err.Source & vbLf & "Number" & vbTab & vbTab & Err.Number & vbLf & "Description" & vbTab & Err.Description & vbLf & "DLL Error" & vbTab & vbTab & Err.LastDLLError)
End Try
End Sub
End Class

« コンソールウィンドウの表示を変更するコマンドをVB.NETで作る | トップページ | コマンドプロンプトのコンソールログを採取するコマンドをVB.NETで作る(その2) »