You are here:

C++/C++ calling programs within programs

Advertisement


Question
"Hello I am a 1st year learner of C++ and I have been using Visual Studio 2005/2008 to compile all of my code. My question is this, I have all my programs (labs, projects etc...) and I would like to create a program that can call the other programs.

Basically I would like to use a switch to allow the user to select the program they would like to use and through there input calling  the other programs.

Is there a special command I can use to do this in Windows? Do I have to worry about the data files the other programs might need?

Thanks

Answer
-------------------------------------------------------------------------------------------------

Apologies - I meant _chdir not _mkdir in the next to last paragraph
(see http://msdn.microsoft.com/library/bf7fwze1.aspx for MSDN Library MSVC++ 2008 documentation).
You might also like to check out _chdrive
(see http://msdn.microsoft.com/library/0d1409hb.aspx)

-------------------------------------------------------------------------------------------------


I suggest you start by looking at the following MSVC++ C Run Time (CRT) library functions:

   system (ISO C/C++)
   _spawn family (MSVC++ CRT)
   _exec family ((MSVC++ CRT)

The system function takes a C-style string and executes it using a command processor (CMD.EXE), causing the calling program to wait until the command completes. This is the only function provided by the ISO standard C and C++ libraries for doing this sort of thing.

The _spawn family of functions also execute an executable file, as a new process. The differences in the various _spawn functions relate to the parameters - how arguments are passed, if an environment is passed, and whether to search using the PATH environment variable or not. The _spawn functions operate in one of several modes that determine how the new and existing processes interact:

   - the new process overlays the existing, destroying the calling process
   - the calling program (in fact the calling thread) is suspended until the new process completes - somewhat like system.
   - the new program executes independently of the calling program, with the _spawn function returning almost immediately.
       The new process can either be attached to the console and keyboard or detached with no access
       (running in the background).

The _exec family of functions work like _spawn in suspend calling thread mode.

For information on these functions check out the C runtime library documentation for MSVC++ 2008 or use the online version of the MSDN library at:

   http://msdn.microsoft.com/library/default.aspx

I have not mentioned native Win32/Win64 functions yet. If you wish to use native Win32/Win64 then try looking up: CreateProcess, CreateProcessAsUser, CreateProcessWithLogonW, SHCreateProcessAsUserW, ShellExecute and ShellExecuteEx for starters in the MSDN library.

As to whether you have to worry about data files - well that depends on how your programs locate them. If they assume the data files are in (or relative to) the current directory then you will have to change to that directory before executing the program - check out the _chdir CRT library function. If they take such information as command line parameters then ensure they are passed the correct information. If they use some other scheme - well ensure it is set up correctly before executing the program.

Hope this is of use and moves you forward.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.