How to create / run an .exe from a program? (C ++)
Is it possible (and if so, how) to write a C ++ program that takes parameters and 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 other than instead of a function, a separate C ++. Exe?
EDIT: Since a lot of people don't understand my question, I'll try to tell you exactly what I want to know how to do.
I want to have a program, name it "p1".
I want this program to receive 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 just like a function, apart from a separate program.
Then I want to run the second program from the first program with the numbers obtained from the user. I.e:
#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 can I use this to pass arguments, and how can I write a program that takes those arguments?
On most operating systems, you can pass arguments to system () by separating them:
system("someapp.exe 42 24");
This string can of course be created by sprintf or std :: ostringstream or whatever you have.
Then you just need to process your arguments in another 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
}
source to share
I'm not really sure what you want to do, but you can launch applications on Windows in several ways. The most common "windows" way:
- Use ShellExecute to run the program in windows.
- If you need to return a return value, you can use CreateProcess instead .
It looks like you want to take the argc and argv values from the original application, and then use those values as parameters in the above functions to start the second application.
You can communicate between processes in several ways, using pipes, sockets, files, or even the return code of a second program.
You might be better off creating a DLL to load from your first application. The DLL will do what you want and can return values directly without an additional layer of communication.
source to share
Your question is a little unclear.
Can you accidentally find something like a system call? http://en.wikipedia.org/wiki/System_(C_standard_library)
On the system, you can call any executable and pass any number of command line arguments to that executable.
source to share
Yes, there are several ways like sockets, pipes, rpc ... http://en.wikipedia.org/wiki/Inter-process_communication
source to share
Yes, the singulararchist was on the right track. A C ++ console application starts with a call main( int argc, char* argv[] )
that takes two arguments:
-
argc
= number of arguments: number of input arguments -
argv
= vector argument:char*
array of arguments
So if you compile your program like prog.exe
and want to run it like
prog 12 23
Then argc
is equal to 3, and the vector of the argument is argv
equivalent to
argv[0] = "prog" ; // (or "prog.exe")
argv[1] = "12" ;
argv[2] = "23" ;
Where you can do what you want with the input arguments.
source to share
This is a special solution for Windows, but you can call a function defined in another executable. Usually a DLL task, but EXE and DLL are the same anyway (there are a few different flags, but they are both in Portable Executable (PE) format), so it should work. You will need 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; }
Denial of responsibility:
- Untested (both syntactic and boolean)
- Doesn't check for errors / free resources
Literature:
LoadLibrary
GetProcAddress
source to share
A few questions:
1) Never use system (). This is evil (not platform independent, security bug, etc.). system () assumes that the target computer has a shell that it may not have!
2) If you want to use the system, do this (if you have a vector of arguments called args, the process to be called is on a line named proc)
string final = proc;
final += " ";
for (int j = 0; j < args.size(); j++) {
final += args[j];
final += " ";
}
system(final.c_str());
You can compile, assuming the target computer has a compiler (again, not secure) and calls it (e.g. g ++ source.cpp).
3) use pipes. Sample code:
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;
}
source to share