Click here to Skip to main content
Licence Ms-PL
First Posted 4 Feb 2002
Views 138,489
Bookmarked 62 times

Controlling console applications

Run console applications and controll/use their input/output streams

Console windows and application output

During a "simple" project I discovered the tedious task of capturing the output of a console window into a control of the application. The additional difficulty was to hide the console window, thus hiding the application.

As it was to be expected, things were not as easy as redirecting output into a file and reading that file back in.

So I started creating a wrapper class for this task and it proved harder than it seemed at first. The provided sample in the MSDN did not mention the most important things and leaves more questions then it solves.

Searching around on various code sites shed some more light on the topic. Finally I could assemble a working solution. (Although there are some minor wishes left)

The CSpawn Class

This class has a pretty simple interface. A constructor, an execute function, an output function and finally a test to see if it is still active. The execute function and the output function are available in various flavours.

For the feedback you must derive a class from CSpawnConsumer and overload the Consume() function.

Usually it looks like this:

class CSpawnConsumer1 : public CSpawnConsumer
{
public:
    explicit CSpawnConsumer1(CMyEdit* pEdit)
                                   : m_pEdit(pEdit){}
    void Consume(TCHAR* p, DWORD dw)
    {
        m_pEdit->DoWhateverWithTheInput(p);
    }

private:
    CMyEdit* m_pEdit;
};

This class is called from the thread reading the output of the console window.

You can use the CSpawn class in two ways:

  1. Using the constructor method CSpawn::CSpawn(CString& exe, CSpawnConsumer* sc)
  2. Or creating a CSpawn variable and calling Execute(CString& exe, CSpawnConsumer* sc)

The default implementation will spawn the executable by resolving the ComSpec environment variable (normally CMD.EXE or COMMAND.EXE) and passing parameters to the given application.

You can provide user input to a running application by calling SendInput().

The Sample

The included sample project shows a simple edit control where any text entered is passed to the standard command interpreter and returns the results into the same edit control.

It shows how to interface with the CSpawn class in the most common way.

Compatibility

CSpawn was tested under Windows 2000 and Windows XP in Unicode and MBCS (both included in demo project) using Visual C++ 6.0 SP5 and MFC 6. Its not tested with .NET and MFC 7.

Revision History

19 Jun 2002 - Initial Revision

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Andreas Saurwein Franci Gonçalves

CEO
Uniwares Ltda.
Brazil Brazil

Member



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
GeneralCaptureConsole.DLL - A universal Console Output Redirector for all Compilers PinmemberElmue6:10 3 Feb '09  
GeneralLinewise reading data Pinmembersmzhaq22:24 26 Sep '07  
QuestionHow to terminate early Pinmemberacjohnson559:25 7 Jul '06  
QuestionError storing m_hSaveStdout ? PinmemberJohn P. Curtis18:56 25 Feb '06  
AnswerRe: Error storing m_hSaveStdout ? PinmemberSaurweinAndreas4:20 26 Feb '06  
GeneralI fixed a bug PinmemberJunho Ryu2:17 27 Sep '05  
GeneralRe: I fixed a bug Pinmemberacjohnson559:22 7 Jul '06  
GeneralRe: I fixed a bug Pinmemberjoeasnake15:32 21 Nov '07  
QuestionRe: I fixed a bug Pinmemberrl7475:46 7 May '08  
GeneralInvalid Handle exception when creating several CSpawn classes Pinmemberjohnpsquared13:26 1 Aug '04  
Questionmultiline output? Pinmembermikemurphy10:39 28 Feb '04  
AnswerRe: multiline output? Pinmemberjohnpsquared13:33 1 Aug '04  
Questionhow to get all stdout during batch command mode PinsussAnonymous10:24 18 Dec '03  
GeneralVC++ Newbie Help Pinmembermessy12:05 17 Oct '03  
GeneralSmall bug fix Pinmembermschoneman14:13 23 Sep '03  
GeneralInvoking ".cmd" files Pinmemberdshah3:41 13 Jun '03  
GeneralbRet = GetExitCodeProcess always true Pinmemberdonaldw12:36 25 Apr '03  
GeneralGetting stdout immediately PinmemberMilhouse1000:49 6 Mar '03  
GeneralRe: Getting stdout immediately PinmemberAndreas Saurwein1:19 6 Mar '03  
GeneralRe: Getting stdout immediately PinsussAnonymous10:35 17 Mar '03  
GeneralSendInput Question Pinsuss6R15U23:47 28 Sep '02  
GeneralRe: SendInput Question PinmemberAndreas Saurwein0:24 4 Oct '02  
GeneralHelp! with CSpawn class... Pinmemberjohnpsquared13:29 1 Aug '04  
GeneralQuestion when using with processes that spawn other processes PinmemberJim Crafton9:40 18 Sep '02  
GeneralRe: Question when using with processes that spawn other processes PinmemberAndreas Saurwein1:06 23 Sep '02  

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
Web04 | 2.5.120517.1 | Last Updated 5 Feb 2002
Article Copyright 2002 by Andreas Saurwein Franci Gonçalves
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid