Redirect






4.94/5 (9 votes)
May 4, 2000

160657

2789
An ATL-control for redirecting stdout/stdin
Redirect
This control makes it possible to redirect the stdin/stdout of a console-application to your program. It's even possible to redirect MS-DOS commands to your application. I've written this project in Visual C++ 6.0 with ATL 3.0, because you can't use multithreading in Visual Basic.
Properties of the Application Object
Name | Type | R/W | Description |
---|---|---|---|
BufferSize | Integer | Read/Write | Returns or sets the buffersize that is used to read the stdout. Default is 8192. |
LastErrorNumber | Long | Read | Returns the last windows errornumber. |
Name | String | Read/Write | The name of the console-application. |
Running | Boolean | Read | Returns true if the console-application is running. |
Wait | Long | Read/Write | Sets the milliseconds to wait for checking the stdout. If the console-application runs a long time, make sure you set this property, because otherwise your program will not have enough time to process other tasks. |
Methods of the Application Object
Name | Returns | Parameters | Description |
---|---|---|---|
Start | eStartResult | / | Starts the console-application. Returns laAlreadyRunning when the console-application was already started. Retuns laWindowsError when an error occurred. Returns laOk when the console-application is started correctly. |
Stop | / | Stops the console-application. | |
Write | Boolean | Byval sCommandString As String | Writes sCommandString to the stdin of the console-application. Returns false when an error ocurred. |
Events of the Application Object
Name | Parameters | Description |
---|---|---|
DataReceived | sData As String | The data from the stdout of the console-application. |
ProcessEnded | / | The console-application ended. |
Example
The following VB-Example starts the MS-DOS prompt in Windows'98 and executes the dir command. The received data is displayed in a TextBox.
Dim WithEvents oLaunch As redirectlib.Application Set oLaunch = New redirectlib.Application oLaunch.BufferSize = 8192 oLaunch.Wait = 1000 oLaunch.Name = "c:\windows\command.com" Select Case oLaunch.Start Case laAlreadyRunning MsgBox "Already running !" Case laWindowsError MsgBox "Windows error: " & CStr(oLaunch.LastErrorNumber) & "!" Case laOk oLaunch.Write "dir" + vbCrLf oLaunch.Write "exit" + vbCrLf End Select Private Sub oLaunch_DataReceived(ByVal sData As String) txtOutput.Text = txtOutput.Text + sData txtOutput.SelStart = Len(txtOutput.Text) End Sub Private Sub oLaunch_ProcessEnded() MsgBox "Program stopped" End Sub
Remarks
When you use the MS-prompt in Windows NT (cmd.exe) then you can stop the console-application with the Stop-method. However in Windows '98 this doesn't work. Why I don't know. You can solve this by writing the "exit"-command to stdin of the console-application. Other console-applications work fine with NT and '98.
The control was tested on Windows NT and Windows '98.
The control is written with Visual C++ 6.0 using ATL 3.0. The control was tested with Visual Basic 6.0
Resources
These are resources I've used to create this project :
- HOWTO: Spawn Console Processes with Redirected Standard Handles
- Redirecting Standard Output to a CEdit Control
- Redirect Output of CMD.EXE to a Pipe
Check my website for updates.