silly c++ question but...?

This is probably so simple that nobody bothered putting it in the tutorial, but how do you call one c++ program from inside of another c++ program. So inside the main of one program i'm in I want to call the main from another program that I have and store the return value from it.

so if I have programA and programB which accepts two strings can I just say:
result=programB(string1,string2);
from inside programA?

doug2010-03-18T09:17:18Z

Favorite Answer

system("someprogram.exe").

http://www.cplusplus.com/reference/clibrary/cstdlib/system/

Visual Studio has a ShellExecute() function:

http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx

As for the return value, I think you may be limited in this regard.

There are other ways. Since the program you are calling is your own, you can have it write a value to a text file and then having the calling program read it. And there are 100 other ways to do this.

ʃοχειλ2010-03-18T16:24:22Z

It is not possible to call the main function of another C/C++ program. You should either use DLL code (the main function of your program calls a function in DLL library), or you should consider inter-process communication such as TCP or UDP (your program as a client communicates with a server).

The simpler alternative is to start another program from your program and to examine its exit code which is actually the return value of main() function. The exact function name depends on the compiler you are using. Function names are mormally one of exec(), spawn(), start(), ...

timba2010-03-18T16:10:13Z

im not sure but i don't think you can do what you explained, i think the only way would be to run program B as a separate process from program A and have the result of your function written to a file and then from program B access the file and get the result.

bernmeister2010-03-18T16:05:58Z

Here's what I used to do in C years ago. Should get you going at least.

http://en.wikipedia.org/wiki/System_%28C_standard_library%29