MadSci Network: Computer Science
Query:

Re: How do you call a program in C++?

Date: Wed Dec 13 19:22:20 2000
Posted By: Mike Westerfield, Staff, Computer Science, Byte Works, Inc.
Area of science: Computer Science
ID: 976495668.Cs
Message:

This isn’t a simple question. I’ll start with some background so you can see 
the real problem, then follow with some concrete suggestions.

C++, like most languages, is rather myopic. The language doesn’t know there 
is a real world out there. It has no idea what operating system it is 
running under, whether there are any other programs, or what I/O devices are 
available. The libraries that come with the language make some assumptions. 
They assume, for example, that there is some way to send text somewhere and 
some way to get text from the outside world. In point of fact, though, some 
useful C++ programs run in an environment where these assumptions are not 
valid.

You’re probably starting to see why you can’t find information about how to 
run an application in C++ books. The simple answer is that C++ can’t do it, 
at least not directly. C++ relies on libraries that may or may not exist in 
a particular environment to get the job done.

The only facility that I’m aware of that comes close to being available in 
all implementations of C++ is the system() function. This function takes a 
string and passes it to a command processor. There are a lot of potential 
problems with system(), though. There is no guarantee that a command 
processor exists in a particular implementation of C++. Even if there is 
something on the other end listening to the commands you send, it may not be 
capable of launching an application. I didn’t have any luck using system() 
to launch an application on Windows 95 using Microsoft VC++, for example. 
You can find the system() function documented in any decent book on C or C++
. My favorite is C, A Reference Manual, Harbison & Steele, Tartan 
Laboratories. It's a C book, not C++, but it does describe system().

What you’ll probably end up using is a library that is specific to the 
operating system you’re using instead of something that is in every 
implementation of C++. As an example, you will probably use ShellExecute if 
your application runs on Windows. You might pick WinExec or CreateProcess 
instead, though. If you’re running on some other operating system, you’ll 
use some other OS level call, but it’s impossible to name it without knowing 
the specific OS you are using.

Assuming for a moment that you’re running under some flavor of Windows, 
though, start by looking for ShellExecute in your online documentation. If 
that’s unavailable, or you aren’t finding what you need, try 
www.microsoft.com. A search for ShellExecute will turn up dozens of 
listings, from documentation of the command itself to several programming 
examples in a variety of languages.

Mike Westerfield
Byte Works, Inc.



Current Queue | Current Queue for Computer Science | Computer Science archives

Try the links in the MadSci Library for more information on Computer Science.



MadSci Home | Information | Search | Random Knowledge Generator | MadSci Archives | Mad Library | MAD Labs | MAD FAQs | Ask a ? | Join Us! | Help Support MadSci


MadSci Network, webadmin@www.madsci.org
© 1995-2000. All rights reserved.