Click here to Skip to main content
Click here to Skip to main content

Controlling console applications

By , 4 Feb 2002
 
<!-- Download Links --> <!-- Add the rest of your HTML here -->

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
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralCaptureConsole.DLL - A universal Console Output Redirector for all CompilersmemberElmue3-Feb-09 6:10 
GeneralLinewise reading datamembersmzhaq26-Sep-07 22:24 
QuestionHow to terminate earlymemberacjohnson557-Jul-06 9:25 
QuestionError storing m_hSaveStdout ?memberJohn P. Curtis25-Feb-06 18:56 
AnswerRe: Error storing m_hSaveStdout ?memberSaurweinAndreas26-Feb-06 4:20 
GeneralI fixed a bugmemberJunho Ryu27-Sep-05 2:17 
I have found a bug which can be duplicated when the child process has a delay between its stdout or stderr.
 
When there is a delay, the code checks whether the child process is still active by CSpawn::TestProcess. Because the function checks return value of GetExitCodeProcess instead of lpExitCode, CSpawn::ReadPipeThreadProc returns whether the child process is still running or not. Many people told about this before.
 
However, there was another problem.
Though the child process is already terminated, ReadFile does not return with the error code ERROR_BROKEN_PIPE. The reason is that the parent process also has both sides of handles of the pipes.
 
To fix the bug, close child side handles of pipes immediately after creating the child process. By doing it, we don't have to call PeekNamedPipe and GetExitCodeProcess. ReadFile will return an error. This is what Q190351[^] is doing.
 
And I think WaitForSingleObject (line 183) is unnecessary.
 
This is my spawn.cpp.[^]
GeneralRe: I fixed a bugmemberacjohnson557-Jul-06 9:22 
GeneralRe: I fixed a bugmemberjoeasnake21-Nov-07 15:32 
QuestionRe: I fixed a bugmemberrl7477-May-08 5:46 
GeneralInvalid Handle exception when creating several CSpawn classesmemberjohnpsquared1-Aug-04 13:26 
Questionmultiline output?membermikemurphy28-Feb-04 10:39 
AnswerRe: multiline output?memberjohnpsquared1-Aug-04 13:33 
Questionhow to get all stdout during batch command modesussAnonymous18-Dec-03 10:24 
GeneralVC++ Newbie Helpmembermessy17-Oct-03 12:05 
GeneralSmall bug fixmembermschoneman23-Sep-03 14:13 
GeneralInvoking &quot;.cmd&quot; filesmemberdshah13-Jun-03 3:41 
GeneralbRet = GetExitCodeProcess always truememberdonaldw25-Apr-03 12:36 
GeneralGetting stdout immediatelymemberMilhouse1006-Mar-03 0:49 
GeneralRe: Getting stdout immediatelymemberAndreas Saurwein6-Mar-03 1:19 
GeneralRe: Getting stdout immediatelysussAnonymous17-Mar-03 10:35 
GeneralSendInput Questionsuss6R15U28-Sep-02 23:47 
GeneralRe: SendInput QuestionmemberAndreas Saurwein4-Oct-02 0:24 
GeneralHelp! with CSpawn class...memberjohnpsquared1-Aug-04 13:29 
GeneralQuestion when using with processes that spawn other processesmemberJim Crafton18-Sep-02 9:40 
GeneralRe: Question when using with processes that spawn other processesmemberAndreas Saurwein23-Sep-02 1:06 
Generalnon-starter for win9x/MEsussumeca7416-Sep-02 10:46 
GeneralRe: non-starter for win9x/MEmemberAndreas Saurwein23-Sep-02 1:08 
GeneralRedirecting existing process IOmemberAnonymous22-Aug-02 4:15 
GeneralRe: Redirecting existing process IOsussAndreas Saurwein26-Aug-02 1:02 
GeneralWrong function!memberNguyen Binh5-Aug-02 23:44 
GeneralRe: Wrong function!memberAndreas Saurwein6-Aug-02 2:22 
GeneralRe: Wrong function!memberJim Crafton22-Aug-02 8:25 
General4k limitationmemberBed / Placid18-Jun-02 5:23 
GeneralRe: 4k limitationmemberAndreas Saurwein18-Jun-02 5:55 
Generalillegal memory access in Release config (Non-Unicode).memberBlade[DMS]4-Mar-02 6:54 
GeneralRe: illegal memory access in Release config (Non-Unicode).memberAndreas Saurwein4-Mar-02 12:01 
GeneralRe: illegal memory access in Release config (Non-Unicode).memberBlade[DMS]4-Mar-02 23:08 
GeneralVerry coolmemberJim Crafton8-Feb-02 8:44 
Generaldirecting cout/stdout to windowmemberN Singh6-Feb-02 12:00 
GeneralRe: directing cout/stdout to windowmemberAndreas Saurwein6-Feb-02 12:10 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 5 Feb 2002
Article Copyright 2002 by Andreas Saurwein Franci Gonçalves
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid