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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 TCHAR* exeName_, const TCHAR* params_)
  30. : exeName(tstrDup(exeName_)), params(tstrDup(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. TCharArray exePath;
  55. if (!tstrContains(exeName.buf, _T('\\'))) {
  56. ModuleFileName filename;
  57. TCharArray path; splitPath(filename.buf, &path.buf, 0);
  58. exePath.buf = new TCHAR[_tcslen(path.buf) + _tcslen(exeName.buf) + 2];
  59. _stprintf(exePath.buf, _T("%s\\%s"), path.buf, exeName.buf);
  60. } else {
  61. exePath.buf = tstrDup(exeName.buf);
  62. }
  63. // - Start the process
  64. // Note: We specify the exe's precise path in the ApplicationName parameter,
  65. // AND include the name as the first part of the CommandLine parameter,
  66. // because CreateProcess doesn't make ApplicationName argv[0] in C programs.
  67. TCharArray cmdLine(_tcslen(exeName.buf) + 3 + _tcslen(params.buf) + 1);
  68. _stprintf(cmdLine.buf, _T("\"%s\" %s"), exeName.buf, params.buf);
  69. DWORD flags = createConsole ? CREATE_NEW_CONSOLE : CREATE_NO_WINDOW;
  70. BOOL success;
  71. if (userToken != INVALID_HANDLE_VALUE)
  72. success = CreateProcessAsUser(userToken, exePath.buf, cmdLine.buf, 0, 0, FALSE, flags, 0, 0, &sinfo, &procInfo);
  73. else
  74. success = CreateProcess(exePath.buf, cmdLine.buf, 0, 0, FALSE, flags, 0, 0, &sinfo, &procInfo);
  75. if (!success)
  76. throw rdr::SystemException("unable to launch process", GetLastError());
  77. // Wait for it to finish initialising
  78. WaitForInputIdle(procInfo.hProcess, 15000);
  79. }
  80. void LaunchProcess::detach()
  81. {
  82. if (!procInfo.hProcess)
  83. return;
  84. CloseHandle(procInfo.hProcess);
  85. CloseHandle(procInfo.hThread);
  86. memset(&procInfo, 0, sizeof(procInfo));
  87. }
  88. bool LaunchProcess::await(DWORD timeoutMs) {
  89. if (!procInfo.hProcess)
  90. return true;
  91. DWORD result = WaitForSingleObject(procInfo.hProcess, timeoutMs);
  92. if (result == WAIT_OBJECT_0) {
  93. GetExitCodeProcess(procInfo.hProcess, &returnCode);
  94. detach();
  95. return true;
  96. } else if (result == WAIT_FAILED) {
  97. throw rdr::SystemException("await() failed", GetLastError());
  98. }
  99. return false;
  100. }