You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LaunchProcess.cxx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. // -=- LaunchProcess.cxx
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <rfb_win32/LaunchProcess.h>
  23. #include <rfb_win32/ModuleFileName.h>
  24. #include <rfb_win32/Win32Util.h>
  25. #include <rdr/Exception.h>
  26. #include <stdio.h>
  27. using namespace rfb;
  28. using namespace win32;
  29. LaunchProcess::LaunchProcess(const char* exeName_, const char* params_)
  30. : exeName(exeName_), params(params_) {
  31. memset(&procInfo, 0, sizeof(procInfo));
  32. }
  33. LaunchProcess::~LaunchProcess() {
  34. await();
  35. }
  36. void LaunchProcess::start(HANDLE userToken, bool createConsole) {
  37. if (procInfo.hProcess && (WaitForSingleObject(procInfo.hProcess, 0) != WAIT_OBJECT_0))
  38. return;
  39. await();
  40. returnCode = STILL_ACTIVE;
  41. DWORD size;
  42. char desktopName[256];
  43. char buf[256];
  44. HDESK desktop = GetThreadDesktop(GetCurrentThreadId());
  45. if (!GetUserObjectInformation(desktop, UOI_NAME, buf, 256, &size))
  46. throw rdr::SystemException("unable to launch process", GetLastError());
  47. snprintf(desktopName, 256, "WinSta0\\%s", buf);
  48. // - Create storage for the process startup information
  49. STARTUPINFO sinfo;
  50. memset(&sinfo, 0, sizeof(sinfo));
  51. sinfo.cb = sizeof(sinfo);
  52. sinfo.lpDesktop = desktopName;
  53. // - Concoct a suitable command-line
  54. std::string exePath;
  55. if (exeName.find('\\') == std::string::npos) {
  56. ModuleFileName filename;
  57. std::string path(filename.buf);
  58. if (path.rfind('\\') != std::string::npos)
  59. path.resize(path.rfind('\\'));
  60. exePath = path + '\\' + exeName;
  61. } else {
  62. exePath = exeName;
  63. }
  64. // - Start the process
  65. // Note: We specify the exe's precise path in the ApplicationName parameter,
  66. // AND include the name as the first part of the CommandLine parameter,
  67. // because CreateProcess doesn't make ApplicationName argv[0] in C programs.
  68. std::string cmdLine;
  69. cmdLine = (std::string)"\"" + exeName + "\" " + params;
  70. DWORD flags = createConsole ? CREATE_NEW_CONSOLE : CREATE_NO_WINDOW;
  71. BOOL success;
  72. if (userToken != INVALID_HANDLE_VALUE)
  73. success = CreateProcessAsUser(userToken, exePath.c_str(),
  74. (char*)cmdLine.c_str(), 0, 0, FALSE,
  75. flags, 0, 0, &sinfo, &procInfo);
  76. else
  77. success = CreateProcess(exePath.c_str(), (char*)cmdLine.c_str(), 0,
  78. 0, FALSE, flags, 0, 0, &sinfo, &procInfo);
  79. if (!success)
  80. throw rdr::SystemException("unable to launch process", GetLastError());
  81. // Wait for it to finish initialising
  82. WaitForInputIdle(procInfo.hProcess, 15000);
  83. }
  84. void LaunchProcess::detach()
  85. {
  86. if (!procInfo.hProcess)
  87. return;
  88. CloseHandle(procInfo.hProcess);
  89. CloseHandle(procInfo.hThread);
  90. memset(&procInfo, 0, sizeof(procInfo));
  91. }
  92. bool LaunchProcess::await(DWORD timeoutMs) {
  93. if (!procInfo.hProcess)
  94. return true;
  95. DWORD result = WaitForSingleObject(procInfo.hProcess, timeoutMs);
  96. if (result == WAIT_OBJECT_0) {
  97. GetExitCodeProcess(procInfo.hProcess, &returnCode);
  98. detach();
  99. return true;
  100. } else if (result == WAIT_FAILED) {
  101. throw rdr::SystemException("await() failed", GetLastError());
  102. }
  103. return false;
  104. }