views:

1407

answers:

7

Is it possible (and if so, how) to write a program in C++ that takes parameters, then run that program from another C++ program.

Ie: I can create a function that adds two numbers:

int add(int a,int b){return a+b;}

Is it possible to do the same thing, except instead of a function, a separate C++ .exe?

EDIT: Since a lot of people don't understand my question, I'll try to say exactly what I want to know how to do.

I want to have a program, lets call it "p1".

I want that program to get two numbers from the user:

int x,y;
cin>>x;
cin>>y;

Now I want to create another program, that takes two numbers, and adds them, the same way a function would, except a seperate program.

Then I want to run the second program from the first program with the numbers it got from the user. Ie:

#include<iostream>
using namespace std;
int main()
{
     int x,y;
     cin>>x;
     cin>>y;
     add(x,y); //this is how I would call a function named "add".  I want to know how to do that with a separate program instead of just a separate function.
     return 0;
}

EDIT: I figured out how to use

(void)system("C:\\Program Files\\test.exe");

How would I use that to pass arguments, and how could I write a program that takes those arguments?

+1  A: 

Your quesion is a bit unclear.

Are you by any chance looking for something like the system call? http://en.wikipedia.org/wiki/System_(C_standard_library)

With system you can call any executable and pass any number of command line arguments to that executable.

lothar
A: 

Yep, there're several ways like sockets, pipes, rpc... http://en.wikipedia.org/wiki/Inter-process_communication

ktulur
+4  A: 

I'm not exactly sure what you want to do, but you can start apps in windows several ways. The most common "windows" way to do it is:

  • Use ShellExecute to start a program in windows.
  • If you need to get the return value back, you can use CreateProcess instead.

It sounds like you want to take the argc, and argv values from the source application, and then use those values as parameters into the above functions to start your second application.

You can communicate between the processes in several ways, by using pipes, sockets, files, or even the 2nd program's return code might be enough.

You might be better off creating a DLL to load from your first application. The DLL would do whatever you want and can return the values to you directly without an extra communications layer.

Brian R. Bondy
Note: My answer is specific to windows because the poster was asking about .exe
Brian R. Bondy
A: 

Not sure I understand the question but you can add parameters to a C/C++ program using :

int main (int argc, char *argv[])
{
   if (argc == 1)

}
A: 

Yes, singinanarchist was on the right track. A C++ console application begins with the call main( int argc, char* argv[] ), which accepts two arguments:

  • argc = argument count: number of input arguments
  • argv = argument vector: char* array of arguments

So, if you compile your program as prog.exe, and wish to run it as

prog 12 23

Then argc is 3, and the argument vector argv is equivalent to

argv[0] = "prog" ; // (or "prog.exe")
argv[1] = "12" ;
argv[2] = "23" ;

In which you can perform what you'd like to the input arguments.

swongu
So then how could I run this program from another?
Keand64
+1  A: 

On most OSes, you can pass arguments in system() by space-seperating them:

system("someapp.exe 42 24");

This string, of course, can be built up by sprintf or std::ostringstream or what have you.

Then you just need to process your arguments in the other program:

int main(int argc, char **argv) {
  // important - make sure we have enough args
  if (argc != 3) { // first (zeroth) argument is always the program name, so 1+2 args is needed
    fprintf(stderr, "Wrong number of arguments\n");
    return EXIT_FAILURE;
  }
  int a = atoi(argv[1]);
  int b = atoi(argv[2]);
  // do stuff
}
bdonlan
And I'm assuming that is I wrote int n=system("someapp.exe 42 24"); It would make n the return value of someapp?
Keand64
Depends on the OS. I'm not familiar with windows' handling. In particular, this is NOT the case on Unix-like operating systems such as Linux (on those OSes, WEXITSTATUS(return_from_system) gets the exit code)
bdonlan
A: 

This is a windows-specific solution but you might be able to invoke a function that's been defined in another executable. That's usually the task of DLLs, but EXEs and DLLs are both the same format anyways (there's a few different flags, but they're both in Portable Executable (PE) format), so it should work. You'd have to do something like this:

caller.cpp:

#include <windows.h>

// [...]

HMODULE moduleHandle = LoadLibrary("callee.exe"); // or .dll
int(*add)(int, int) = (int(*)(int, int))GetProcAddress(moduleHandle, "add");
int result = add(1, 1);

callee.h:

// Not sure if dllexport will work for EXEs
__declspec(dllexport) int add(int a, int b);

callee.cpp:

#include "callee.h"

int add(int a, int b) { return a + b; }

Disclaimer:

  • Untested (both syntax-wise and logic-wise)
  • Does no error checking/resource freeing

References:
LoadLibrary
GetProcAddress

Trillian