CreateProcess() and wait for result






4.20/5 (8 votes)
A general function using MFC that runs a command using CreateProcess(), waits for it to terminate, and returns its ExitCode.
Introduction
This is a short function that will run another program as if from the command line, wait for it to finish, and return the process's exit code.
Using the Code
cmdLine
= The command to execute with all command-line parameters, like
\
"\"c:\\Program Files\\Acme\\runcheck.exe\" -t \"Hourly Check\""
Remember to:
- Include the fully-qualified path to each file.
- Put each file path in quotes
- Escape your backslashes
- Escape any quotation marks
//////////////////////////////////////////////////////////////////////////////////////////////////
// Executes the given command using CreateProcess() and WaitForSingleObject().
// Returns FALSE if the command could not be executed or if the exit code could not be determined.
BOOL executeCommandLine(CString cmdLine, DWORD & exitCode)
{
PROCESS_INFORMATION processInformation = {0};
STARTUPINFO startupInfo = {0};
startupInfo.cb = sizeof(startupInfo);
int nStrBuffer = cmdLine.GetLength() + 50;
// Create the process
BOOL result = CreateProcess(NULL, cmdLine.GetBuffer(nStrBuffer),
NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
NULL, NULL, &startupInfo, &processInformation);
cmdLine.ReleaseBuffer();
if (!result)
{
// CreateProcess() failed
// Get the error from the system
LPVOID lpMsgBuf;
DWORD dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
// Display the error
CString strError = (LPTSTR) lpMsgBuf;
TRACE(_T("::executeCommandLine() failed at CreateProcess()\nCommand=%s\nMessage=%s\n\n"), cmdLine, strError);
// Free resources created by the system
LocalFree(lpMsgBuf);
// We failed.
return FALSE;
}
else
{
// Successfully created the process. Wait for it to finish.
WaitForSingleObject( processInformation.hProcess, INFINITE );
// Get the exit code.
result = GetExitCodeProcess(processInformation.hProcess, &exitCode);
// Close the handles.
CloseHandle( processInformation.hProcess );
CloseHandle( processInformation.hThread );
if (!result)
{
// Could not get exit code.
TRACE(_T("Executed command but couldn't get exit code.\nCommand=%s\n"), cmdLine);
return FALSE;
}
// We succeeded.
return TRUE;
}
}
Points of Interest
The option CREATE_NO_WINDOW
prevents a black command-prompt window from appearing.
The function will return FALSE if the command successfully executes but could not determine the exit code. You may or may not want to modify this behavior.
An exit code of zero typically indicates success and other return value indicates failure but this is not a rule. Test the command you are interested in to determine which exit codes it uses.