How to create a process and wait till it ends in C++

C, C++, Visual C++, C++.Net Topics

How to create a process and wait till it ends in C++

Postby Saman » Thu Nov 24, 2011 8:43 am

With this function you can launch an executable file and wait until it finishes the operation.
Syntax: [ Download ] [ Hide ]
Using C Syntax Highlighting
  1. size_t ExecuteProcess(std::wstring FullPathToExe, std::wstring Parameters, size_t SecondsToWait){
  2.  
  3.         size_t iMyCounter = 0, iReturnVal = 0, iPos = 0;
  4.         DWORD dwExitCode = 0;
  5.         std::wstring sTempStr = L"";
  6.  
  7.         /* - NOTE - You should check here to see if the exe even exists */
  8.  
  9.         /* Add a space to the beginning of the Parameters */
  10.         if (Parameters.size() != 0){
  11.                         if (Parameters[0] != L' '){
  12.                                 Parameters.insert(0,L" ");
  13.                         }
  14.         }
  15.  
  16.         /* The first parameter needs to be the exe itself */
  17.         sTempStr = FullPathToExe;
  18.         iPos = sTempStr.find_last_of(L"\\");
  19.         sTempStr.erase(0, iPos +1);
  20.         Parameters = sTempStr.append(Parameters);
  21.  
  22.         /* CreateProcessW can modify Parameters thus we allocate needed memory */
  23.         wchar_t * pwszParam = new wchar_t[Parameters.size() + 1];
  24.         if (pwszParam == 0){
  25.                 return 1;
  26.         }
  27.         const wchar_t* pchrTemp = Parameters.c_str();
  28.         wcscpy_s(pwszParam, Parameters.size() + 1, pchrTemp);
  29.  
  30.         /* CreateProcess API initialization */
  31.         STARTUPINFOW siStartupInfo;
  32.         PROCESS_INFORMATION piProcessInfo;
  33.         memset(&siStartupInfo, 0, sizeof(siStartupInfo));
  34.         memset(&piProcessInfo, 0, sizeof(piProcessInfo));
  35.         siStartupInfo.cb = sizeof(siStartupInfo);
  36.  
  37.         if (CreateProcessW(const_cast<LPCWSTR>(FullPathToExe.c_str()), pwszParam, 0, 0, false, CREATE_DEFAULT_ERROR_MODE, 0, 0, &siStartupInfo, &piProcessInfo) != false){
  38.                 /* Watch the process. */
  39.                 dwExitCode = WaitForSingleObject(piProcessInfo.hProcess, (SecondsToWait * 1000));
  40.         }
  41.         else{
  42.                 /* CreateProcess failed */
  43.                 iReturnVal = GetLastError();
  44.         }
  45.  
  46.         /* Free memory */
  47.         delete[]pwszParam;
  48.         pwszParam = 0;
  49.  
  50.         /* Release handles */
  51.         CloseHandle(piProcessInfo.hProcess);
  52.         CloseHandle(piProcessInfo.hThread);
  53.  
  54.         return iReturnVal;
  55. }
  56.  
Syntax parsed in 0.012 seconds
User avatar
Saman
Support Team
Support Team
 
Posts: 814
Joined: Fri Jul 31, 2009 5:02 pm
Cash on hand: 152,553.20
Location: Mount Lavinia
Medals: 1
EC_Achievment (1)

Invitations sent: 44
Registered friends: 1
Reputation point: 37
Staff Sergeant

Return to C/C++ Programming

Who is online

Users browsing this forum: No registered users and 0 guests