In C++ is it possible to call/run another executable and receive a return value from that executable(for example either a 1 or 0 to signify whether the other exe performed its operations successfully)?
For simplicity, take this example, if I have an external console .exe called filelist.exe that lists all files in a directory and writes those file names to a file. If the filelist.exe runs successfully then main returns 1 and if not returns 0.
If I were to run filelist.exe using the following code, is there a way to get a return value from filelist.exe?
int res = system("filelist.exe dirPath");
// Maybe the windows function CreateProcess() allows filelist.exe to return
// a value to the currently running application?
CreateProcess();
Note I am not intending to create a simple console application that lists files in a directory I am attempting to create a console application that checks whether the user has a valid version of a 3rd party program and return 1 if they do have a valid version and 0 if not.
Yes, you would start another process and run the executable. What you're asking is called inter-process communication and is typically achieved through either signals or pipes in a scenario like yours.
An example looks as follows:
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;
}
The return code of the external process is retrieved from the Process
object using GetExitProcess()
. This assumes that your code is waiting for completion after starting the process.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.