Skip to content

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 String

Parameters

ParameterTypeDefaultDescription
CommandLineString-Command line to execute (includes program path and arguments)
TimeoutMsLong10000Timeout (milliseconds), default 10 seconds
CurrentDirectoryString""Working directory, empty string means use current directory
exitCodeLong-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 CodeConstantDescription
0ERROR_OKExecution successful
1ERROR_CREATE_PIPEFailed to create pipe
2ERROR_DUPLICATE_HANDLEFailed to duplicate handle
3ERROR_CREATE_PROCESSFailed to create process
4ERROR_READ_OUTPUTFailed to read output
5ERROR_PROCESS_TIMEOUTExecution 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 World

Command 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 If

Complete 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 Sub

Specify 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

VB6 and LOGO copyright of Microsoft Corporation