Click here to Skip to main content
15,891,204 members
Articles / Desktop Programming / MFC

Controlling console applications

Rate me:
Please Sign up or sign in to vote.
4.17/5 (9 votes)
4 Feb 2002Ms-PL2 min read 173.2K   3.1K   65  
Run console applications and controll/use their input/output streams
//
// (c) 2002 Andreas Saurwein - saurwein@uniwares.com
//
// Use as you like, dont claim ownership of this code until
// you extended it sufficiently. Mention my name when you like to.
// Spawn.h: interface for the CSpawn class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(_SPAWN_H_INCLUDED_)
#define _SPAWN_H_INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef NO_STL
#include <string>

#if defined(_UNICODE) || defined(UNICODE)
using std::wstring;
typedef wstring	tstring;
#else
using std::string;
typedef string	tstring;
#endif
#endif // NO_STL

class CSpawnConsumer
{
public:
	CSpawnConsumer() {};
	virtual ~CSpawnConsumer() {};
private:
	//virtual Consume(tstring& output) = 0;
	virtual void Consume(TCHAR*	output, DWORD dwChars) = 0;
	friend class CSpawn;
};

class CSpawn  
{
public:
	CSpawn();
	CSpawn(CString& exe, CSpawnConsumer* sc);

	virtual ~CSpawn();
 
	bool Execute(CSpawnConsumer* sc);
	bool Execute(CString& exe, CSpawnConsumer* sc)	{ m_szExecutable = exe; return Execute(sc);}
	bool IsExecuting() { return TestProcess(); }
	void SendInput(LPCTSTR pIn)	{ WriteToPipe(pIn); }
	void SendInput(CString& In) { WriteToPipe(LPCTSTR(In));	}
#ifndef NO_STL
	void SendInput(tstring& In) { WriteToPipe(In.c_str());	}
#endif
	
protected:
	bool CreateChildProcess(); 
	void WriteToPipe(LPCTSTR line); 
	void ReadFromPipe(); 
	void ErrMsg(LPTSTR, bool); 
	void TestAndCleanProcess();
	bool TestProcess();
	
	void Redirect();
 
private:
	void	Init();
	static UINT ReadPipeThreadProc(LPVOID pParam);
	
	PROCESS_INFORMATION m_piProcInfo; 
	STARTUPINFO			m_siStartInfo; 
	
	HANDLE	m_hChildStdinRd, 
			m_hChildStdinWr, 
			m_hChildStdinWrDup, 
			m_hChildStdoutRd, 
			m_hChildStdoutWr, 
			m_hChildStdoutRdDup, 
			m_hInputFile, 
			m_hSaveStdin, 
			m_hSaveStdout; 

	bool	m_bRedirected;

	CString	m_szExecutable;
	CWinThread*	m_pReadThread;
	DWORD	m_dwProcessId;
	
	CSpawnConsumer* m_Consumer;
};

#endif // !defined(_SPAWN_H_INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
Portugal Portugal
Software Smith, Blacksmith, Repeat Founder, Austrian, Asgardian.

Comments and Discussions