Click here to Skip to main content
Licence 
First Posted 3 May 2000
Views 121,034
Bookmarked 36 times

Redirect

By | 7 Jun 2000 | Article
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.

    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 :

    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.

    License

    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

    Member

    Programmer since 1991 using C/C++/Visual Basic and Java. Playing with wxWindows at home.

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralClean Shutdown Pinmembersprice8622:58 26 Apr '10  
    GeneralProject request Pinmemberaryanabc20:02 29 Jun '09  
    GeneralCaptureConsole.DLL - A universal Console Output Redirector for all Compilers PinmemberElmue5:54 3 Feb '09  
    GeneralRe: CaptureConsole.DLL - A universal Console Output Redirector for all Compilers Pinmembersprice868:38 14 May '10  
    GeneralRe: CaptureConsole.DLL - A universal Console Output Redirector for all Compilers PinmvpElmue7:51 16 May '10  
    GeneralApologies Pinmembersprice863:35 17 May '10  
    GeneralRe: Apologies PinmvpElmue13:41 17 May '10  
    QuestionRun multiple cmd.exe sessions and send/receive input/output to differnet VB controls? Pinmembertfertodd18:42 14 Jun '08  
    GeneralGreat stuff Pinmembertnsbox14:57 30 Oct '07  
    This is just excellent!
    I've been trying to figure out the best way of
    parsing console output for days now. This helped
    me along the way. Thank you Smile | :)
    Questioncan we use the dll in c# Pinmemberaodeng9312:01 12 Dec '06  
    GeneralNot all stdout or stderr is printed Pinmemberjarrusin11:15 21 Nov '04  
    Generalreal time screen update PinmemberBertV3:39 4 Dec '03  
    GeneralRe: real time screen update PinmemberFranky Braem7:15 4 Dec '03  
    Generalusing control in vbscript or javascript Pinsussdavid hubbard9:40 4 Oct '00  
    GeneralRe: using control in vbscript or javascript PinsussFranky Braem0:57 5 Oct '00  
    GeneralRe: using control in vbscript or javascript PinmemberAnonymous10:01 14 Sep '01  
    QuestionExample of use in MFC ? PinsussLe Chi Thu8:14 29 Aug '00  
    AnswerRe: Example of use in MFC ? PinsussFranky Braem0:09 30 Aug '00  
    GeneralConnection to other apps. PinsussArinté3:04 6 Jul '00  
    GeneralRe: Connection to other apps. Pinsuss Franky Braem0:11 30 Aug '00  
    GeneralRedirecting MS-DOS PinsussGisle Vanem22:52 7 Jun '00  
    GeneralRe: Redirecting MS-DOS PinsussFranky Braem22:24 8 Jun '00  
    GeneralRe: Redirecting MS-DOS Pinmembermohith2:53 17 Mar '02  
    GeneralRe: Redirecting MS-DOS Pinmemberpravin 2516920721:09 12 May '08  
    GeneralInconsistencies w/ Downloads PinsussPatrick Dell'Era14:22 2 Jun '00  

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

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

    Permalink | Advertise | Privacy | Mobile
    Web03 | 2.5.120529.1 | Last Updated 8 Jun 2000
    Article Copyright 2000 by Franky Braem
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid