Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB4/32,VB5,VB6
Expertise: Intermediate
May 19, 2001



Building the Right Environment to Support AI, Machine Learning and Deep Learning

ReadFromStdInput - Read from standard input stream

Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) _
    As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _
    lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, _
    lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long

Private Const STD_INPUT_HANDLE = -10&

' read a number of chars from standard input channel
' if NumBytes < 0 then it reads all the bytes
' from the standard input
' NOTE: this routine works only in compiled applications

Function 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)
        ' 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 < Len(buffer)
End Function
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date