Click here to Skip to main content
Click here to Skip to main content

CreateProcess() and wait for result

, 2 Mar 2012
Rate this:
Please Sign up or sign in to vote.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SuperFun21

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 PinmemberYuan Xulei29-Mar-13 8:52 
QuestionNot an article... PinmvpDave Kreskowiak25-Feb-12 9:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 2 Mar 2012
Article Copyright 2012 by SuperFun21
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid