Combinding c++ pages

I was wondering how to run a C++ exe from another. Or how do you "call" I guess is the term from C++ program from another. Kind of like a function maybe? Just a quick question, any HELPFUL answer is appreciated.
Mentioning "exe" it would mean I guess a dependence of a platform, Windows platform to be more specific. If you're interested in doing something dependent on Windows, please address the question on the "Windows Programming" side of forum:
http://www.cplusplus.com/forum/windows
As for your question, the simplest way is to make a call to system:

system("d:\dir\program.exe");

where d: and dir as you might figured out are the drive and the directory path which may be relative or absolute. This way of starting another executable would imply opening a command prompt till the opened executable closes.
A more elaborate solution is MFC's Process and Thread Functions. What you want is the CreateProcess:
http://msdn.microsoft.com/en-us/library/ms682425(v=VS.85).aspx
A few issues:

- In C++ the backslash is an escape character in strings so you need to double up on them like this
system("d:\\dir\\program.exe");

- You should include "cstdlib" when using the "system(...)" function. Some compilers will let you use this without the library but that's not good practice.

- You won't get any data back directly from the "system(...)" command. So for that reason I suggest using the platform API commands instead.
Last edited on
To be honest, this sounds like a job for a DLL, or an included cpp/h file or pretty much ANYTHING other than trying to run a program like a function.

Also, can't you use START with system() on NT?
You could but it would be redundent. WINNT sees the ".exe" extension and assums that it's an executable so it runs it with it's own shell anyway. It would be the same as using START PROGRAM.EXE from the command line compared to just typing PROGRAM.EXE
It's still good that you can, there are a lot of advanced options in START that would require some real hikery-pokery to sort out with Win32 or whatever.
Topic archived. No new replies allowed.