devxlogo

ReadFromStdInput – Read from standard input stream

ReadFromStdInput – Read from standard input stream

Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) _    As LongPrivate Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _    lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, _    lpNumberOfBytesRead As Long, lpOverlapped As Any) As LongPrivate Const STD_INPUT_HANDLE = -10&' read a number of chars from standard input channel'' if NumBytes ' from the standard input'' NOTE: this routine works only in compiled applicationsFunction ReadFromStdInput(Optional ByVal NumBytes As Long = -1) As String    Dim hStdIn As Long    Dim ret As Long    Dim buffer As String    Dim bytesRead As Long        ' get the handle of standard input    hStdIn = GetStdHandle(STD_INPUT_HANDLE)        ' prepare receiving buffer    buffer = Space$(1024)        Do        ' read from standard input        ret = ReadFile(hStdIn, ByVal buffer, Len(buffer), bytesRead, ByVal 0&)            ' deal with errors        If ret = 0 Then            Err.Raise 1001, , "Unable to read from standard input"        End If                ' append to result built so far        ReadFromStdInput = ReadFromStdInput & Left$(buffer, bytesRead)                ' continue to read if there are more chars    Loop Until bytesRead 

devx-admin

Share the Post: