Reputation:
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
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
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:
References:
LoadLibrary
GetProcAddress
Upvotes: 0
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
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 argumentsargv
= argument vector: char*
array of argumentsSo, 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
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
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:
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
Reputation: 1126
Yep, there're several ways like sockets, pipes, rpc... http://en.wikipedia.org/wiki/Inter-process_communication
Upvotes: 0
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