Click here to Skip to main content
14,977,910 members
Articles / Desktop Programming / ATL
Posted 3 May 2000


37 bookmarked


Rate me:
Please Sign up or sign in to vote.
4.94/5 (10 votes)
7 Jun 2000
An ATL-control for redirecting stdout/stdin
  • Download demo project - 6 Kb
  • Download source - 21Kb
  • Download control (ver. Windows '98) - 26 Kb
  • 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.


    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\"
      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


    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


    These are resources I've used to create this project :

    1. HOWTO: Spawn Console Processes with Redirected Standard Handles
    2. Redirecting Standard Output to a CEdit Control
    3. Redirect Output of CMD.EXE to a Pipe

    Check my website for updates.


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Franky Braem
    Web Developer
    Belgium Belgium
    Programmer since 1991 using C/C++/Visual Basic and Java. Playing with wxWindows at home.

    Comments and Discussions

    GeneralClean Shutdown Pin
    sprice8626-Apr-10 22:58
    professionalsprice8626-Apr-10 22:58 
    GeneralProject request Pin
    aryanabc29-Jun-09 20:02
    Memberaryanabc29-Jun-09 20:02 
    GeneralCaptureConsole.DLL - A universal Console Output Redirector for all Compilers Pin
    Elmue3-Feb-09 5:54
    MemberElmue3-Feb-09 5:54 
    GeneralRe: CaptureConsole.DLL - A universal Console Output Redirector for all Compilers Pin
    sprice8614-May-10 8:38
    professionalsprice8614-May-10 8:38 
    GeneralRe: CaptureConsole.DLL - A universal Console Output Redirector for all Compilers Pin
    Elmue16-May-10 7:51
    MemberElmue16-May-10 7:51 
    GeneralApologies Pin
    sprice8617-May-10 3:35
    professionalsprice8617-May-10 3:35 
    GeneralRe: Apologies Pin
    Elmue17-May-10 13:41
    MemberElmue17-May-10 13:41 
    QuestionRun multiple cmd.exe sessions and send/receive input/output to differnet VB controls? Pin
    tfertodd14-Jun-08 18:42
    Membertfertodd14-Jun-08 18:42 
    GeneralGreat stuff Pin
    tnsbox30-Oct-07 14:57
    Membertnsbox30-Oct-07 14:57 
    Questioncan we use the dll in c# Pin
    aodeng9312-Dec-06 12:01
    Memberaodeng9312-Dec-06 12:01 
    GeneralNot all stdout or stderr is printed Pin
    J.Ru21-Nov-04 11:15
    MemberJ.Ru21-Nov-04 11:15 
    Generalreal time screen update Pin
    BertV4-Dec-03 3:39
    MemberBertV4-Dec-03 3:39 
    GeneralRe: real time screen update Pin
    Franky Braem4-Dec-03 7:15
    MemberFranky Braem4-Dec-03 7:15 
    Generalusing control in vbscript or javascript Pin
    David Hubbard4-Oct-00 9:40
    MemberDavid Hubbard4-Oct-00 9:40 
    GeneralRe: using control in vbscript or javascript Pin
    Franky Braem5-Oct-00 0:57
    MemberFranky Braem5-Oct-00 0:57 
    GeneralRe: using control in vbscript or javascript Pin
    14-Sep-01 10:01
    suss14-Sep-01 10:01 
    QuestionExample of use in MFC ? Pin
    Le Chi Thu29-Aug-00 8:14
    sussLe Chi Thu29-Aug-00 8:14 
    AnswerRe: Example of use in MFC ? Pin
    Franky Braem30-Aug-00 0:09
    MemberFranky Braem30-Aug-00 0:09 
    GeneralConnection to other apps. Pin
    Arinté6-Jul-00 3:04
    sussArinté6-Jul-00 3:04 
    GeneralRe: Connection to other apps. Pin
    Franky Braem30-Aug-00 0:11
    suss Franky Braem30-Aug-00 0:11 
    GeneralRedirecting MS-DOS Pin
    Gisle Vanem7-Jun-00 22:52
    MemberGisle Vanem7-Jun-00 22:52 
    GeneralRe: Redirecting MS-DOS Pin
    Franky Braem8-Jun-00 22:24
    MemberFranky Braem8-Jun-00 22:24 
    GeneralRe: Redirecting MS-DOS Pin
    mohith17-Mar-02 2:53
    Membermohith17-Mar-02 2:53 
    GeneralRe: Redirecting MS-DOS Pin
    pravin 2516920712-May-08 21:09
    Memberpravin 2516920712-May-08 21:09 
    GeneralInconsistencies w/ Downloads Pin
    Patrick DellEra2-Jun-00 14:22
    MemberPatrick DellEra2-Jun-00 14:22 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.