Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

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?

4 Answers

Relevance
  • doug
    Lv 5
    1 decade ago
    Favorite Answer

    system("someprogram.exe").

    http://www.cplusplus.com/reference/clibrary/cstdli...

    Visual Studio has a ShellExecute() function:

    http://msdn.microsoft.com/en-us/library/bb762153(V...

    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.

  • 1 decade ago

    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(), ...

  • 1 decade ago

    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.

  • 1 decade ago

    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_...

Still have questions? Get your answers by asking now.