: If you make a batch-file with the text...:
:
: App.exe
: echo The program returned %ERRORLEVEL%
: pause
:
: ...then it will print what App.exe returned. I want to do this in a c++ program. Please help!
:
This is as close as I could get, browsing through the MSDN Library online:
#include <windows.h>
#include <stdio.h>
void main( VOID )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
TEXT("MyChildProcess"), // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
Now I think you can call GetExitCodeProcess just before the CloseHandle() statement to find out the exit code of the process created.
Here's the MSDN Page on it:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getexitcodeprocess.asp
The code example I gave you can be found at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/creating_processes.asp
The MSDN article on CreateProcess:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp
At the remarks there is a comment about running DOS programs. You need to set the Filename paramater to NULL and make the entire call in the Commandline...
Hope this is of any assistance to you...
Greets...
Richard