Click here to Skip to main content
15,881,852 members
Articles / Desktop Programming / MFC

QuickWin - Turn a Console Application into a Windows Program

Rate me:
Please Sign up or sign in to vote.
4.83/5 (51 votes)
17 Oct 2000 537.6K   13.1K   230   78
Redirect stdin, stdout and stderr into a window
QuickWin is a very useful program to spawn script programs or console applications in a Windows environment without opening a DOS box. In this article, you will see how QuickWin works.

Sample Image

Introduction

QuickWin is a Windows application that spawns a Win32 console application that redirected its stdin, stdout and stderr handles to a window. The console application is hidden and all I/O operations are made through QuickWin.

This is a very useful program to spawn script programs or console applications in a Windows environment without opening a DOS box.

How QuickWin Works

QuickWin contains one or two windows depending on whether you want the same stdout view for stderr output or another view. stdin is in the same view as stdout. The view is a CRichEditView window and is not limited in space for the output. Commands like cut, copy, paste, find are also available. QuickView can spawn console applications, shell (COMMAND.COM or CMD.EXE) or batch files. You start the console application from the command line or browse your disk. You can also stop and restart the console application many times. The content of the output view can be saved to file. The last 10 programs are saved into the registry and available for quick start.

The following parameters are available:

Full or Half Duplex

In full duplex, characters are send to stdin and displayed into the view. In half duplex, they are only sent to stdin.

Enter

The enter key (CR) can be translated into CR, LF or CR+LF.

Close on Exit

If the console application is terminated, QuickWin will terminate.

Show Child Window

You can display the DOS box child window. If you want to write into this "Console", open the "CON" file and write into it.

OEM -> Ansi

Normally, we must translate characters for console application.

Use Input Buffer

If it is not necessary to send each character one at a time to the stdin. Characters can be stored in an input buffer until CR is pressed.

Command Line

For COMMAND.COM or CMD.EXE, the command is echoed to stdout. This option removes the typed command.

Options for COMMAND.COM or CMD.EXE.

Fullduplex, CR->CR+LF,Oem->ANSI, Use input buffer and Command line.

For standard applications, remove the Command line option.

Main Points of Interest in the Source Code

A CRedirect class that can be used in any WIN32 or MFC applications. This class contains four virtual functions for notification.

C++
virtual void OnChildStarted(LPCSTR lpszCmdLine);
virtual void OnChildStdOutWrite(LPCSTR lpszOutput);
virtual void OnChildStdErrWrite(LPCSTR lpszOutput);
virtual void OnChildTerminate();

The main program is an MFC application that uses the CRedirect class. PostThreadMessage is used to post commands in a multithread environment. WM_COPYDATA is used to post messages to the window.

The MFC print preview bug is not corrected!

Thanks to Paul DiLascia for some pieces of code from TraceWin.

History

  • 17th October, 2000: Initial post

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.


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: What about redirecting standard output of your own app? Pin
ralle7211-Oct-06 22:39
ralle7211-Oct-06 22:39 
Generalvery very helpful Pin
Anonymous11-Oct-04 1:33
Anonymous11-Oct-04 1:33 
Generalvery very helpful Pin
Anonymous11-Oct-04 1:32
Anonymous11-Oct-04 1:32 
Generalredirecting stdout without a new process Pin
S.Cartwright12-Sep-04 22:13
S.Cartwright12-Sep-04 22:13 
GeneralPlz help me Pin
toto56782-Jun-04 3:20
toto56782-Jun-04 3:20 
GeneralQuickwin.exe execution directory Pin
dmtudder30-Dec-03 10:30
dmtudder30-Dec-03 10:30 
QuestionHandle problem - Someone has a solution? Pin
fato17-Dec-03 6:01
fato17-Dec-03 6:01 
AnswerApplication fix Pin
fato29-Dec-03 0:13
fato29-Dec-03 0:13 
Found here a first shot of fixes to improve the code :

In redirect.cpp -> void CRedirect::TerminateChildProcess()
{
// Tell the threads to exit and wait for process thread to die.
m_bRunThread = FALSE;
::SetEvent(m_hExitEvent);
Sleep(500);

// Check the process thread.
if (m_hProcessThread != NULL)
{
VERIFY(::WaitForSingleObject(m_hProcessThread, 1000) != WAIT_TIMEOUT);
VERIFY(::CloseHandle(m_hProcessThread));
m_hProcessThread = NULL;
}

// Close all child handles first.
if (m_hStdIn != NULL)
VERIFY(::CloseHandle(m_hStdIn));
m_hStdIn = NULL;
if (m_hStdOut != NULL)
VERIFY(::CloseHandle(m_hStdOut));
m_hStdOut = NULL;
if (m_hStdErr != NULL)
VERIFY(::CloseHandle(m_hStdErr));
m_hStdErr = NULL;
Sleep(100);

// Close all parent handles.
if (m_hStdInWrite != NULL)
VERIFY(::CloseHandle(m_hStdInWrite));
m_hStdInWrite = NULL;
if (m_hStdOutRead != NULL)
VERIFY(::CloseHandle(m_hStdOutRead));
m_hStdOutRead = NULL;
if (m_hStdErrRead != NULL)
VERIFY(::CloseHandle(m_hStdErrRead));

m_hStdErrRead = NULL;
Sleep(100);

// Stop the stdout read thread.
if (m_hStdOutThread != NULL)
{
if (!::IsWinNT())
::TerminateThread(m_hStdOutThread, 1);
VERIFY(::WaitForSingleObject(m_hStdOutThread, 1000) != WAIT_TIMEOUT);
VERIFY(::CloseHandle(m_hStdOutThread));
m_hStdOutThread = NULL;
}

// Stop the stderr read thread.
if (m_hStdErrThread != NULL)
{
if (!::IsWinNT())
::TerminateThread(m_hStdErrThread, 1);
VERIFY(::WaitForSingleObject(m_hStdErrThread, 1000) != WAIT_TIMEOUT);
VERIFY(::CloseHandle(m_hStdErrThread));
m_hStdErrThread = NULL;
}
Sleep(100);

// Stop the child process if not already stopped.
// It's not the best solution, but it is a solution.
// On Win98 it may crash the system if the child process is the COMMAND.COM.
// The best way is to terminate the COMMAND.COM process with an "exit" command.

if (IsChildRunning())
{
VERIFY(::TerminateProcess(m_hChildProcess, 1));
VERIFY(::WaitForSingleObject(m_hChildProcess, 1000) != WAIT_TIMEOUT);
VERIFY(::CloseHandle(m_hChildProcess));
}

m_hChildProcess = NULL;



// cleanup the exit event
if (m_hExitEvent != NULL)
VERIFY(::CloseHandle(m_hExitEvent));
m_hExitEvent = NULL;
}

GeneralCompile Error under VS.NET 2003 on XP Pro Pin
[DAve]5-Dec-03 5:26
[DAve]5-Dec-03 5:26 
GeneralRe: Compile Error under VS.NET 2003 on XP Pro Pin
seclib6-Feb-04 4:44
seclib6-Feb-04 4:44 
GeneralRe: Compile Error under VS.NET 2003 on XP Pro Pin
[DAve]6-Feb-04 5:23
[DAve]6-Feb-04 5:23 
GeneralRe: Compile Error under VS.NET 2003 on XP Pro Pin
sars3-Apr-05 15:18
sars3-Apr-05 15:18 
GeneralRe: Compile Error under VS.NET 2003 on XP Pro Pin
Bobby Kolev4-Nov-05 5:07
Bobby Kolev4-Nov-05 5:07 
Generaldisplaying calculation reports Pin
RAIMONDI LUIGI26-Nov-03 22:00
RAIMONDI LUIGI26-Nov-03 22:00 
Generalvery well, thanx Pin
pureman10-Nov-03 19:13
pureman10-Nov-03 19:13 
GeneralVery nice and usefull but... Pin
J.R.4-Oct-03 21:10
J.R.4-Oct-03 21:10 
GeneralStream output does not work under Visual C++ .Net Pin
ASBR13-Mar-03 7:37
ASBR13-Mar-03 7:37 
GeneralRe: Stream output does not work under Visual C++ .Net Pin
Airpatrol24-Apr-04 18:50
Airpatrol24-Apr-04 18:50 
GeneralSorry, there already had a response Pin
Pacino9416-Jan-03 21:14
Pacino9416-Jan-03 21:14 
Generalno all the stdout is printed Pin
Pacino9413-Jan-03 23:24
Pacino9413-Jan-03 23:24 
GeneralCatch Main Messages Pin
BtL2712-Jan-03 6:04
BtL2712-Jan-03 6:04 
GeneralStdin and Stdout in different Windows Pin
Bert Tuyt4-Aug-02 0:52
Bert Tuyt4-Aug-02 0:52 
GeneralIdea: Windrop service Pin
Kastellanos Nikos1-Aug-02 21:52
Kastellanos Nikos1-Aug-02 21:52 
Generalcapture console text not assured Pin
Steven Szelei4-Jul-02 11:43
Steven Szelei4-Jul-02 11:43 
GeneralRe: capture console text not assured Pin
Anonymous15-Nov-02 23:46
Anonymous15-Nov-02 23:46 

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.