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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include <rfb_win32/LaunchProcess.h>
  20. #include <rfb_win32/ModuleFileName.h>
  21. #include <rfb_win32/Win32Util.h>
  22. #include <rdr/Exception.h>
  23. #include <stdio.h>
  24. using namespace rfb;
  25. using namespace win32;
  26. LaunchProcess::LaunchProcess(const TCHAR* exeName_, const TCHAR* params_)
  27. : exeName(tstrDup(exeName_)), params(tstrDup(params_)) {
  28. memset(&procInfo, 0, sizeof(procInfo));
  29. }
  30. LaunchProcess::~LaunchProcess() {
  31. await();
  32. }
  33. void LaunchProcess::start(HANDLE userToken, bool createConsole) {
  34. if (procInfo.hProcess && (WaitForSingleObject(procInfo.hProcess, 0) != WAIT_OBJECT_0))
  35. return;
  36. await();
  37. returnCode = STILL_ACTIVE;
  38. DWORD size;
  39. char desktopName[256];
  40. char buf[256];
  41. HDESK desktop = GetThreadDesktop(GetCurrentThreadId());
  42. if (!GetUserObjectInformation(desktop, UOI_NAME, buf, 256, &size))
  43. throw rdr::SystemException("unable to launch process", GetLastError());
  44. snprintf(desktopName, 256, "WinSta0\\%s", buf);
  45. // - Create storage for the process startup information
  46. STARTUPINFO sinfo;
  47. memset(&sinfo, 0, sizeof(sinfo));
  48. sinfo.cb = sizeof(sinfo);
  49. sinfo.lpDesktop = desktopName;
  50. // - Concoct a suitable command-line
  51. TCharArray exePath;
  52. if (!tstrContains(exeName.buf, _T('\\'))) {
  53. ModuleFileName filename;
  54. TCharArray path; splitPath(filename.buf, &path.buf, 0);
  55. exePath.buf = new TCHAR[_tcslen(path.buf) + _tcslen(exeName.buf) + 2];
  56. _stprintf(exePath.buf, _T("%s\\%s"), path.buf, exeName.buf);
  57. } else {
  58. exePath.buf = tstrDup(exeName.buf);
  59. }
  60. // - Start the process
  61. // Note: We specify the exe's precise path in the ApplicationName parameter,
  62. // AND include the name as the first part of the CommandLine parameter,
  63. // because CreateProcess doesn't make ApplicationName argv[0] in C programs.
  64. TCharArray cmdLine(_tcslen(exeName.buf) + 3 + _tcslen(params.buf) + 1);
  65. _stprintf(cmdLine.buf, _T("\"%s\" %s"), exeName.buf, params.buf);
  66. DWORD flags = createConsole ? CREATE_NEW_CONSOLE : CREATE_NO_WINDOW;
  67. BOOL success;
  68. if (userToken != INVALID_HANDLE_VALUE)
  69. success = CreateProcessAsUser(userToken, exePath.buf, cmdLine.buf, 0, 0, FALSE, flags, 0, 0, &sinfo, &procInfo);
  70. else
  71. success = CreateProcess(exePath.buf, cmdLine.buf, 0, 0, FALSE, flags, 0, 0, &sinfo, &procInfo);
  72. if (!success)
  73. throw rdr::SystemException("unable to launch process", GetLastError());
  74. // Wait for it to finish initialising
  75. WaitForInputIdle(procInfo.hProcess, 15000);
  76. }
  77. void LaunchProcess::detach()
  78. {
  79. if (!procInfo.hProcess)
  80. return;
  81. CloseHandle(procInfo.hProcess);
  82. CloseHandle(procInfo.hThread);
  83. memset(&procInfo, 0, sizeof(procInfo));
  84. }
  85. bool LaunchProcess::await(DWORD timeoutMs) {
  86. if (!procInfo.hProcess)
  87. return true;
  88. DWORD result = WaitForSingleObject(procInfo.hProcess, timeoutMs);
  89. if (result == WAIT_OBJECT_0) {
  90. GetExitCodeProcess(procInfo.hProcess, &returnCode);
  91. detach();
  92. return true;
  93. } else if (result == WAIT_FAILED) {
  94. throw rdr::SystemException("await() failed", GetLastError());
  95. }
  96. return false;
  97. }