blob: 0a48d60c7f5f9bdedfdbb91ece5760d1ec31f31c [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* 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
19// -=- LaunchProcess.cxx
20
21#include <rfb_win32/LaunchProcess.h>
22#include <rfb_win32/Win32Util.h>
23
24#include <rfb/util.h>
25
26using namespace rfb;
27using namespace win32;
28
29
30LaunchProcess::LaunchProcess(const TCHAR* exeName_, const TCHAR* params_)
31: exeName(tstrDup(exeName_)), params(tstrDup(params_)) {
32 memset(&procInfo, 0, sizeof(procInfo));
33}
34
35LaunchProcess::~LaunchProcess() {
36 await();
37}
38
39
40void LaunchProcess::start(HANDLE userToken) {
41 if (procInfo.hProcess && (WaitForSingleObject(procInfo.hProcess, 0) != WAIT_OBJECT_0))
42 return;
43 await();
44
45 // - Create storage for the process startup information
46 STARTUPINFO sinfo;
47 memset(&sinfo, 0, sizeof(sinfo));
48 sinfo.cb = sizeof(sinfo);
49
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
61 // - Start the VNC server
62 // Note: We specify the exe's precise path in the ApplicationName parameter,
63 // AND include the name as the first part of the CommandLine parameter,
64 // because CreateProcess doesn't make ApplicationName argv[0] in C programs.
65 TCharArray cmdLine(_tcslen(exeName.buf) + 3 + _tcslen(params.buf) + 1);
66 _stprintf(cmdLine.buf, _T("\"%s\" %s"), exeName.buf, params.buf);
67#ifdef _DEBUG
68 DWORD flags = CREATE_NEW_CONSOLE;
69#else
70 DWORD flags = CREATE_NO_WINDOW;
71#endif
72 BOOL success;
73 if (userToken)
74 success = CreateProcessAsUser(userToken, exePath.buf, cmdLine.buf, 0, 0, FALSE, flags, 0, 0, &sinfo, &procInfo);
75 else
76 success = CreateProcess(exePath.buf, cmdLine.buf, 0, 0, FALSE, flags, 0, 0, &sinfo, &procInfo);
77 if (!success)
78 throw rdr::SystemException("unable to launch process", GetLastError());
79
80 // Wait for it to finish initialising
81 WaitForInputIdle(procInfo.hProcess, 15000);
82}
83
84void LaunchProcess::await() {
85 if (!procInfo.hProcess)
86 return;
87 WaitForSingleObject(procInfo.hProcess, INFINITE);
88 CloseHandle(procInfo.hProcess);
89 CloseHandle(procInfo.hThread);
90 memset(&procInfo, 0, sizeof(procInfo));
91}
92