cStdIO Methods Reference
ExecuteCommand
Executes command line program and captures its standard output.
vb
Public Function ExecuteCommand( _
ByVal CommandLine As String, _
Optional ByVal TimeoutMs As Long = 10000, _
Optional ByVal CurrentDirectory As String = "", _
Optional ByRef exitCode As Long _
) As StringParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
CommandLine | String | - | Command line to execute (includes program path and arguments) |
TimeoutMs | Long | 10000 | Timeout (milliseconds), default 10 seconds |
CurrentDirectory | String | "" | Working directory, empty string means use current directory |
exitCode | Long | - | Output parameter, process exit code |
Return Value
- Success - Returns command line standard output text
- Failure - Returns error description string (starting with "STDIO ERROR:")
Error Codes
Error codes returned via exitCode parameter:
| Error Code | Constant | Description |
|---|---|---|
| 0 | ERROR_OK | Execution successful |
| 1 | ERROR_CREATE_PIPE | Failed to create pipe |
| 2 | ERROR_DUPLICATE_HANDLE | Failed to duplicate handle |
| 3 | ERROR_CREATE_PROCESS | Failed to create process |
| 4 | ERROR_READ_OUTPUT | Failed to read output |
| 5 | ERROR_PROCESS_TIMEOUT | Execution timeout |
Examples
Basic Usage
vb
Dim StdIO As New cStdIO
Dim Output As String
Dim ExitCode As Long
Output = StdIO.ExecuteCommand("echo Hello World", 5000, "", ExitCode)
Debug.Print Output ' Output: Hello WorldCommand with Arguments
vb
Dim StdIO As New cStdIO
Dim Output As String
Dim ExitCode As Long
' Execute command with arguments
Output = StdIO.ExecuteCommand( _
"findstr /s /i ""keyword"" *.txt", _
10000, _
"C:\\Documents", _
ExitCode _
)
If ExitCode = 0 Then
Debug.Print "Found match: " & Output
Else
Debug.Print "No match found"
End IfComplete Error Handling
vb
Private Sub ExecuteSafe()
Dim StdIO As New cStdIO
Dim Result As String
Dim ExitCode As Long
On Error GoTo ErrorHandler
Result = StdIO.ExecuteCommand("mycommand.exe", 5000, "", ExitCode)
' Check if returns error string
If Left$(Result, 11) = "STDIO ERROR" Then
MsgBox "Execution error: " & Result, vbCritical
Exit Sub
End If
' Check exit code
Select Case ExitCode
Case 0
Debug.Print "Execution successful: " & Result
Case 1
Debug.Print "Command returned error: " & Result
Case 5
MsgBox "Execution timeout!", vbExclamation
Case Else
Debug.Print "Unknown error, exit code: " & ExitCode
End Select
Exit Sub
ErrorHandler:
MsgBox "Exception occurred: " & Err.Description, vbCritical
End SubSpecify Working Directory
vb
Private Sub RunInDirectory()
Dim StdIO As New cStdIO
Dim Output As String
Dim ExitCode As Long
' Execute git command in specified directory
Output = StdIO.ExecuteCommand( _
"git log --oneline -10", _
10000, _
"D:\\Projects\\MyRepo", _
ExitCode _
)
If ExitCode = 0 Then
txtLog.Text = Output
End If
End Sub