preferred method of spawning a DOS program

preferred method of spawning a DOS program

Post by Corey Wiru » Fri, 22 Oct 2004 05:14:46


Hi All,

Within the MFC environment, what is the preferred way to synchronously spawn
(i.e. execute and wait) a DOS console application (with an argv argument
list). Sure, I could use CRT's _spawn, but is there a more appropriate way?

Thanks in Advance!
C.
 
 
 

preferred method of spawning a DOS program

Post by Joseph M. » Fri, 22 Oct 2004 06:00:19

Generally you do not want to "wait" for a program to complete; what you want to do is run
the program and do something when it completes. See my essay on asynchronous process
notification on my MVP Tips site.

If you just "wait", then your program appears to lock up; the GUI is non-responsive. This
is usually considered tasteless.

The normal way of spawning a program is to use CreateProcess or ShellExecuteEx and get the
process handle. You then set some state that indicates that the process is running, and in
my model, spawn a thread that blocks on the handle. The menu item that would launch the
process is then disabled, e.g.,

void CWhatever::OnUpdateLaunchProcess(CCmdUI * pCmdUI)
{
pCmdUI->Enable(!ProcessIsRunning);
}

So you set ProcessIsRunning to TRUE when the you have successfully launched the process.

In my model, when the thread comes out of its wait, it does a PostMessage to the main GUI
thread. In the message handler for this user-defined message will set the ProcessIsRunning
flag to FALSE so you can launch another process. If you want to capture the output from
the process, you might want to look at my process class as well, which uses a pipe to
retrieve the stdout/stderr output (you can do whatever you want with it; in my case, I
added it to a ListBox so the user could see the compiler messages).
joe




Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.yqcomputer.com/
MVP Tips: http://www.yqcomputer.com/

 
 
 

preferred method of spawning a DOS program

Post by AliR » Fri, 22 Oct 2004 06:16:50

You would want to use CreateProcess to run the command and
WaitForSingleObject to wait for it to finish.

void RunAndWait(CString Command)
{
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};
si.cb = sizeof(STARTUPINFO);
si.lpReserved = NULL;
si.lpReserved2 = NULL;
si.cbReserved2 = 0;
si.lpDesktop = NULL;
si.dwFlags = 0;
CreateProcess( NULL,
Command,
NULL,
NULL,
TRUE,
NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&si,
&pi );
WaitForSingleObject( pi.hProcess, INFINITE );
}

AliR



spawn
way?
 
 
 

preferred method of spawning a DOS program

Post by Doug Harri » Fri, 22 Oct 2004 06:39:53


As you are not setting any STARTUPINFO values to non-zero, you can simplify
the setup to:

STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;

You don't need to initialize pi as it is for output only. In addition, you
need to check the return value of CreateProcess and close handles when
you're through with them:

if (CreateProcess(...))
{
// Use handles, and then...
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

Finally, you're not going to have much luck passing a CString as the second
argument to CreateProcess, as the parameter has type LPTSTR, and CString is
not convertible to that type. (Don't use a cast, or a string literal, as the
array has to be modifiable for Unicode builds.) In addition, be sure to read
the latest documentation on the first two parameters, as they interact with
one another in complicated, hard-to-remember ways, and you may need to quote
the executable filename and arguments.

--
Doug Harrison
Microsoft MVP - Visual C++
 
 
 

preferred method of spawning a DOS program

Post by AliR » Fri, 22 Oct 2004 06:47:38

Thanks Doug,

I pretty much cut and pasted the example out VC++ Help :)

AliR.




simplify
second
is
the
read
with
quote
 
 
 

preferred method of spawning a DOS program

Post by Corey Wiru » Sun, 24 Oct 2004 00:49:25

Thanks for your replies all! Good information.

Corey.



spawn
way?