1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/* $Id: VBoxWindowsAdditions.cpp $ */
/** @file
* VBoxWindowsAdditions - The Windows Guest Additions Loader.
*
* This is STUB which select whether to install 32-bit or 64-bit additions.
*/
/*
* Copyright (C) 2006-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <Windows.h>
#ifndef ERROR_ELEVATION_REQUIRED /* Windows Vista and later. */
# define ERROR_ELEVATION_REQUIRED 740
#endif
#include <stdio.h>
#include <string.h>
static BOOL IsWow64(void)
{
BOOL bIsWow64 = FALSE;
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(L"kernel32"), "IsWow64Process");
if (fnIsWow64Process != NULL)
{
if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
{
fwprintf(stderr, L"ERROR: Could not determine process type!\n");
/* Error in retrieving process type - assume that we're running on 32bit. */
bIsWow64 = FALSE;
}
}
return bIsWow64;
}
static void WaitForProcess2(HANDLE hProcess, int *piExitCode)
{
/*
* Wait for the process, make sure the deal with messages.
*/
for (;;)
{
DWORD dwRc = MsgWaitForMultipleObjects(1, &hProcess, FALSE, 5000/*ms*/, QS_ALLEVENTS);
MSG Msg;
while (PeekMessageW(&Msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&Msg);
DispatchMessageW(&Msg);
}
if (dwRc == WAIT_OBJECT_0)
break;
if ( dwRc != WAIT_TIMEOUT
&& dwRc != WAIT_OBJECT_0 + 1)
{
fwprintf(stderr, L"ERROR: MsgWaitForMultipleObjects failed: %u (%u)\n", dwRc, GetLastError());
break;
}
}
/*
* Collect the process info.
*/
DWORD dwExitCode;
if (GetExitCodeProcess(hProcess, &dwExitCode))
*piExitCode = (int)dwExitCode;
else
{
fwprintf(stderr, L"ERROR: GetExitCodeProcess failed: %u\n", GetLastError());
*piExitCode = 16;
}
}
static void WaitForProcess(HANDLE hProcess, int *piExitCode)
{
DWORD WaitRc = WaitForSingleObjectEx(hProcess, INFINITE, TRUE);
while ( WaitRc == WAIT_IO_COMPLETION
|| WaitRc == WAIT_TIMEOUT)
WaitRc = WaitForSingleObjectEx(hProcess, INFINITE, TRUE);
if (WaitRc == WAIT_OBJECT_0)
{
DWORD dwExitCode;
if (GetExitCodeProcess(hProcess, &dwExitCode))
*piExitCode = (int)dwExitCode;
else
{
fwprintf(stderr, L"ERROR: GetExitCodeProcess failed: %u\n", GetLastError());
*piExitCode = 16;
}
}
else
{
fwprintf(stderr, L"ERROR: WaitForSingleObjectEx failed: %u (%u)\n", WaitRc, GetLastError());
*piExitCode = 16;
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
/*
* Gather the parameters of the real installer program.
*/
SetLastError(NO_ERROR);
WCHAR wszCurDir[_MAX_PATH] = { 0 };
DWORD cchCurDir = GetCurrentDirectoryW(sizeof(wszCurDir), wszCurDir);
if (cchCurDir == 0 || cchCurDir >= sizeof(wszCurDir))
{
fwprintf(stderr, L"ERROR: GetCurrentDirectoryW failed: %u (ret %u)\n", GetLastError(), cchCurDir);
return 12;
}
SetLastError(NO_ERROR);
WCHAR wszModule[_MAX_PATH] = { 0 };
DWORD cchModule = GetModuleFileNameW(NULL, wszModule, sizeof(wszModule));
if (cchModule == 0 || cchModule >= sizeof(wszModule))
{
fwprintf(stderr, L"ERROR: GetModuleFileNameW failed: %u (ret %u)\n", GetLastError(), cchModule);
return 13;
}
/* Strip the extension off the module name and construct the arch specific
one of the real installer program. */
DWORD off = cchModule - 1;
while ( off > 0
&& ( wszModule[off] != '/'
&& wszModule[off] != '\\'
&& wszModule[off] != ':'))
{
if (wszModule[off] == '.')
{
wszModule[off] = '\0';
cchModule = off;
break;
}
off--;
}
WCHAR const *pwszSuff = IsWow64() ? L"-amd64.exe" : L"-x86.exe";
size_t cchSuff = wcslen(pwszSuff);
if (cchSuff + cchModule >= sizeof(wszModule))
{
fwprintf(stderr, L"ERROR: Real installer name is too long (%u chars)\n", cchSuff + cchModule);
return 14;
}
wcscpy(&wszModule[cchModule], pwszSuff);
cchModule += cchSuff;
/* Replace the first argument of the argument list. */
PWCHAR pwszNewCmdLine = NULL;
LPCWSTR pwszOrgCmdLine = GetCommandLineW();
if (pwszOrgCmdLine) /* Dunno if this can be NULL, but whatever. */
{
/* Skip the first argument in the original. */
/** @todo Is there some ISBLANK or ISSPACE macro/function in Win32 that we could
* use here, if it's correct wrt. command line conventions? */
WCHAR wch;
while ((wch = *pwszOrgCmdLine) == L' ' || wch == L'\t')
pwszOrgCmdLine++;
if (wch == L'"')
{
pwszOrgCmdLine++;
while ((wch = *pwszOrgCmdLine) != L'\0')
{
pwszOrgCmdLine++;
if (wch == L'"')
break;
}
}
else
{
while ((wch = *pwszOrgCmdLine) != L'\0')
{
pwszOrgCmdLine++;
if (wch == L' ' || wch == L'\t')
break;
}
}
while ((wch = *pwszOrgCmdLine) == L' ' || wch == L'\t')
pwszOrgCmdLine++;
/* Join up "szModule" with the remainder of the original command line. */
size_t cchOrgCmdLine = wcslen(pwszOrgCmdLine);
size_t cchNewCmdLine = 1 + cchModule + 1 + 1 + cchOrgCmdLine + 1;
PWCHAR pwsz = pwszNewCmdLine = (PWCHAR)LocalAlloc(LPTR, cchNewCmdLine * sizeof(WCHAR));
if (!pwsz)
{
fwprintf(stderr, L"ERROR: Out of memory (%u bytes)\n", cchNewCmdLine);
return 15;
}
*pwsz++ = L'"';
wcscpy(pwsz, wszModule);
pwsz += cchModule;
*pwsz++ = L'"';
if (cchOrgCmdLine)
{
*pwsz++ = L' ';
wcscpy(pwsz, pwszOrgCmdLine);
}
else
{
*pwsz = L'\0';
pwszOrgCmdLine = NULL;
}
}
/*
* Start the process.
*/
int iRet = 0;
STARTUPINFOW StartupInfo = { sizeof(StartupInfo), 0 };
PROCESS_INFORMATION ProcInfo = { 0 };
SetLastError(740);
BOOL fOk = CreateProcessW(wszModule,
pwszNewCmdLine,
NULL /*pProcessAttributes*/,
NULL /*pThreadAttributes*/,
TRUE /*fInheritHandles*/,
0 /*dwCreationFlags*/,
NULL /*pEnvironment*/,
NULL /*pCurrentDirectory*/,
&StartupInfo,
&ProcInfo);
if (fOk)
{
/* Wait for the process to finish. */
CloseHandle(ProcInfo.hThread);
WaitForProcess(ProcInfo.hProcess, &iRet);
CloseHandle(ProcInfo.hProcess);
}
else if (GetLastError() == ERROR_ELEVATION_REQUIRED)
{
/*
* Elevation is required. That can be accomplished via ShellExecuteEx
* and the runas atom.
*/
MSG Msg;
PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE);
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
SHELLEXECUTEINFOW ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFOW);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = L"runas" ;
ShExecInfo.lpFile = wszModule;
ShExecInfo.lpParameters = pwszOrgCmdLine; /* pass only args here!!! */
ShExecInfo.lpDirectory = wszCurDir;
ShExecInfo.nShow = SW_NORMAL;
ShExecInfo.hProcess = INVALID_HANDLE_VALUE;
if (ShellExecuteExW(&ShExecInfo))
{
if (ShExecInfo.hProcess != INVALID_HANDLE_VALUE)
{
WaitForProcess2(ShExecInfo.hProcess, &iRet);
CloseHandle(ShExecInfo.hProcess);
}
else
{
fwprintf(stderr, L"ERROR: ShellExecuteExW did not return a valid process handle!\n");
iRet = 1;
}
}
else
{
fwprintf(stderr, L"ERROR: Failed to execute '%ws' via ShellExecuteExW: %u\n", wszModule, GetLastError());
iRet = 9;
}
}
else
{
fwprintf(stderr, L"ERROR: Failed to execute '%ws' via CreateProcessW: %u\n", wszModule, GetLastError());
iRet = 8;
}
if (pwszNewCmdLine)
LocalFree(pwszNewCmdLine);
#if 0
fwprintf(stderr, L"DEBUG: iRet=%d\n", iRet);
fflush(stderr);
#endif
return iRet;
}
|