blob: 56a712e6e9f9a9fda288ca92cab8f74ceeb4c5cf [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* 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
27using namespace rfb;
28using namespace win32;
29
30
31LaunchProcess::LaunchProcess(const TCHAR* exeName_, const TCHAR* params_)
32: exeName(tstrDup(exeName_)), params(tstrDup(params_)) {
33 memset(&procInfo, 0, sizeof(procInfo));
34}
35
36LaunchProcess::~LaunchProcess() {
37 await();
38}
39
40
41void 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
47 // - Create storage for the process startup information
48 STARTUPINFO sinfo;
49 memset(&sinfo, 0, sizeof(sinfo));
50 sinfo.cb = sizeof(sinfo);
51
52 // - Concoct a suitable command-line
53 TCharArray exePath;
54 if (!tstrContains(exeName.buf, _T('\\'))) {
55 ModuleFileName filename;
56 TCharArray path; splitPath(filename.buf, &path.buf, 0);
57 exePath.buf = new TCHAR[_tcslen(path.buf) + _tcslen(exeName.buf) + 2];
58 _stprintf(exePath.buf, _T("%s\\%s"), path.buf, exeName.buf);
59 } else {
60 exePath.buf = tstrDup(exeName.buf);
61 }
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
78 // Wait for it to finish initialising
79 WaitForInputIdle(procInfo.hProcess, 15000);
80}
81
82void LaunchProcess::detach()
83{
84 if (!procInfo.hProcess)
85 return;
86 CloseHandle(procInfo.hProcess);
87 CloseHandle(procInfo.hThread);
88 memset(&procInfo, 0, sizeof(procInfo));
89}
90
91bool LaunchProcess::await(DWORD timeoutMs) {
92 if (!procInfo.hProcess)
93 return true;
94 DWORD result = WaitForSingleObject(procInfo.hProcess, timeoutMs);
95 if (result == WAIT_OBJECT_0) {
96 GetExitCodeProcess(procInfo.hProcess, &returnCode);
97 detach();
98 return true;
99 } else if (result == WAIT_FAILED) {
100 throw rdr::SystemException("await() failed", GetLastError());
101 }
102 return false;
103}