Advertisement
Guest User

Untitled

a guest
Nov 27th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include "RunProcess.h"
  2.  
  3.  
  4. RunProcess::RunProcess(std::wstring Path,std::wstring name, std::wstring Param, size_t Wait)
  5. {
  6. std::wstring ExePath = Path + name;
  7. ExecuteProcess(ExePath,Path,Param,Wait);
  8. }
  9. size_t RunProcess::ExecuteProcess(std::wstring FullPathToExe,std::wstring EnvironmentPath, std::wstring Parameters, size_t SecondsToWait)
  10. {
  11. size_t iMyCounter = 0, iReturnVal = 0, iPos = 0;
  12. DWORD dwExitCode = 0;
  13. std::wstring sTempStr = L"";
  14.  
  15. /* - NOTE - You should check here to see if the exe even exists */
  16.  
  17. /* Add a space to the beginning of the Parameters */
  18. if (Parameters.size() != 0)
  19. {
  20. if (Parameters[0] != L' ')
  21. {
  22. Parameters.insert(0,L" ");
  23. }
  24. }
  25.  
  26. /* The first parameter needs to be the exe itself */
  27. sTempStr = FullPathToExe;
  28. iPos = sTempStr.find_last_of(L"\\");
  29. sTempStr.erase(0, iPos +1);
  30. Parameters = sTempStr.append(Parameters);
  31.  
  32. /* CreateProcessW can modify Parameters thus we allocate needed memory */
  33. wchar_t * pwszParam = new wchar_t[Parameters.size() + 1];
  34. if (pwszParam == 0)
  35. {
  36. return 1;
  37. }
  38. const wchar_t* pchrTemp = Parameters.c_str();
  39. wcscpy_s(pwszParam, Parameters.size() + 1, pchrTemp);
  40.  
  41. /* CreateProcess API initialization */
  42. STARTUPINFOW siStartupInfo;
  43. PROCESS_INFORMATION piProcessInfo;
  44. memset(&siStartupInfo, 0, sizeof(siStartupInfo));
  45. memset(&piProcessInfo, 0, sizeof(piProcessInfo));
  46. siStartupInfo.cb = sizeof(siStartupInfo);
  47. siStartupInfo.dwFlags = 0x00000020;
  48. siStartupInfo.wShowWindow = 5;
  49.  
  50. if (CreateProcessW(const_cast<LPCWSTR>(FullPathToExe.c_str()),
  51. pwszParam, 0, 0, true,
  52. CREATE_DEFAULT_ERROR_MODE, 0,EnvironmentPath.c_str(),
  53. &siStartupInfo, &piProcessInfo) != false)
  54. {
  55. /* Watch the process. */
  56. dwExitCode = WaitForSingleObject(piProcessInfo.hProcess, SecondsToWait);
  57.  
  58. }
  59. else
  60. {
  61. /* CreateProcess failed */
  62. iReturnVal = GetLastError();
  63. }
  64.  
  65. /* Free memory */
  66. delete[]pwszParam;
  67. pwszParam = 0;
  68.  
  69. /* Release handles */
  70. CloseHandle(piProcessInfo.hProcess);
  71. CloseHandle(piProcessInfo.hThread);
  72.  
  73. return iReturnVal;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement