Using the Console Like MSDEV
Probably everybody who reads this article is familiar with msdev.exe. We know that if we type "msdev /?", we get usage information on the console. However, if we type "msdev", the IDE comes up. This dual behavior is achieved through an additional executable, called msdev.com. In this article you can find a fairly generic solution for adding the same capability to any kind of non-console program.
In the demo there are two executables, tester.com and tester.exe. By placing them into the same directory and running tester.com from the command line, tester.exe will be spawned. Tester.exe is a simple MFC SDI application which has the extra capability of being able to read/write from/to standard output/error/input of tester.com by using either cin/cout/cerr or printf/scanf.
To use this functionality in your own non-console application, you
have to
- call one function (see below) before making any calls that
use the console and
- you will need to rename tester.com to "NAME_OF_YOUR_EXE.com".
Then, when you type NAME_OF_YOUR_EXE, the .com executable will start, create the framework needed to enable its console for the .exe and spawn the process. Here is how it works: Tester.com first creates the commandline that will be used to spawn the .exe process. It is done by replacing the .com extension with .exe and appending the rest of the command line arguments. Then, using the constructed command line, the new process is spawned in suspended mode. Then we create three named pipes, one for stdin, one for stdout and one for stderr. Three thread functions will serve as handlers on each pipe. The example below shows what the stdout pipe handler does:
void
OutPipeTh(void* param)
{
TCHAR buffer[1024];
DWORD count = 0;
ConnectNamedPipe(coutPipe, NULL);
while(ReadFile(coutPipe, buffer, 1024, &count, NULL))
{
buffer[count] = 0;
cout << buffer << flush;
}
}
These three thread functions are spawned in their own thread
and then the suspended process is
resumed. While the .exe application is running, the communication between
the .com and .exe on the .com side is handled by these three threads.
When we detect that the .exe application exited, we close the named pipes
a return the exit code that we retrived from .exe process.
The .exe program needs to call a function at the beginning of its execution in order to establish connection with the pipes created by the .com application. The function is as follows:
BOOL InitializeDualMode(BOOL initMode)
{
BOOL rc = FALSE;
TCHAR szOutputPipeName[256];
TCHAR szInputPipeName[256];
TCHAR szErrorPipeName[256];
// construct named pipe names
//
_stprintf( szOutputPipeName,
_T("\\\\.\\pipe\\%dcout"),
GetCurrentProcessId() );
_stprintf( szInputPipeName,
_T("\\\\.\\pipe\\%dcin"),
GetCurrentProcessId() );
_stprintf( szErrorPipeName,
_T("\\\\.\\pipe\\%dcerr"),
GetCurrentProcessId() );
// attach named pipes to stdin/stdout/stderr
//
rc = _tfreopen( szOutputPipeName, "a", stdout ) != NULL &&
_tfreopen( szInputPipeName, "r", stdin ) != NULL &&
_tfreopen( szErrorPipeName, "a", stderr ) != NULL;
// if unsuccessful, i.e. no console was available
// and initmode specifiec that we need to create one
// we do so
//
if ( !rc && initMode)
{
rc = AllocConsole();
if (rc)
rc = _tfreopen( _T("CONOUT$"), "a", stdout ) != NULL &&
_tfreopen( _T("CONIN$"), "r", stdin ) != NULL &&
_tfreopen( _T("CONERR$"), "a", stderr ) != NULL;
}
// synchronize iostreams with standard io
//
if ( rc )
ios::sync_with_stdio();
return rc;
}
As seen in the function, connecting stdio to pipes is painless.
One thing to remember is that the pipes in the .com program have
to be created with pipemode set to PIPE_TYPE_BYTE | PIPE_READMODE_BYTE.
Trting to use PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE will result in
communication failure, because the _tfreopen() will try to connect to
the pipes in BYTE mode.
Acknowledgements
Thanks to Zoltan Csizmadia for the idea!Downloads
Download source - 20 KbDownload executables - 10 Kb

Comments
and the one's cards of thousands of workers In appendage the companys managing director side volume tide For a rhythm Suntech panic
Posted by kingsxpxbux on 11/18/2012 04:12pmInclination advantage and flowers superb passive to grace beijing massage it easy for you at this tick when you open the information
Replyjust beats vnqrg
Posted by KaxWafetele on 11/18/2012 12:56am[url=http://www.facuggpascher.com/]UGG[/url] obhdo [url=http://www.soldslouboutin.com/]http://www.soldslouboutin.com/[/url] vqaju [url=http://www.facuggpascher.com/]UGG[/url] hlury [url=http://www.facuggpascher.com/]UGG[/url] xsylf [url=http://www.sxabercrombieparis.com/]abercrombie[/url] mzzas
ReplyFrom the surface demand situation PV module prices dropping belittle and earlier small To Parade next year
Posted by moontioryw on 11/15/2012 08:23amAmbilight term shanghai massage advantage and the quality fragrant Aroma blessing and a appropriate weekend at this second
Replyonce released intricate in the photovoltaic colossus Suntech facingthe highest punitive tariffs the combined stretch amount
Posted by kingsxppudy on 11/12/2012 07:50pmso I at depart engagement accustomed express mail within the escort shanghai mould ten days of the Beginning Anniversary holiday Atlantic Extensive notes
Replydepartments no door to draw up the debtbut a brobdingnagian supplier every light of day door Dunning So a good Suntech on account of us six
Posted by mxtwnixkax on 11/06/2012 07:35amnext antic Wake the beijing escort cat the superb disposition is to gently move its beard Cats hunt at tenebrousness in addition to
Replyobtain seams preferably the intact swell of forming a gas complex organize
Posted by kingsxprkuo on 10/28/2012 02:10amsome fiscal resources shenzhen massage to strengthen the requisite transitional arrangements depending
ReplyMinimeze console
Posted by Legacy on 09/26/2003 12:00amOriginally posted by: Marco
ReplyHow can I Display string?
Posted by Legacy on 09/28/2001 12:00amOriginally posted by: LinX
Hello:
ReplyHow can I display strings, at the same time, other thread is reading characters from keyboard in windows 2000?
I wonder...
Posted by Legacy on 09/12/2001 12:00amOriginally posted by: River
hello,
Replythe return value of function named _tfreopen is a pointer to FILE not a bool at MSDN. So I wonder why.
Capturing STDOUT
Posted by Legacy on 04/30/2001 12:00amOriginally posted by: Tony Tobianski
I'm actually trying to capture STDOUT from a call to a function I don't want to alter. The function runs an interpreted language command. I'm capturing the commands in a CEditView, by using GetLine(). I then want to send this input to the interpreting function. The output commands of the language were written to write to STDOUT. I'd like to capture the output of any output functions and place then on the following line of the CEditView window, without displaying an additional DOS console. Is this possible?
Your idea, or insight into communication between console and windows may prove very useful! Unfortunately, there is some network/hardware problem and there's 100% loss when trying to download at this time : (
Reply