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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| Option Explicit
'#####################################################################################
'# Standard IO (StdIn/StdOut/StdErr) Wrapper Class (clsStdIO.cls)
'# By: Nick Campbeln
'# Aug 4, 2002
'#
'# Please remember to vote on PSC.com if you like this code!
'# Code URL: http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=37607&lngWId=1
'#####################################################################################
'#### Declare the required APIs for StdIn/Out
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, ByVal lpBuffer As String, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long
'#### Declare the required consts for StdIn/StdOut/StdErr
Private Const STD_INPUT_HANDLE = -10&
Private Const STD_OUTPUT_HANDLE = -11&
Private Const STD_ERROR_HANDLE = -12&
'#####################################################################################
'# Public class subs/functions
'#####################################################################################
'#########################################################
'# Reads the data from StdIn, returning it to the caller
'#########################################################
Public Function StdIn() As String
Dim sBuffer As String
Dim lBytesRead As Long
Dim lStdInHandle As Long
'#### Setup lStdInHandle and default the return value
lStdInHandle = GetStdHandle(STD_INPUT_HANDLE)
StdIn = ""
'#### Loop while reading from StdIn
Do
'#### (Re)Set the sBuffer to a length of 1024
sBuffer = String(65536, 0)
'#### If we can successfully read from StdIn
If (ReadFile(lStdInHandle, ByVal sBuffer, Len(sBuffer), lBytesRead, ByVal 0&)) Then
'#### Append the data collected above onto the return value
StdIn = StdIn & Left(sBuffer, lBytesRead)
'#### Else StdIn was not able to to read, so fall out of the loop
Else
Exit Do
End If
Loop
End Function
'#########################################################
'# Writes the passed sData to StdOut
'#########################################################
Public Sub StdOut(sData)
'#### Call the API, passing in STD_OUTPUT_HANDLE as the file handle to write to
'Call WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), sData & vbCrLf, Len(sData & vbCrLf), 0, ByVal 0&)
Call WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), sData, Len(sData), 0, ByVal 0&)
End Sub
'#########################################################
'# Writes the passed sData to StdErr
'#########################################################
Public Sub StdOutErr(sData)
'#### Call the API, passing in STD_ERROR_HANDLE as the file handle to write to
Call WriteFile(GetStdHandle(STD_ERROR_HANDLE), sData & vbCrLf, Len(sData & vbCrLf), 0, ByVal 0&)
End Sub
Public Sub StdInOut(FileName As String)
Dim StdIn As String
Dim sBuffer As String
Dim lBytesRead As Long
Dim lStdInHandle As Long
Dim hFich As Integer
hFich = FreeFile
Open FileName For Output Access Write Shared As #hFich
'#### Setup lStdInHandle and default the return value
lStdInHandle = GetStdHandle(STD_INPUT_HANDLE)
StdIn = ""
'#### Loop while reading from StdIn
Do
'#### (Re)Set the sBuffer to a length of 1024
sBuffer = String(65536, 0)
'#### If we can successfully read from StdIn
If (ReadFile(lStdInHandle, ByVal sBuffer, Len(sBuffer), lBytesRead, ByVal 0&)) Then
'#### Append the data collected above onto the return value
'StdIn = StdIn & Left(sBuffer, lBytesRead)
StdIn = Left(sBuffer, lBytesRead)
Print #hFich, StdIn;
StdOut StdIn
DoEvents
'#### Else StdIn was not able to to read, so fall out of the loop
Else
Exit Do
End If
Loop
Close #hFich
End Sub |
Partager