Skip to content

Instantly share code, notes, and snippets.

@pravynandas
Forked from xaprb/ModStdIO.bas
Created June 24, 2021 23:26
Show Gist options
  • Save pravynandas/df0dafd226e2fbd06d0f2935fa7d49e5 to your computer and use it in GitHub Desktop.
Save pravynandas/df0dafd226e2fbd06d0f2935fa7d49e5 to your computer and use it in GitHub Desktop.
Attribute VB_Name = "ModStdIO"
Option Explicit
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 Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, _
lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long
Private Const STD_OUTPUT_HANDLE = -11&
Private Const STD_INPUT_HANDLE = -10&
Function ReadStdIn(Optional ByVal NumBytes As Long = -1) As String
Dim StdIn As Long
Dim Result As Long
Dim Buffer As String
Dim BytesRead As Long
StdIn = GetStdHandle(STD_INPUT_HANDLE)
Buffer = Space$(1024)
Do
Result = ReadFile(StdIn, ByVal Buffer, Len(Buffer), BytesRead, ByVal 0&)
If Result = 0 Then
Err.Raise 1001, , "Unable to read from standard input"
End If
ReadStdIn = ReadStdIn & Left$(Buffer, BytesRead)
Loop Until BytesRead < Len(Buffer)
End Function
Sub WriteStdOut(ByVal Text As String)
Dim StdOut As Long
Dim Result As Long
Dim BytesWritten As Long
StdOut = GetStdHandle(STD_OUTPUT_HANDLE)
Result = WriteFile(StdOut, ByVal Text, Len(Text), BytesWritten, ByVal 0&)
If Result = 0 Then
Err.Raise 1001, , "Unable to write to standard output"
ElseIf BytesWritten < Len(Text) Then
Err.Raise 1002, , "Incomplete write operation"
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment