Click here to Skip to main content
14,573,754 members

CreateProcess() and wait for result

Rate this:
4.20 (8 votes)
Please Sign up or sign in to vote.
4.20 (8 votes)
2 Mar 2012CPOL
A general function using MFC that runs a command using CreateProcess(), waits for it to terminate, and returns its ExitCode.


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);

   if (!result)
      // CreateProcess() failed
      // Get the error from the system
      LPVOID lpMsgBuf;
      DWORD dw = GetLastError();
                    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

      // We failed.
      return FALSE;
      // 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.


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


About the Author

United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 4 Pin
Yuan Xulei29-Mar-13 8:52
MemberYuan Xulei29-Mar-13 8:52 
QuestionNot an article... Pin
Dave Kreskowiak25-Feb-12 9:06
mveDave Kreskowiak25-Feb-12 9:06 

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

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

Posted 2 Mar 2012

Tagged as


20 bookmarked