如何在 C++ 程序中调用 .exe 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5155537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call .exe file within c++ program?
提问by niro
I want to use a exe file (convert.exe), inside my C++ program. This "exe" file change my output file format to another format. when, i use this convert.exe from my command-prompt (cmd), i have to type like this;
我想在我的 C++ 程序中使用一个 exe 文件 (convert.exe)。这个“exe”文件将我的输出文件格式更改为另一种格式。当我从我的命令提示符(cmd)中使用这个 convert.exe 时,我必须像这样输入;
convert -in myfile -out convertedfile -n -e -h
转换 -in myfile -out 转换文件 -n -e -h
where;
在哪里;
myfile= name of the file, I obtain from my c++ program convertedfile= result of the "convert.exe" file -n, -e, -h = are some parameters (columns) that i need to use to get output file with my desired data columns.
myfile= 文件名,我从我的 C++ 程序获得了“convert.exe”文件的结果所需的数据列。
i tried with system(convert.exe). but, it does not work as i did not know how to use all those parameters.
我尝试使用系统(convert.exe)。但是,它不起作用,因为我不知道如何使用所有这些参数。
回答by dm76
The std::system
function expects a const char *
, so how about you try
该std::system
函数需要 a const char *
,所以你试试
system("convert -in myfile -out convertedfile -n -e -h")
system("convert -in myfile -out convertedfile -n -e -h")
Then, if you want to be a little more flexible, you use std::sprintf
can create a string with the right elements in it and then pass it to the system() function like so:
然后,如果你想更灵活一点,你std::sprintf
可以使用can 创建一个包含正确元素的字符串,然后将它传递给 system() 函数,如下所示:
// create a string, i.e. 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);
回答by fretje
Have a look at the ShellExecute function:
ShellExecute(NULL, "open", "<fully_qualified_path_to_executable>\convert.exe",
"-in myfile -out convertedfile -n -e -h",
NULL, SW_SHOWNORMAL);
回答by rkellerm
You can use Win32 API CreateProcess.
您可以使用 Win32 API CreateProcess。
回答by Yuriy Petrovskiy
Use one of these:
使用以下之一:
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);
exec() family of functions creates a new process image from a regular, executable file...
回答by raetza
system (command); from stdlib.h
系统(命令);来自 stdlib.h
Since you don't want parallel execution wouldn't three consecutive calls to the execs with system () work for you?
既然您不希望并行执行,那么使用 system() 连续三次调用 exec 对您不起作用吗?