user98188
user98188

Reputation:

How to create/run a .exe from a program? (C++)

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?

Upvotes: 0

Views: 12543

Answers (8)

ndrewxie
ndrewxie

Reputation: 172

A few things to address:

1) Never use system(). It's evil (not platform independent, security bug, etc). system() assumes that the target computer has a command interpreter, which it might not have!

2) If you want to use system, do this (assuming you have a vector of arguments called args, the process to be called is in a string called proc)

string final = proc;
final += " ";
for (int j = 0; j < args.size(); j++) {
  final += args[j];
  final += " ";    
}
system(final.c_str());

You could compile by assuming that the target PC has a compiler (once again, unsafe), and invoking it (eg g++ source.cpp).

3) use pipes. Code sample:

inline std::string exec_proc(const char* cmd) {
    std::array<char, 128> buffer;
    std::string result;
    std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
    if (!pipe) cout << "Launch failed, possible permission error" << endl;
    while (!feof(pipe.get())) {
        if (fgets(buffer.data(), 128, pipe.get()) != NULL)
            result += buffer.data();
    }
    return result;
}

Upvotes: 0

Trillian
Trillian

Reputation: 6447

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

Upvotes: 0

bdonlan
bdonlan

Reputation: 231443

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
}

Upvotes: 2

swongu
swongu

Reputation: 2299

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.

Upvotes: 0

singinanarchist
singinanarchist

Reputation:

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)

}

Upvotes: 0

Brian R. Bondy
Brian R. Bondy

Reputation: 347566

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.

Upvotes: 4

cort
cort

Reputation: 1126

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

Upvotes: 0

lothar
lothar

Reputation: 20257

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.

Upvotes: 1

Related Questions