How to call a .exe file in a C ++ program?

I want to use an exe file (convert.exe) inside my C ++ program. This exe file changes the output file format to another format. when, I use this convert.exe from my command line (cmd), I have to enter like this:

convert -in myfile -out convertfile -n -e -h

Where

myfile = file name, I get from my C ++ program convertfile = the result of the file "convert.exe" -n, -e, -h = are some parameters (columns) that I need to use to get the output file using my required data columns.

I tried with the system (convert.exe). but, this does not work, as I did not know how to use all of these parameters.

+7
source share
5 answers

The std::system function expects const char * , so what about trying

system("convert -in myfile -out convertedfile -n -e -h")

Then, if you want to be a little more flexible, you use std::sprintf to create a string with the necessary elements in it and then pass it to the system () function as follows:

 // create a string, ie an array of 50 char char command[50]; // this will 'fill' the string command with the right stuff, // assuming myFile and convertedFile are strings themselves sprintf (command, "convert -in %s -out %s -n -e -h", myFile, convertedFile); // system call system(command); 
+13
source

Check out the ShellExecute function :

 ShellExecute(NULL, "open", "<fully_qualified_path_to_executable>\convert.exe", "-in myfile -out convertedfile -n -e -h", NULL, SW_SHOWNORMAL); 
+4
source

You can use the Win32 API CreateProcess .

+2
source

Use one of the following:

 int execl(char * pathname, char * arg0, arg1, ..., argn, NULL); int execle(char * pathname, char * arg0, arg1,..., argn, NULL, char ** envp); int execlp(char * pathname, char * arg0, arg1,..., argn, NULL); int execlpe(char * pathname, char * arg0, arg1,..., argn, NULL, char ** envp);int execv(char * pathname, char * argv[]); int execve(char * pathname, char * argv[], char ** envp); int execvp(char * pathname, char * argv[]); int execvpe(char * pathname, char * argv[],char ** envp); 

The exec () family of functions creates a new process image from a regular executable ...

+1
source

system (team); from stdlib.h

Since you don't want concurrent execution to not execute three consecutive execs calls with system ()?

+1
source

All Articles