运行external.exe并从中接收返回值 在C++中,有可能调用/运行另一个可执行文件并从该可执行文件中接收返回值(例如1或0,以表示其他exe是否成功地执行了它的操作)?p>
为了简单起见,举个例子,如果我有一个名为filelist.exe的外部控制台.exe,它列出目录中的所有文件并将这些文件名写入文件。如果filelist.exe成功运行,则main返回1,否则返回0 如果使用以下代码运行filelist.exe,是否有方法从filelist.exe获取返回值运行external.exe并从中接收返回值 在C++中,有可能调用/运行另一个可执行文件并从该可执行文件中接收返回值(例如1或0,以表示其他exe是否成功地执行了它的操作)?p>,c++,windows,command-line,console,C++,Windows,Command Line,Console,为了简单起见,举个例子,如果我有一个名为filelist.exe的外部控制台.exe,它列出目录中的所有文件并将这些文件名写入文件。如果filelist.exe成功运行,则main返回1,否则返回0 如果使用以下代码运行filelist.exe,是否有方法从filelist.exe获取返回值 int res = system("filelist.exe dirPath"); // Maybe the windows function CreateProcess() allows filelis
int res = system("filelist.exe dirPath");
// Maybe the windows function CreateProcess() allows filelist.exe to return
// a value to the currently running application?
CreateProcess();
注意:我不打算创建一个简单的控制台应用程序来列出目录中的文件。我正在尝试创建一个控制台应用程序来检查用户是否有第三方程序的有效版本,如果有有效版本,则返回1,如果没有,则返回0 是的,您将启动另一个进程并运行可执行文件。您的请求被调用,并且通常通过或在类似您的场景中实现。是的,您将启动另一个进程并运行可执行文件。您所要求的是调用,通常通过或在类似您的场景中实现。示例如下:
res = CreateProcess(
NULL, // pointer to name of executable module
commandLine, // pointer to command line string
NULL, // pointer to process security attributes
NULL, // pointer to thread security attributes
TRUE, // handle inheritance flag
0, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&StartupInfo, // pointer to STARTUPINFO
&ProcessInfo // pointer to PROCESS_INFORMATION
);
if (!res)
// process creation failed!
{
showError(commandLine);
retVal = 1;
}
else if (waitForCompletion)
{
res = WaitForSingleObject(
ProcessInfo.hProcess,
INFINITE // time-out interval in milliseconds
);
GetExitCodeProcess(ProcessInfo.hProcess, &exitCode);
retVal = (int)exitCode;
}
使用
GetExitProcess()
从process
对象检索外部进程的返回代码。这假设您的代码在启动流程后等待完成。示例如下所示:
res = CreateProcess(
NULL, // pointer to name of executable module
commandLine, // pointer to command line string
NULL, // pointer to process security attributes
NULL, // pointer to thread security attributes
TRUE, // handle inheritance flag
0, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&StartupInfo, // pointer to STARTUPINFO
&ProcessInfo // pointer to PROCESS_INFORMATION
);
if (!res)
// process creation failed!
{
showError(commandLine);
retVal = 1;
}
else if (waitForCompletion)
{
res = WaitForSingleObject(
ProcessInfo.hProcess,
INFINITE // time-out interval in milliseconds
);
GetExitCodeProcess(ProcessInfo.hProcess, &exitCode);
retVal = (int)exitCode;
}
使用
GetExitProcess()
从process
对象检索外部进程的返回代码。这假设您的代码在启动进程后等待完成。如果您尝试使用CreateProcess
则需要使用GetExitCodeProcess
函数在进程退出后获取返回值。如果您尝试使用CreateProcess
则需要使用GetExitCodeProcess
函数,用于在进程退出后获取返回值。