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.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright (C) 2002-2004 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/Win32Util.h>
  21. #include <rfb/util.h>
  22. using namespace rfb;
  23. using namespace win32;
  24. LaunchProcess::LaunchProcess(const TCHAR* exeName_, const TCHAR* params_)
  25. : exeName(tstrDup(exeName_)), params(tstrDup(params_)) {
  26. memset(&procInfo, 0, sizeof(procInfo));
  27. }
  28. LaunchProcess::~LaunchProcess() {
  29. await();
  30. }
  31. void LaunchProcess::start(HANDLE userToken) {
  32. if (procInfo.hProcess && (WaitForSingleObject(procInfo.hProcess, 0) != WAIT_OBJECT_0))
  33. return;
  34. await();
  35. // - Create storage for the process startup information
  36. STARTUPINFO sinfo;
  37. memset(&sinfo, 0, sizeof(sinfo));
  38. sinfo.cb = sizeof(sinfo);
  39. // - Concoct a suitable command-line
  40. TCharArray exePath;
  41. if (!tstrContains(exeName.buf, _T('\\'))) {
  42. ModuleFileName filename;
  43. TCharArray path; splitPath(filename.buf, &path.buf, 0);
  44. exePath.buf = new TCHAR[_tcslen(path.buf) + _tcslen(exeName.buf) + 2];
  45. _stprintf(exePath.buf, _T("%s\\%s"), path.buf, exeName.buf);
  46. } else {
  47. exePath.buf = tstrDup(exeName.buf);
  48. }
  49. // - Start the VNC server
  50. // Note: We specify the exe's precise path in the ApplicationName parameter,
  51. // AND include the name as the first part of the CommandLine parameter,
  52. // because CreateProcess doesn't make ApplicationName argv[0] in C programs.
  53. TCharArray cmdLine(_tcslen(exeName.buf) + 3 + _tcslen(params.buf) + 1);
  54. _stprintf(cmdLine.buf, _T("\"%s\" %s"), exeName.buf, params.buf);
  55. #ifdef _DEBUG
  56. DWORD flags = CREATE_NEW_CONSOLE;
  57. #else
  58. DWORD flags = CREATE_NO_WINDOW;
  59. #endif
  60. BOOL success;
  61. if (userToken)
  62. success = CreateProcessAsUser(userToken, exePath.buf, cmdLine.buf, 0, 0, FALSE, flags, 0, 0, &sinfo, &procInfo);
  63. else
  64. success = CreateProcess(exePath.buf, cmdLine.buf, 0, 0, FALSE, flags, 0, 0, &sinfo, &procInfo);
  65. if (!success)
  66. throw rdr::SystemException("unable to launch process", GetLastError());
  67. // Wait for it to finish initialising
  68. WaitForInputIdle(procInfo.hProcess, 15000);
  69. }
  70. void LaunchProcess::await() {
  71. if (!procInfo.hProcess)
  72. return;
  73. WaitForSingleObject(procInfo.hProcess, INFINITE);
  74. CloseHandle(procInfo.hProcess);
  75. CloseHandle(procInfo.hThread);
  76. memset(&procInfo, 0, sizeof(procInfo));
  77. }