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
BOOL executeCommandLine(CString cmdLine, DWORD & exitCode)
{
PROCESS_INFORMATION processInformation = {0};
STARTUPINFO startupInfo = {0};
startupInfo.cb = sizeof(startupInfo);
int nStrBuffer = cmdLine.GetLength() + 50;
BOOL result = CreateProcess(NULL, cmdLine.GetBuffer(nStrBuffer),
NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
NULL, NULL, &startupInfo, &processInformation);
cmdLine.ReleaseBuffer();
if (!result)
{
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);
CString strError = (LPTSTR) lpMsgBuf;
TRACE(_T("::executeCommandLine() failed at CreateProcess()\nCommand=%s\nMessage=%s\n\n"), cmdLine, strError);
LocalFree(lpMsgBuf);
return FALSE;
}
else
{
WaitForSingleObject( processInformation.hProcess, INFINITE );
result = GetExitCodeProcess(processInformation.hProcess, &exitCode);
CloseHandle( processInformation.hProcess );
CloseHandle( processInformation.hThread );
if (!result)
{
TRACE(_T("Executed command but couldn't get exit code.\nCommand=%s\n"), cmdLine);
return FALSE;
}
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.