How to spawn console app from windows GUI program w/o creating temporary DOS command window

How to spawn console app from windows GUI program w/o creating temporary DOS command window

Post by David A.Le » Tue, 02 Aug 2005 02:52:14


I've got what I think is relatively common problem but haven't been
able to come up with a solution by searching.

I have a standard MFC program that needs to spawn a shell command-line
program that takes its arguements, completes a task, then exits.

The problem I have is that I want to suppress the temporary MS-DOS
command box that pops up once the program is spawned, then
automatically closes when the program exits.

I also need to be able to trap the error output and optionally display
any errors in a MessageBox, i.e, program_name <parameters> 2>
Results.txt

Not having any luck with using system(), or any of the _exec or _spawn
family functions, so need to look at

How does one run a process that won't create a window (at least a
visible one), but still have a mechanism to trap error ouput?
THanks
 
 
 

How to spawn console app from windows GUI program w/o creating temporary DOS command window

Post by William De » Tue, 02 Aug 2005 06:53:35


You have a couple of options. One is to redirect the standard input and
output devices for the image that you spawn to pipes that you created in
advance. You read from the "child's" output device to receive his output.
You write to his input device to force feed him input.

The technique is described here:

http://www.yqcomputer.com/

Another way to approach the problem is use CreateProcess() with the
CREATE_NO_WINDOW option. If you do this you won't be able to trap the output
but if the child exits by setting a status code you can retrieve it by
calling GetExitCodeProcess(). If the agreed upon code is a system error code
or one in a private message file you can get the text to go with the message
by calling FormatMessage().


See the pipe technique above.

<rant>
IMO, this is a hackneyed tired old technique. It's far better to invoke an
API to perform a task than to "shell out" and resort to what amounts to
screen scraping. YMMV.
</rant>

Regards,
Will