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