1 | /* $Id: VBoxDrvInst.cpp 42154 2012-07-13 23:00:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxDrvInst - Driver and service installation helper for Windows guests.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #ifndef UNICODE
|
---|
23 | # define UNICODE
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | #include <VBox/version.h>
|
---|
27 |
|
---|
28 | #include <Windows.h>
|
---|
29 | #include <setupapi.h>
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <tchar.h>
|
---|
32 |
|
---|
33 | /*******************************************************************************
|
---|
34 | * Defines *
|
---|
35 | *******************************************************************************/
|
---|
36 |
|
---|
37 | /* Exit codes */
|
---|
38 | #define EXIT_OK (0)
|
---|
39 | #define EXIT_REBOOT (1)
|
---|
40 | #define EXIT_FAIL (2)
|
---|
41 | #define EXIT_USAGE (3)
|
---|
42 |
|
---|
43 | /* Prototypes */
|
---|
44 | typedef struct {
|
---|
45 | PWSTR pApplicationId;
|
---|
46 | PWSTR pDisplayName;
|
---|
47 | PWSTR pProductName;
|
---|
48 | PWSTR pMfgName;
|
---|
49 | } INSTALLERINFO, *PINSTALLERINFO;
|
---|
50 | typedef const PINSTALLERINFO PCINSTALLERINFO;
|
---|
51 |
|
---|
52 | typedef enum {
|
---|
53 | DIFXAPI_SUCCESS,
|
---|
54 | DIFXAPI_INFO,
|
---|
55 | DIFXAPI_WARNING,
|
---|
56 | DIFXAPI_ERROR
|
---|
57 | } DIFXAPI_LOG;
|
---|
58 |
|
---|
59 | typedef void (WINAPI * DIFXLOGCALLBACK_W)( DIFXAPI_LOG Event, DWORD Error, PCWSTR EventDescription, PVOID CallbackContext);
|
---|
60 | typedef void ( __cdecl* DIFXAPILOGCALLBACK_W)( DIFXAPI_LOG Event, DWORD Error, PCWSTR EventDescription, PVOID CallbackContext);
|
---|
61 |
|
---|
62 | typedef DWORD (WINAPI *fnDriverPackageInstall) (PCTSTR DriverPackageInfPath, DWORD Flags, PCINSTALLERINFO pInstallerInfo, BOOL *pNeedReboot);
|
---|
63 | fnDriverPackageInstall g_pfnDriverPackageInstall = NULL;
|
---|
64 |
|
---|
65 | typedef DWORD (WINAPI *fnDriverPackageUninstall) (PCTSTR DriverPackageInfPath, DWORD Flags, PCINSTALLERINFO pInstallerInfo, BOOL *pNeedReboot);
|
---|
66 | fnDriverPackageUninstall g_pfnDriverPackageUninstall = NULL;
|
---|
67 |
|
---|
68 | typedef VOID (WINAPI *fnDIFXAPISetLogCallback) (DIFXAPILOGCALLBACK_W LogCallback, PVOID CallbackContext);
|
---|
69 | fnDIFXAPISetLogCallback g_pfnDIFXAPISetLogCallback = NULL;
|
---|
70 |
|
---|
71 | /* Defines */
|
---|
72 | #define DRIVER_PACKAGE_REPAIR 0x00000001
|
---|
73 | #define DRIVER_PACKAGE_SILENT 0x00000002
|
---|
74 | #define DRIVER_PACKAGE_FORCE 0x00000004
|
---|
75 | #define DRIVER_PACKAGE_ONLY_IF_DEVICE_PRESENT 0x00000008
|
---|
76 | #define DRIVER_PACKAGE_LEGACY_MODE 0x00000010
|
---|
77 | #define DRIVER_PACKAGE_DELETE_FILES 0x00000020
|
---|
78 |
|
---|
79 | /* DIFx error codes */
|
---|
80 | /** @todo any reason why we're not using difxapi.h instead of these redefinitions? */
|
---|
81 | #ifndef ERROR_DRIVER_STORE_ADD_FAILED
|
---|
82 | # define ERROR_DRIVER_STORE_ADD_FAILED \
|
---|
83 | (APPLICATION_ERROR_MASK | ERROR_SEVERITY_ERROR | 0x0247L)
|
---|
84 | #endif
|
---|
85 | #define ERROR_DEPENDENT_APPLICATIONS_EXIST \
|
---|
86 | (APPLICATION_ERROR_MASK|ERROR_SEVERITY_ERROR|0x300)
|
---|
87 | #define ERROR_DRIVER_PACKAGE_NOT_IN_STORE \
|
---|
88 | (APPLICATION_ERROR_MASK|ERROR_SEVERITY_ERROR|0x302)
|
---|
89 |
|
---|
90 | /* Registry string list flags */
|
---|
91 | #define VBOX_REG_STRINGLIST_NONE 0x00000000 /* No flags set. */
|
---|
92 | #define VBOX_REG_STRINGLIST_ALLOW_DUPLICATES 0x00000001 /* Allows duplicates in list when adding a value. */
|
---|
93 |
|
---|
94 | #ifdef DEBUG
|
---|
95 | # define VBOX_DRVINST_LOGFILE "C:\\Temp\\VBoxDrvInstDIFx.log"
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | bool GetErrorMsg(DWORD dwLastError, _TCHAR *pszMsg, DWORD dwBufSize)
|
---|
99 | {
|
---|
100 | if (::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwLastError, 0, pszMsg, dwBufSize / sizeof(TCHAR), NULL) == 0)
|
---|
101 | {
|
---|
102 | _sntprintf(pszMsg, dwBufSize / sizeof(TCHAR), _T("Unknown error!\n"), dwLastError);
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 | else
|
---|
106 | {
|
---|
107 | _TCHAR *p = _tcschr(pszMsg, _T('\r'));
|
---|
108 | if (p != NULL)
|
---|
109 | *p = _T('\0');
|
---|
110 | }
|
---|
111 |
|
---|
112 | return true;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Log callback for DIFxAPI calls.
|
---|
117 | *
|
---|
118 | * @param Event The event's structure to log.
|
---|
119 | * @param dwError The event's error level.
|
---|
120 | * @param pEventDescription The event's text description.
|
---|
121 | * @param pCallbackContext User-supplied callback context.
|
---|
122 | */
|
---|
123 | void LogCallback(DIFXAPI_LOG Event, DWORD dwError, PCWSTR pEventDescription, PVOID pCallbackContext)
|
---|
124 | {
|
---|
125 | if (dwError == 0)
|
---|
126 | _tprintf(_T("(%u) %ws\n"), Event, pEventDescription);
|
---|
127 | else
|
---|
128 | _tprintf(_T("(%u) ERROR: %u - %ws\n"), Event, dwError, pEventDescription);
|
---|
129 |
|
---|
130 | if (pCallbackContext)
|
---|
131 | fwprintf((FILE*)pCallbackContext, _T("(%u) %u - %s\n"), Event, dwError, pEventDescription);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * (Un)Installs a driver from/to the system.
|
---|
136 | *
|
---|
137 | * @return Exit code (EXIT_OK, EXIT_FAIL)
|
---|
138 | * @param fInstall Flag indicating whether to install (TRUE) or uninstall (FALSE) a driver.
|
---|
139 | * @param pszDriverPath Pointer to full qualified path to the driver's .INF file (+ driver files).
|
---|
140 | * @param fSilent Flag indicating a silent installation (TRUE) or not (FALSE).
|
---|
141 | * @param pszLogFile Pointer to full qualified path to log file to be written during installation.
|
---|
142 | * Optional.
|
---|
143 | */
|
---|
144 | int VBoxInstallDriver(const BOOL fInstall, const _TCHAR *pszDriverPath, BOOL fSilent,
|
---|
145 | const _TCHAR *pszLogFile)
|
---|
146 | {
|
---|
147 | HRESULT hr = S_OK;
|
---|
148 | HMODULE hDIFxAPI = LoadLibrary(_T("DIFxAPI.dll"));
|
---|
149 | if (NULL == hDIFxAPI)
|
---|
150 | {
|
---|
151 | _tprintf(_T("ERROR: Unable to locate DIFxAPI.dll!\n"));
|
---|
152 | hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | if (fInstall)
|
---|
157 | {
|
---|
158 | g_pfnDriverPackageInstall = (fnDriverPackageInstall)GetProcAddress(hDIFxAPI, "DriverPackageInstallW");
|
---|
159 | if (g_pfnDriverPackageInstall == NULL)
|
---|
160 | {
|
---|
161 | _tprintf(_T("ERROR: Unable to retrieve entry point for DriverPackageInstallW!\n"));
|
---|
162 | hr = HRESULT_FROM_WIN32(ERROR_PROC_NOT_FOUND);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | else
|
---|
166 | {
|
---|
167 | g_pfnDriverPackageUninstall = (fnDriverPackageUninstall)GetProcAddress(hDIFxAPI, "DriverPackageUninstallW");
|
---|
168 | if (g_pfnDriverPackageUninstall == NULL)
|
---|
169 | {
|
---|
170 | _tprintf(_T("ERROR: Unable to retrieve entry point for DriverPackageUninstallW!\n"));
|
---|
171 | hr = HRESULT_FROM_WIN32(ERROR_PROC_NOT_FOUND);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (SUCCEEDED(hr))
|
---|
176 | {
|
---|
177 | g_pfnDIFXAPISetLogCallback = (fnDIFXAPISetLogCallback)GetProcAddress(hDIFxAPI, "DIFXAPISetLogCallbackW");
|
---|
178 | if (g_pfnDIFXAPISetLogCallback == NULL)
|
---|
179 | {
|
---|
180 | _tprintf(_T("ERROR: Unable to retrieve entry point for DIFXAPISetLogCallbackW!\n"));
|
---|
181 | hr = HRESULT_FROM_WIN32(ERROR_PROC_NOT_FOUND);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (SUCCEEDED(hr))
|
---|
187 | {
|
---|
188 | FILE *phFile = NULL;
|
---|
189 | if (pszLogFile)
|
---|
190 | {
|
---|
191 | phFile = _wfopen(pszLogFile, _T("a"));
|
---|
192 | if (!phFile)
|
---|
193 | _tprintf(_T("ERROR: Unable to create log file!\n"));
|
---|
194 | g_pfnDIFXAPISetLogCallback(LogCallback, phFile);
|
---|
195 | }
|
---|
196 |
|
---|
197 | INSTALLERINFO instInfo =
|
---|
198 | {
|
---|
199 | TEXT("{7d2c708d-c202-40ab-b3e8-de21da1dc629}"), /* Our GUID for representing this installation tool. */
|
---|
200 | TEXT("VirtualBox Guest Additions Install Helper"),
|
---|
201 | TEXT("VirtualBox Guest Additions"), /** @todo Add version! */
|
---|
202 | TEXT("Oracle Corporation")
|
---|
203 | };
|
---|
204 |
|
---|
205 | _TCHAR szDriverInf[MAX_PATH + 1];
|
---|
206 | if (0 == GetFullPathNameW(pszDriverPath, MAX_PATH, szDriverInf, NULL))
|
---|
207 | {
|
---|
208 | _tprintf(_T("ERROR: INF-Path too long / could not be retrieved!\n"));
|
---|
209 | hr = HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
|
---|
210 | }
|
---|
211 | else
|
---|
212 | {
|
---|
213 | if (fInstall)
|
---|
214 | _tprintf(_T("Installing driver ...\n"));
|
---|
215 | else
|
---|
216 | _tprintf(_T("Uninstalling driver ...\n"));
|
---|
217 | _tprintf(_T("INF-File: %ws\n"), szDriverInf);
|
---|
218 |
|
---|
219 | DWORD dwFlags = DRIVER_PACKAGE_FORCE;
|
---|
220 | if (!fInstall)
|
---|
221 | dwFlags |= DRIVER_PACKAGE_DELETE_FILES;
|
---|
222 |
|
---|
223 | OSVERSIONINFO osi;
|
---|
224 | memset(&osi, 0, sizeof(OSVERSIONINFO));
|
---|
225 | osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
---|
226 | if ( (GetVersionEx(&osi) != 0)
|
---|
227 | && (osi.dwPlatformId == VER_PLATFORM_WIN32_NT)
|
---|
228 | && (osi.dwMajorVersion < 6))
|
---|
229 | {
|
---|
230 | if (fInstall)
|
---|
231 | {
|
---|
232 | _tprintf(_T("Using legacy mode for install ...\n"));
|
---|
233 | dwFlags |= DRIVER_PACKAGE_LEGACY_MODE;
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | if (fSilent)
|
---|
238 | {
|
---|
239 | _tprintf(_T("Installation is silent ...\n"));
|
---|
240 | dwFlags |= DRIVER_PACKAGE_SILENT;
|
---|
241 | }
|
---|
242 |
|
---|
243 | BOOL fReboot;
|
---|
244 | DWORD dwRet = fInstall ?
|
---|
245 | g_pfnDriverPackageInstall(szDriverInf, dwFlags, &instInfo, &fReboot)
|
---|
246 | : g_pfnDriverPackageUninstall(szDriverInf, dwFlags, &instInfo, &fReboot);
|
---|
247 | if (dwRet != ERROR_SUCCESS)
|
---|
248 | {
|
---|
249 | switch (dwRet)
|
---|
250 | {
|
---|
251 | case CRYPT_E_FILE_ERROR:
|
---|
252 | _tprintf(_T("ERROR: The catalog file for the specified driver package was not found!\n"));
|
---|
253 | break;
|
---|
254 |
|
---|
255 | case ERROR_ACCESS_DENIED:
|
---|
256 | _tprintf(_T("ERROR: Caller is not in Administrators group to (un)install this driver package!\n"));
|
---|
257 | break;
|
---|
258 |
|
---|
259 | case ERROR_BAD_ENVIRONMENT:
|
---|
260 | _tprintf(_T("ERROR: The current Microsoft Windows version does not support this operation!\n"));
|
---|
261 | break;
|
---|
262 |
|
---|
263 | case ERROR_CANT_ACCESS_FILE:
|
---|
264 | _tprintf(_T("ERROR: The driver package files could not be accessed!\n"));
|
---|
265 | break;
|
---|
266 |
|
---|
267 | case ERROR_DEPENDENT_APPLICATIONS_EXIST:
|
---|
268 | _tprintf(_T("ERROR: DriverPackageUninstall removed an association between the driver package and the specified application but the function did not uninstall the driver package because other applications are associated with the driver package!\n"));
|
---|
269 | break;
|
---|
270 |
|
---|
271 | case ERROR_DRIVER_PACKAGE_NOT_IN_STORE:
|
---|
272 | _tprintf(_T("ERROR: There is no INF file in the DIFx driver store that corresponds to the INF file %ws!\n"), szDriverInf);
|
---|
273 | break;
|
---|
274 |
|
---|
275 | case ERROR_FILE_NOT_FOUND:
|
---|
276 | _tprintf(_T("ERROR: File not found! File = %ws\n"), szDriverInf);
|
---|
277 | break;
|
---|
278 |
|
---|
279 | case ERROR_IN_WOW64:
|
---|
280 | _tprintf(_T("ERROR: The calling application is a 32-bit application attempting to execute in a 64-bit environment, which is not allowed!\n"));
|
---|
281 | break;
|
---|
282 |
|
---|
283 | case ERROR_INVALID_FLAGS:
|
---|
284 | _tprintf(_T("ERROR: The flags specified are invalid!\n"));
|
---|
285 | break;
|
---|
286 |
|
---|
287 | case ERROR_INSTALL_FAILURE:
|
---|
288 | _tprintf(_T("ERROR: The (un)install operation failed! Consult the Setup API logs for more information.\n"));
|
---|
289 | break;
|
---|
290 |
|
---|
291 | case ERROR_NO_MORE_ITEMS:
|
---|
292 | _tprintf(
|
---|
293 | _T(
|
---|
294 | "ERROR: The function found a match for the HardwareId value, but the specified driver was not a better match than the current driver and the caller did not specify the INSTALLFLAG_FORCE flag!\n"));
|
---|
295 | break;
|
---|
296 |
|
---|
297 | case ERROR_NO_DRIVER_SELECTED:
|
---|
298 | _tprintf(_T("ERROR: No driver in .INF-file selected!\n"));
|
---|
299 | break;
|
---|
300 |
|
---|
301 | case ERROR_SECTION_NOT_FOUND:
|
---|
302 | _tprintf(_T("ERROR: Section in .INF-file was not found!\n"));
|
---|
303 | break;
|
---|
304 |
|
---|
305 | case ERROR_SHARING_VIOLATION:
|
---|
306 | _tprintf(_T("ERROR: A component of the driver package in the DIFx driver store is locked by a thread or process\n"));
|
---|
307 | break;
|
---|
308 |
|
---|
309 | /*
|
---|
310 | * ! sig: Verifying file against specific Authenticode(tm) catalog failed! (0x800b0109)
|
---|
311 | * ! sig: Error 0x800b0109: A certificate chain processed, but terminated in a root certificate which is not trusted by the trust provider.
|
---|
312 | * !!! sto: No error message will be displayed as client is running in non-interactive mode.
|
---|
313 | * !!! ndv: Driver package failed signature validation. Error = 0xE0000247
|
---|
314 | */
|
---|
315 | case ERROR_DRIVER_STORE_ADD_FAILED:
|
---|
316 | _tprintf(_T("ERROR: Adding driver to the driver store failed!!\n"));
|
---|
317 | break;
|
---|
318 |
|
---|
319 | case ERROR_UNSUPPORTED_TYPE:
|
---|
320 | _tprintf(_T("ERROR: The driver package type is not supported of INF %ws!\n"), szDriverInf);
|
---|
321 | break;
|
---|
322 |
|
---|
323 | default:
|
---|
324 | {
|
---|
325 | /* Try error lookup with GetErrorMsg(). */
|
---|
326 | TCHAR szErrMsg[1024];
|
---|
327 | GetErrorMsg(dwRet, szErrMsg, sizeof(szErrMsg));
|
---|
328 | _tprintf(_T("ERROR (%08x): %ws\n"), dwRet, szErrMsg);
|
---|
329 | break;
|
---|
330 | }
|
---|
331 | }
|
---|
332 | hr = HRESULT_FROM_WIN32(dwRet);
|
---|
333 | }
|
---|
334 | g_pfnDIFXAPISetLogCallback(NULL, NULL);
|
---|
335 | if (phFile)
|
---|
336 | fclose(phFile);
|
---|
337 | if (SUCCEEDED(hr))
|
---|
338 | {
|
---|
339 | _tprintf(_T("Driver was installed successfully!\n"));
|
---|
340 | if (fReboot)
|
---|
341 | _tprintf(_T("A reboot is needed to complete the driver (un)installation!\n"));
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | if (NULL != hDIFxAPI)
|
---|
347 | FreeLibrary(hDIFxAPI);
|
---|
348 |
|
---|
349 | return SUCCEEDED(hr) ? EXIT_OK : EXIT_FAIL;
|
---|
350 | }
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Executes a sepcified .INF section to install/uninstall drivers and/or services.
|
---|
354 | *
|
---|
355 | * @return Exit code (EXIT_OK, EXIT_FAIL)
|
---|
356 | * @param pszSection Section to execute; usually it's "DefaultInstall".
|
---|
357 | * @param iMode Execution mode to use (see MSDN).
|
---|
358 | * @param pszInf Full qualified path of the .INF file to use.
|
---|
359 | */
|
---|
360 | int ExecuteInfFile(const _TCHAR *pszSection, int iMode, const _TCHAR *pszInf)
|
---|
361 | {
|
---|
362 | _tprintf(_T("Executing INF-File: %ws (Section: %ws) ...\n"), pszInf, pszSection);
|
---|
363 |
|
---|
364 | /* Executed by the installer that already has proper privileges. */
|
---|
365 | _TCHAR szCommandLine[_MAX_PATH + 1] = { 0 };
|
---|
366 | swprintf(szCommandLine, sizeof(szCommandLine), TEXT( "%ws %d %ws" ), pszSection, iMode, pszInf);
|
---|
367 |
|
---|
368 | #ifdef DEBUG
|
---|
369 | _tprintf (_T( "Commandline: %ws\n"), szCommandLine);
|
---|
370 | #endif
|
---|
371 |
|
---|
372 | InstallHinfSection(NULL, NULL, szCommandLine, SW_SHOW);
|
---|
373 | /* No return value given! */
|
---|
374 |
|
---|
375 | return EXIT_OK;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Adds a string entry to a MULTI_SZ registry list.
|
---|
380 | *
|
---|
381 | * @return Exit code (EXIT_OK, EXIT_FAIL)
|
---|
382 | * @param pszSubKey Sub key containing the list.
|
---|
383 | * @param pszKeyValue The actual key name of the list.
|
---|
384 | * @param pszValueToRemove The value to add to the list.
|
---|
385 | * @param uiOrder Position (zero-based) of where to add the value to the list.
|
---|
386 | */
|
---|
387 | int RegistryAddStringToMultiSZ(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToAdd, unsigned int uiOrder)
|
---|
388 | {
|
---|
389 | #ifdef DEBUG
|
---|
390 | _tprintf(_T("AddStringToMultiSZ: Adding MULTI_SZ string %ws to %ws\\%ws (Order = %d)\n"), pszValueToAdd, pszSubKey, pszKeyValue, uiOrder);
|
---|
391 | #endif
|
---|
392 |
|
---|
393 | HKEY hKey = NULL;
|
---|
394 | DWORD disp, dwType;
|
---|
395 | LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, &disp);
|
---|
396 | if (lRet != ERROR_SUCCESS)
|
---|
397 | _tprintf(_T("AddStringToMultiSZ: RegCreateKeyEx %ts failed with error %ld!\n"), pszSubKey, lRet);
|
---|
398 |
|
---|
399 | if (lRet == ERROR_SUCCESS)
|
---|
400 | {
|
---|
401 | TCHAR szKeyValue[512] = { 0 };
|
---|
402 | TCHAR szNewKeyValue[512] = { 0 };
|
---|
403 | DWORD cbKeyValue = sizeof(szKeyValue);
|
---|
404 |
|
---|
405 | lRet = RegQueryValueEx(hKey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
|
---|
406 | if ( lRet != ERROR_SUCCESS
|
---|
407 | || dwType != REG_MULTI_SZ)
|
---|
408 | {
|
---|
409 | _tprintf(_T("AddStringToMultiSZ: RegQueryValueEx failed with error %ld, key type = 0x%x!\n"), lRet, dwType);
|
---|
410 | }
|
---|
411 | else
|
---|
412 | {
|
---|
413 |
|
---|
414 | /* Look if the network provider is already in the list. */
|
---|
415 | int iPos = 0;
|
---|
416 | size_t cb = 0;
|
---|
417 |
|
---|
418 | /* Replace delimiting "\0"'s with "," to make tokenizing work. */
|
---|
419 | for (int i = 0; i < cbKeyValue / sizeof(TCHAR); i++)
|
---|
420 | if (szKeyValue[i] == '\0') szKeyValue[i] = ',';
|
---|
421 |
|
---|
422 | TCHAR *pszToken = wcstok(szKeyValue, _T(","));
|
---|
423 | TCHAR *pszNewToken = NULL;
|
---|
424 | TCHAR *pNewKeyValuePos = szNewKeyValue;
|
---|
425 | while (pszToken != NULL)
|
---|
426 | {
|
---|
427 | pszNewToken = wcstok(NULL, _T(","));
|
---|
428 |
|
---|
429 | /* Append new value (at beginning if iOrder=0). */
|
---|
430 | if (iPos == uiOrder)
|
---|
431 | {
|
---|
432 | memcpy(pNewKeyValuePos, pszValueToAdd, wcslen(pszValueToAdd)*sizeof(TCHAR));
|
---|
433 |
|
---|
434 | cb += (wcslen(pszValueToAdd) + 1) * sizeof(TCHAR); /* Add trailing zero as well. */
|
---|
435 | pNewKeyValuePos += wcslen(pszValueToAdd) + 1;
|
---|
436 | iPos++;
|
---|
437 | }
|
---|
438 |
|
---|
439 | if (0 != wcsicmp(pszToken, pszValueToAdd))
|
---|
440 | {
|
---|
441 | memcpy(pNewKeyValuePos, pszToken, wcslen(pszToken)*sizeof(TCHAR));
|
---|
442 | cb += (wcslen(pszToken) + 1) * sizeof(TCHAR); /* Add trailing zero as well. */
|
---|
443 | pNewKeyValuePos += wcslen(pszToken) + 1;
|
---|
444 | iPos++;
|
---|
445 | }
|
---|
446 |
|
---|
447 | pszToken = pszNewToken;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /* Append as last item if needed. */
|
---|
451 | if (uiOrder >= iPos)
|
---|
452 | {
|
---|
453 | memcpy(pNewKeyValuePos, pszValueToAdd, wcslen(pszValueToAdd)*sizeof(TCHAR));
|
---|
454 | cb += wcslen(pszValueToAdd) * sizeof(TCHAR); /* Add trailing zero as well. */
|
---|
455 | }
|
---|
456 |
|
---|
457 | lRet = RegSetValueExW(hKey, pszKeyValue, 0, REG_MULTI_SZ, (LPBYTE)szNewKeyValue, (DWORD)cb);
|
---|
458 | if (lRet != ERROR_SUCCESS)
|
---|
459 | _tprintf(_T("AddStringToMultiSZ: RegSetValueEx failed with error %ld!\n"), lRet);
|
---|
460 | }
|
---|
461 |
|
---|
462 | RegCloseKey(hKey);
|
---|
463 | #ifdef DEBUG
|
---|
464 | if (lRet == ERROR_SUCCESS)
|
---|
465 | _tprintf(_T("AddStringToMultiSZ: Value %ws successfully written!\n"), pszValueToAdd);
|
---|
466 | #endif
|
---|
467 | }
|
---|
468 |
|
---|
469 | return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
|
---|
470 | }
|
---|
471 |
|
---|
472 | /**
|
---|
473 | * Removes a string entry from a MULTI_SZ registry list.
|
---|
474 | *
|
---|
475 | * @return Exit code (EXIT_OK, EXIT_FAIL)
|
---|
476 | * @param pszSubKey Sub key containing the list.
|
---|
477 | * @param pszKeyValue The actual key name of the list.
|
---|
478 | * @param pszValueToRemove The value to remove from the list.
|
---|
479 | */
|
---|
480 | int RegistryRemoveStringFromMultiSZ(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToRemove)
|
---|
481 | {
|
---|
482 | // @todo Make string sizes dynamically allocated!
|
---|
483 |
|
---|
484 | const TCHAR *pszKey = pszSubKey;
|
---|
485 | #ifdef DEBUG
|
---|
486 | _tprintf(_T("RemoveStringFromMultiSZ: Removing MULTI_SZ string: %ws from %ws\\%ws ...\n"), pszValueToRemove, pszSubKey, pszKeyValue);
|
---|
487 | #endif
|
---|
488 |
|
---|
489 | HKEY hkey;
|
---|
490 | DWORD disp, dwType;
|
---|
491 | LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hkey, &disp);
|
---|
492 | if (lRet != ERROR_SUCCESS)
|
---|
493 | _tprintf(_T("RemoveStringFromMultiSZ: RegCreateKeyEx %ts failed with error %ld!\n"), pszKey, lRet);
|
---|
494 |
|
---|
495 | if (lRet == ERROR_SUCCESS)
|
---|
496 | {
|
---|
497 | TCHAR szKeyValue[1024];
|
---|
498 | DWORD cbKeyValue = sizeof(szKeyValue);
|
---|
499 |
|
---|
500 | lRet = RegQueryValueEx(hkey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
|
---|
501 | if ( lRet != ERROR_SUCCESS
|
---|
502 | || dwType != REG_MULTI_SZ)
|
---|
503 | {
|
---|
504 | _tprintf(_T("RemoveStringFromMultiSZ: RegQueryValueEx failed with %d, key type = 0x%x!\n"), lRet, dwType);
|
---|
505 | }
|
---|
506 | else
|
---|
507 | {
|
---|
508 | #ifdef DEBUG
|
---|
509 | _tprintf(_T("RemoveStringFromMultiSZ: Current key len: %ld\n"), cbKeyValue);
|
---|
510 | #endif
|
---|
511 |
|
---|
512 | TCHAR szCurString[1024] = { 0 };
|
---|
513 | TCHAR szFinalString[1024] = { 0 };
|
---|
514 | int iIndex = 0;
|
---|
515 | int iNewIndex = 0;
|
---|
516 | for (int i = 0; i < cbKeyValue / sizeof(TCHAR); i++)
|
---|
517 | {
|
---|
518 | if (szKeyValue[i] != _T('\0'))
|
---|
519 | szCurString[iIndex++] = szKeyValue[i];
|
---|
520 |
|
---|
521 | if ( (!szKeyValue[i] == _T('\0'))
|
---|
522 | && (szKeyValue[i + 1] == _T('\0')))
|
---|
523 | {
|
---|
524 | if (NULL == wcsstr(szCurString, pszValueToRemove))
|
---|
525 | {
|
---|
526 | wcscat(&szFinalString[iNewIndex], szCurString);
|
---|
527 |
|
---|
528 | if (iNewIndex == 0)
|
---|
529 | iNewIndex = iIndex;
|
---|
530 | else iNewIndex += iIndex;
|
---|
531 |
|
---|
532 | szFinalString[++iNewIndex] = _T('\0');
|
---|
533 | }
|
---|
534 |
|
---|
535 | iIndex = 0;
|
---|
536 | ZeroMemory(szCurString, sizeof(szCurString));
|
---|
537 | }
|
---|
538 | }
|
---|
539 | szFinalString[++iNewIndex] = _T('\0');
|
---|
540 | #ifdef DEBUG
|
---|
541 | _tprintf(_T("RemoveStringFromMultiSZ: New key value: %ws (%u bytes)\n"),
|
---|
542 | szFinalString, iNewIndex * sizeof(TCHAR));
|
---|
543 | #endif
|
---|
544 |
|
---|
545 | lRet = RegSetValueExW(hkey, pszKeyValue, 0, REG_MULTI_SZ, (LPBYTE)szFinalString, iNewIndex * sizeof(TCHAR));
|
---|
546 | if (lRet != ERROR_SUCCESS)
|
---|
547 | _tprintf(_T("RemoveStringFromMultiSZ: RegSetValueEx failed with %d!\n"), lRet);
|
---|
548 | }
|
---|
549 |
|
---|
550 | RegCloseKey(hkey);
|
---|
551 | #ifdef DEBUG
|
---|
552 | if (lRet == ERROR_SUCCESS)
|
---|
553 | _tprintf(_T("RemoveStringFromMultiSZ: Value %ws successfully removed!\n"), pszValueToRemove);
|
---|
554 | #endif
|
---|
555 | }
|
---|
556 |
|
---|
557 | return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
|
---|
558 | }
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * Adds a string to a registry string list (STRING_SZ).
|
---|
562 | * Only operates in HKLM for now, needs to be extended later for
|
---|
563 | * using other hives. Only processes lists with a "," separator
|
---|
564 | * at the moment.
|
---|
565 | *
|
---|
566 | * @return Exit code (EXIT_OK, EXIT_FAIL)
|
---|
567 | * @param pszSubKey Sub key containing the list.
|
---|
568 | * @param pszKeyValue The actual key name of the list.
|
---|
569 | * @param pszValueToAdd The value to add to the list.
|
---|
570 | * @param uiOrder Position (zero-based) of where to add the value to the list.
|
---|
571 | * @param dwFlags Flags.
|
---|
572 | */
|
---|
573 | int RegistryAddStringToList(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToAdd,
|
---|
574 | unsigned int uiOrder, DWORD dwFlags)
|
---|
575 | {
|
---|
576 | HKEY hKey = NULL;
|
---|
577 | DWORD disp, dwType;
|
---|
578 | LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, &disp);
|
---|
579 | if (lRet != ERROR_SUCCESS)
|
---|
580 | _tprintf(_T("RegistryAddStringToList: RegCreateKeyEx %ts failed with error %ld!\n"), pszSubKey, lRet);
|
---|
581 |
|
---|
582 | TCHAR szKeyValue[512] = { 0 };
|
---|
583 | TCHAR szNewKeyValue[512] = { 0 };
|
---|
584 | DWORD cbKeyValue = sizeof(szKeyValue);
|
---|
585 |
|
---|
586 | lRet = RegQueryValueEx(hKey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
|
---|
587 | if ( lRet != ERROR_SUCCESS
|
---|
588 | || dwType != REG_SZ)
|
---|
589 | {
|
---|
590 | _tprintf(_T("RegistryAddStringToList: RegQueryValueEx failed with %d, key type = 0x%x!\n"), lRet, dwType);
|
---|
591 | }
|
---|
592 |
|
---|
593 | if (lRet == ERROR_SUCCESS)
|
---|
594 | {
|
---|
595 | #ifdef DEBUG
|
---|
596 | _tprintf(_T("RegistryAddStringToList: Key value: %ws\n"), szKeyValue);
|
---|
597 | #endif
|
---|
598 |
|
---|
599 | /* Create entire new list. */
|
---|
600 | unsigned int iPos = 0;
|
---|
601 | TCHAR *pszToken = wcstok(szKeyValue, _T(","));
|
---|
602 | TCHAR *pszNewToken = NULL;
|
---|
603 | while (pszToken != NULL)
|
---|
604 | {
|
---|
605 | pszNewToken = wcstok(NULL, _T(","));
|
---|
606 |
|
---|
607 | /* Append new provider name (at beginning if iOrder=0). */
|
---|
608 | if (iPos == uiOrder)
|
---|
609 | {
|
---|
610 | wcscat(szNewKeyValue, pszValueToAdd);
|
---|
611 | wcscat(szNewKeyValue, _T(","));
|
---|
612 | iPos++;
|
---|
613 | }
|
---|
614 |
|
---|
615 | BOOL fAddToList = FALSE;
|
---|
616 | if ( !wcsicmp(pszToken, pszValueToAdd)
|
---|
617 | && (dwFlags & VBOX_REG_STRINGLIST_ALLOW_DUPLICATES))
|
---|
618 | fAddToList = TRUE;
|
---|
619 | else if (wcsicmp(pszToken, pszValueToAdd))
|
---|
620 | fAddToList = TRUE;
|
---|
621 |
|
---|
622 | if (fAddToList)
|
---|
623 | {
|
---|
624 | wcscat(szNewKeyValue, pszToken);
|
---|
625 | wcscat(szNewKeyValue, _T(","));
|
---|
626 | iPos++;
|
---|
627 | }
|
---|
628 |
|
---|
629 | #ifdef DEBUG
|
---|
630 | _tprintf (_T("RegistryAddStringToList: Temp new key value: %ws\n"), szNewKeyValue);
|
---|
631 | #endif
|
---|
632 | pszToken = pszNewToken;
|
---|
633 | }
|
---|
634 |
|
---|
635 | /* Append as last item if needed. */
|
---|
636 | if (uiOrder >= iPos)
|
---|
637 | wcscat(szNewKeyValue, pszValueToAdd);
|
---|
638 |
|
---|
639 | /* Last char a delimiter? Cut off ... */
|
---|
640 | if (szNewKeyValue[wcslen(szNewKeyValue) - 1] == ',')
|
---|
641 | szNewKeyValue[wcslen(szNewKeyValue) - 1] = '\0';
|
---|
642 |
|
---|
643 | size_t iNewLen = (wcslen(szNewKeyValue) * sizeof(WCHAR)) + sizeof(WCHAR);
|
---|
644 |
|
---|
645 | #ifdef DEBUG
|
---|
646 | _tprintf(_T("RegistryAddStringToList: New provider list: %ws (%u bytes)\n"), szNewKeyValue, iNewLen);
|
---|
647 | #endif
|
---|
648 |
|
---|
649 | lRet = RegSetValueExW(hKey, pszKeyValue, 0, REG_SZ, (LPBYTE)szNewKeyValue, (DWORD)iNewLen);
|
---|
650 | if (lRet != ERROR_SUCCESS)
|
---|
651 | _tprintf(_T("RegistryAddStringToList: RegSetValueEx failed with %ld!\n"), lRet);
|
---|
652 | }
|
---|
653 |
|
---|
654 | RegCloseKey(hKey);
|
---|
655 | return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
|
---|
656 | }
|
---|
657 |
|
---|
658 | /**
|
---|
659 | * Removes a string from a registry string list (STRING_SZ).
|
---|
660 | * Only operates in HKLM for now, needs to be extended later for
|
---|
661 | * using other hives. Only processes lists with a "," separator
|
---|
662 | * at the moment.
|
---|
663 | *
|
---|
664 | * @return Exit code (EXIT_OK, EXIT_FAIL)
|
---|
665 | * @param pszSubKey Sub key containing the list.
|
---|
666 | * @param pszKeyValue The actual key name of the list.
|
---|
667 | * @param pszValueToRemove The value to remove from the list.
|
---|
668 | */
|
---|
669 | int RegistryRemoveStringFromList(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToRemove)
|
---|
670 | {
|
---|
671 | HKEY hKey = NULL;
|
---|
672 | DWORD disp, dwType;
|
---|
673 | LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, &disp);
|
---|
674 | if (lRet != ERROR_SUCCESS)
|
---|
675 | _tprintf(_T("RegistryRemoveStringFromList: RegCreateKeyEx %ts failed with error %ld!\n"), pszSubKey, lRet);
|
---|
676 |
|
---|
677 | TCHAR szKeyValue[512] = { 0 };
|
---|
678 | TCHAR szNewKeyValue[512] = { 0 };
|
---|
679 | DWORD cbKeyValue = sizeof(szKeyValue);
|
---|
680 |
|
---|
681 | lRet = RegQueryValueEx(hKey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
|
---|
682 | if ( lRet != ERROR_SUCCESS
|
---|
683 | || dwType != REG_SZ)
|
---|
684 | {
|
---|
685 | _tprintf(_T("RegistryRemoveStringFromList: RegQueryValueEx failed with %d, key type = 0x%x!\n"), lRet, dwType);
|
---|
686 | }
|
---|
687 |
|
---|
688 | if (lRet == ERROR_SUCCESS)
|
---|
689 | {
|
---|
690 | #ifdef DEBUG
|
---|
691 | _tprintf(_T("RegistryRemoveStringFromList: Key value: %ws\n"), szKeyValue);
|
---|
692 | #endif
|
---|
693 |
|
---|
694 | /* Create entire new list. */
|
---|
695 | int iPos = 0;
|
---|
696 |
|
---|
697 | TCHAR *pszToken = wcstok(szKeyValue, _T(","));
|
---|
698 | TCHAR *pszNewToken = NULL;
|
---|
699 | while (pszToken != NULL)
|
---|
700 | {
|
---|
701 | pszNewToken = wcstok(NULL, _T(","));
|
---|
702 |
|
---|
703 | /* Append all list values as long as it's not the
|
---|
704 | * value we want to remove. */
|
---|
705 | if (wcsicmp(pszToken, pszValueToRemove))
|
---|
706 | {
|
---|
707 | wcscat(szNewKeyValue, pszToken);
|
---|
708 | wcscat(szNewKeyValue, _T(","));
|
---|
709 | iPos++;
|
---|
710 | }
|
---|
711 |
|
---|
712 | #ifdef DEBUG
|
---|
713 | _tprintf (_T("RegistryRemoveStringFromList: Temp new key value: %ws\n"), szNewKeyValue);
|
---|
714 | #endif
|
---|
715 | pszToken = pszNewToken;
|
---|
716 | }
|
---|
717 |
|
---|
718 | /* Last char a delimiter? Cut off ... */
|
---|
719 | if (szNewKeyValue[wcslen(szNewKeyValue) - 1] == ',')
|
---|
720 | szNewKeyValue[wcslen(szNewKeyValue) - 1] = '\0';
|
---|
721 |
|
---|
722 | size_t iNewLen = (wcslen(szNewKeyValue) * sizeof(WCHAR)) + sizeof(WCHAR);
|
---|
723 |
|
---|
724 | #ifdef DEBUG
|
---|
725 | _tprintf(_T("RegistryRemoveStringFromList: New provider list: %ws (%u bytes)\n"), szNewKeyValue, iNewLen);
|
---|
726 | #endif
|
---|
727 |
|
---|
728 | lRet = RegSetValueExW(hKey, pszKeyValue, 0, REG_SZ, (LPBYTE)szNewKeyValue, (DWORD)iNewLen);
|
---|
729 | if (lRet != ERROR_SUCCESS)
|
---|
730 | _tprintf(_T("RegistryRemoveStringFromList: RegSetValueEx failed with %ld!\n"), lRet);
|
---|
731 | }
|
---|
732 |
|
---|
733 | RegCloseKey(hKey);
|
---|
734 | return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
|
---|
735 | }
|
---|
736 |
|
---|
737 | /**
|
---|
738 | * Adds a network provider with a specified order to the system.
|
---|
739 | *
|
---|
740 | * @return Exit code (EXIT_OK, EXIT_FAIL)
|
---|
741 | * @param pszProvider Name of network provider to add.
|
---|
742 | * @param uiOrder Position in list (zero-based) of where to add.
|
---|
743 | */
|
---|
744 | int AddNetworkProvider(const TCHAR *pszProvider, unsigned int uiOrder)
|
---|
745 | {
|
---|
746 | _tprintf(_T("Adding network provider \"%ws\" (Order = %u) ...\n"), pszProvider, uiOrder);
|
---|
747 | int rc = RegistryAddStringToList(_T("System\\CurrentControlSet\\Control\\NetworkProvider\\Order"),
|
---|
748 | _T("ProviderOrder"),
|
---|
749 | pszProvider, uiOrder, VBOX_REG_STRINGLIST_NONE /* No flags set */);
|
---|
750 | if (rc == EXIT_OK)
|
---|
751 | _tprintf(_T("Network provider successfully added!\n"));
|
---|
752 | return rc;
|
---|
753 | }
|
---|
754 |
|
---|
755 | /**
|
---|
756 | * Removes a network provider from the system.
|
---|
757 | *
|
---|
758 | * @return Exit code (EXIT_OK, EXIT_FAIL)
|
---|
759 | * @param pszProvider Name of network provider to remove.
|
---|
760 | */
|
---|
761 | int RemoveNetworkProvider(const TCHAR *pszProvider)
|
---|
762 | {
|
---|
763 | _tprintf(_T("Removing network provider \"%ws\" ...\n"), pszProvider);
|
---|
764 | int rc = RegistryRemoveStringFromList(_T("System\\CurrentControlSet\\Control\\NetworkProvider\\Order"),
|
---|
765 | _T("ProviderOrder"),
|
---|
766 | pszProvider);
|
---|
767 | if (rc == EXIT_OK)
|
---|
768 | _tprintf(_T("Network provider successfully removed!\n"));
|
---|
769 | return rc;
|
---|
770 | }
|
---|
771 |
|
---|
772 | int CreateService(const TCHAR *pszStartStopName,
|
---|
773 | const TCHAR *pszDisplayName,
|
---|
774 | int iServiceType,
|
---|
775 | int iStartType,
|
---|
776 | const TCHAR *pszBinPath,
|
---|
777 | const TCHAR *pszLoadOrderGroup,
|
---|
778 | const TCHAR *pszDependencies,
|
---|
779 | const TCHAR *pszLogonUser,
|
---|
780 | const TCHAR *pszLogonPassword)
|
---|
781 | {
|
---|
782 | int rc = ERROR_SUCCESS;
|
---|
783 |
|
---|
784 | _tprintf(_T("Installing service %ws (%ws) ...\n"), pszDisplayName, pszStartStopName);
|
---|
785 |
|
---|
786 | SC_HANDLE hSCManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);
|
---|
787 | if (hSCManager == NULL)
|
---|
788 | {
|
---|
789 | _tprintf(_T("Could not get handle to SCM! Error: %ld\n"), GetLastError());
|
---|
790 | return EXIT_FAIL;
|
---|
791 | }
|
---|
792 |
|
---|
793 | /* Fixup end of multistring. */
|
---|
794 | TCHAR szDepend[ _MAX_PATH ] = { 0 }; /* @todo Use dynamically allocated string here! */
|
---|
795 | if (pszDependencies != NULL)
|
---|
796 | {
|
---|
797 | _tcsnccpy (szDepend, pszDependencies, wcslen(pszDependencies));
|
---|
798 | DWORD len = (DWORD)wcslen (szDepend);
|
---|
799 | szDepend [len + 1] = 0;
|
---|
800 |
|
---|
801 | /* Replace comma separator on null separator. */
|
---|
802 | for (DWORD i = 0; i < len; i++)
|
---|
803 | {
|
---|
804 | if (',' == szDepend [i])
|
---|
805 | szDepend [i] = 0;
|
---|
806 | }
|
---|
807 | }
|
---|
808 |
|
---|
809 | DWORD dwTag = 0xDEADBEAF;
|
---|
810 | SC_HANDLE hService = CreateService (hSCManager, /* SCManager database handle. */
|
---|
811 | pszStartStopName, /* Name of service. */
|
---|
812 | pszDisplayName, /* Name to display. */
|
---|
813 | SERVICE_ALL_ACCESS, /* Desired access. */
|
---|
814 | iServiceType, /* Service type. */
|
---|
815 | iStartType, /* Start type. */
|
---|
816 | SERVICE_ERROR_NORMAL, /* Error control type. */
|
---|
817 | pszBinPath, /* Service's binary. */
|
---|
818 | pszLoadOrderGroup, /* Ordering group. */
|
---|
819 | (pszLoadOrderGroup != NULL) ? &dwTag : NULL, /* Tag identifier. */
|
---|
820 | (pszDependencies != NULL) ? szDepend : NULL, /* Dependencies. */
|
---|
821 | (pszLogonUser != NULL) ? pszLogonUser: NULL, /* Account. */
|
---|
822 | (pszLogonPassword != NULL) ? pszLogonPassword : NULL); /* Password. */
|
---|
823 | if (NULL == hService)
|
---|
824 | {
|
---|
825 | DWORD dwErr = GetLastError();
|
---|
826 | switch (dwErr)
|
---|
827 | {
|
---|
828 |
|
---|
829 | case ERROR_SERVICE_EXISTS:
|
---|
830 | {
|
---|
831 | _tprintf(_T("Service already exists. No installation required. Updating the service config.\n"));
|
---|
832 |
|
---|
833 | hService = OpenService (hSCManager, /* SCManager database handle. */
|
---|
834 | pszStartStopName, /* Name of service. */
|
---|
835 | SERVICE_ALL_ACCESS); /* Desired access. */
|
---|
836 | if (NULL == hService)
|
---|
837 | {
|
---|
838 | dwErr = GetLastError();
|
---|
839 | _tprintf(_T("Could not open service! Error: %ld\n"), dwErr);
|
---|
840 | }
|
---|
841 | else
|
---|
842 | {
|
---|
843 | BOOL fResult = ChangeServiceConfig (hService, /* Service handle. */
|
---|
844 | iServiceType, /* Service type. */
|
---|
845 | iStartType, /* Start type. */
|
---|
846 | SERVICE_ERROR_NORMAL, /* Error control type. */
|
---|
847 | pszBinPath, /* Service's binary. */
|
---|
848 | pszLoadOrderGroup, /* Ordering group. */
|
---|
849 | (pszLoadOrderGroup != NULL) ? &dwTag : NULL, /* Tag identifier. */
|
---|
850 | (pszDependencies != NULL) ? szDepend : NULL, /* Dependencies. */
|
---|
851 | (pszLogonUser != NULL) ? pszLogonUser: NULL, /* Account. */
|
---|
852 | (pszLogonPassword != NULL) ? pszLogonPassword : NULL, /* Password. */
|
---|
853 | pszDisplayName); /* Name to display. */
|
---|
854 | if (fResult)
|
---|
855 | _tprintf(_T("The service config has been successfully updated.\n"));
|
---|
856 | else
|
---|
857 | {
|
---|
858 | dwErr = GetLastError();
|
---|
859 | _tprintf(_T("Could not change service config! Error: %ld\n"), dwErr);
|
---|
860 | }
|
---|
861 | CloseServiceHandle(hService);
|
---|
862 | }
|
---|
863 |
|
---|
864 | /*
|
---|
865 | * This entire branch do not return an error to avoid installations failures,
|
---|
866 | * if updating service parameters. Better to have a running system with old
|
---|
867 | * parameters and the failure information in the installation log.
|
---|
868 | */
|
---|
869 | break;
|
---|
870 | }
|
---|
871 |
|
---|
872 | case ERROR_INVALID_PARAMETER:
|
---|
873 |
|
---|
874 | _tprintf(_T("Invalid parameter specified!\n"));
|
---|
875 | rc = EXIT_FAIL;
|
---|
876 | break;
|
---|
877 |
|
---|
878 | default:
|
---|
879 |
|
---|
880 | _tprintf(_T("Could not create service! Error: %ld\n"), dwErr);
|
---|
881 | rc = EXIT_FAIL;
|
---|
882 | break;
|
---|
883 | }
|
---|
884 |
|
---|
885 | if (rc == EXIT_FAIL)
|
---|
886 | goto cleanup;
|
---|
887 | }
|
---|
888 | else
|
---|
889 | {
|
---|
890 | CloseServiceHandle (hService);
|
---|
891 | _tprintf(_T("Installation of service successful!\n"));
|
---|
892 | }
|
---|
893 |
|
---|
894 | cleanup:
|
---|
895 |
|
---|
896 | if (hSCManager != NULL)
|
---|
897 | CloseServiceHandle (hSCManager);
|
---|
898 |
|
---|
899 | return rc;
|
---|
900 | }
|
---|
901 |
|
---|
902 | int DelService(const TCHAR *pszStartStopName)
|
---|
903 | {
|
---|
904 | int rc = ERROR_SUCCESS;
|
---|
905 |
|
---|
906 | _tprintf(_T("Deleting service '%ws' ...\n"), pszStartStopName);
|
---|
907 |
|
---|
908 | SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
|
---|
909 | SC_HANDLE hService = NULL;
|
---|
910 | if (hSCManager == NULL)
|
---|
911 | {
|
---|
912 | _tprintf(_T("Could not get handle to SCM! Error: %ld\n"), GetLastError());
|
---|
913 | rc = EXIT_FAIL;
|
---|
914 | }
|
---|
915 | else
|
---|
916 | {
|
---|
917 | hService = OpenService(hSCManager, pszStartStopName, SERVICE_ALL_ACCESS);
|
---|
918 | if (NULL == hService)
|
---|
919 | {
|
---|
920 | _tprintf(_T("Could not open service '%ws'! Error: %ld\n"), pszStartStopName, GetLastError());
|
---|
921 | rc = EXIT_FAIL;
|
---|
922 | }
|
---|
923 | }
|
---|
924 |
|
---|
925 | if (hService != NULL)
|
---|
926 | {
|
---|
927 | if (LockServiceDatabase(hSCManager))
|
---|
928 | {
|
---|
929 | if (FALSE == DeleteService(hService))
|
---|
930 | {
|
---|
931 | DWORD dwErr = GetLastError();
|
---|
932 | switch (dwErr)
|
---|
933 | {
|
---|
934 |
|
---|
935 | case ERROR_SERVICE_MARKED_FOR_DELETE:
|
---|
936 |
|
---|
937 | _tprintf(_T("Service '%ws' already marked for deletion.\n"), pszStartStopName);
|
---|
938 | break;
|
---|
939 |
|
---|
940 | default:
|
---|
941 |
|
---|
942 | _tprintf(_T("Could not delete service '%ws'! Error: %ld\n"), pszStartStopName, GetLastError());
|
---|
943 | rc = EXIT_FAIL;
|
---|
944 | break;
|
---|
945 | }
|
---|
946 | }
|
---|
947 | else
|
---|
948 | {
|
---|
949 | _tprintf(_T("Service '%ws' successfully removed!\n"), pszStartStopName);
|
---|
950 | }
|
---|
951 | UnlockServiceDatabase(hSCManager);
|
---|
952 | }
|
---|
953 | else
|
---|
954 | {
|
---|
955 | _tprintf(_T("Unable to lock service database! Error: %ld\n"), GetLastError());
|
---|
956 | rc = EXIT_FAIL;
|
---|
957 | }
|
---|
958 | CloseServiceHandle(hService);
|
---|
959 | }
|
---|
960 |
|
---|
961 | if (hSCManager != NULL)
|
---|
962 | CloseServiceHandle(hSCManager);
|
---|
963 |
|
---|
964 | return rc;
|
---|
965 | }
|
---|
966 |
|
---|
967 | DWORD RegistryWrite(HKEY hRootKey,
|
---|
968 | const _TCHAR *pszSubKey,
|
---|
969 | const _TCHAR *pszValueName,
|
---|
970 | DWORD dwType,
|
---|
971 | const BYTE *pbData,
|
---|
972 | DWORD cbData)
|
---|
973 | {
|
---|
974 | DWORD lRet;
|
---|
975 | HKEY hKey;
|
---|
976 | lRet = RegCreateKeyEx (hRootKey,
|
---|
977 | pszSubKey,
|
---|
978 | 0, /* Reserved */
|
---|
979 | NULL, /* lpClass [in, optional] */
|
---|
980 | 0, /* dwOptions [in] */
|
---|
981 | KEY_WRITE,
|
---|
982 | NULL, /* lpSecurityAttributes [in, optional] */
|
---|
983 | &hKey,
|
---|
984 | NULL); /* lpdwDisposition [out, optional] */
|
---|
985 | if (lRet != ERROR_SUCCESS)
|
---|
986 | {
|
---|
987 | _tprintf(_T("Could not open registry key! Error: %ld\n"), GetLastError());
|
---|
988 | }
|
---|
989 | else
|
---|
990 | {
|
---|
991 | lRet = RegSetValueEx(hKey, pszValueName, 0, dwType, (BYTE*)pbData, cbData);
|
---|
992 | if (lRet != ERROR_SUCCESS)
|
---|
993 | _tprintf(_T("Could not write to registry! Error: %ld\n"), GetLastError());
|
---|
994 | RegCloseKey(hKey);
|
---|
995 |
|
---|
996 | }
|
---|
997 | return lRet;
|
---|
998 | }
|
---|
999 |
|
---|
1000 | void PrintHelp(void)
|
---|
1001 | {
|
---|
1002 | _tprintf(_T("VirtualBox Guest Additions Installation Helper for Windows\n"));
|
---|
1003 | _tprintf(_T("Version: %d.%d.%d.%d\n\n"), VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
|
---|
1004 | _tprintf(_T("Syntax:\n"));
|
---|
1005 | _tprintf(_T("\n"));
|
---|
1006 | _tprintf(_T("Drivers:\n"));
|
---|
1007 | _tprintf(_T("\tVBoxDrvInst driver install <inf-file> [log file]\n"));
|
---|
1008 | _tprintf(_T("\tVBoxDrvInst driver uninstall <inf-file> [log file]\n"));
|
---|
1009 | _tprintf(_T("\tVBoxDrvInst driver executeinf <inf-file>\n"));
|
---|
1010 | _tprintf(_T("\n"));
|
---|
1011 | _tprintf(_T("Network Provider:\n"));
|
---|
1012 | _tprintf(_T("\tVBoxDrvInst netprovider add <name> [order]\n"));
|
---|
1013 | _tprintf(_T("\tVBoxDrvInst netprovider remove <name>\n"));
|
---|
1014 | _tprintf(_T("\n"));
|
---|
1015 | _tprintf(_T("Registry:\n"));
|
---|
1016 | _tprintf(_T("\tVBoxDrvInst registry write <root> <sub key>\n")
|
---|
1017 | _T("\t <key name> <key type> <value>\n")
|
---|
1018 | _T("\t [type] [size]\n"));
|
---|
1019 | _tprintf(_T("\tVBoxDrvInst registry addmultisz <root> <sub key>\n")
|
---|
1020 | _T("\t <value> [order]\n"));
|
---|
1021 | _tprintf(_T("\tVBoxDrvInst registry delmultisz <root> <sub key>\n")
|
---|
1022 | _T("\t <key name> <value to remove>\n"));
|
---|
1023 | /** @todo Add "service" category! */
|
---|
1024 | _tprintf(_T("\n"));
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | int __cdecl _tmain(int argc, _TCHAR *argv[])
|
---|
1028 | {
|
---|
1029 | int rc = EXIT_USAGE;
|
---|
1030 |
|
---|
1031 | OSVERSIONINFO OSinfo;
|
---|
1032 | OSinfo.dwOSVersionInfoSize = sizeof(OSinfo);
|
---|
1033 | GetVersionEx(&OSinfo);
|
---|
1034 |
|
---|
1035 | if (argc >= 2)
|
---|
1036 | {
|
---|
1037 | if ( !_tcsicmp(argv[1], _T("driver"))
|
---|
1038 | && argc >= 3)
|
---|
1039 | {
|
---|
1040 | _TCHAR szINF[_MAX_PATH] = { 0 }; /* Complete path to INF file.*/
|
---|
1041 | if ( ( !_tcsicmp(argv[2], _T("install"))
|
---|
1042 | || !_tcsicmp(argv[2], _T("uninstall")))
|
---|
1043 | && argc >= 4)
|
---|
1044 | {
|
---|
1045 | if (OSinfo.dwMajorVersion < 5)
|
---|
1046 | {
|
---|
1047 | _tprintf(_T("ERROR: Platform not supported for driver (un)installation!\n"));
|
---|
1048 | rc = EXIT_FAIL;
|
---|
1049 | }
|
---|
1050 | else
|
---|
1051 | {
|
---|
1052 | _sntprintf(szINF, sizeof(szINF) / sizeof(TCHAR), _T("%ws"), argv[3]);
|
---|
1053 |
|
---|
1054 | _TCHAR szLogFile[_MAX_PATH] = { 0 };
|
---|
1055 | if (argc > 4)
|
---|
1056 | _sntprintf(szLogFile, sizeof(szLogFile) / sizeof(TCHAR), _T("%ws"), argv[4]);
|
---|
1057 | rc = VBoxInstallDriver(!_tcsicmp(argv[2], _T("install")) ? TRUE : FALSE, szINF,
|
---|
1058 | FALSE /* Not silent */, szLogFile[0] != NULL ? szLogFile : NULL);
|
---|
1059 | }
|
---|
1060 | }
|
---|
1061 | else if ( !_tcsicmp(argv[2], _T("executeinf"))
|
---|
1062 | && argc == 4)
|
---|
1063 | {
|
---|
1064 | _sntprintf(szINF, sizeof(szINF) / sizeof(TCHAR), _T("%ws"), argv[3]);
|
---|
1065 | rc = ExecuteInfFile(_T("DefaultInstall"), 132, szINF);
|
---|
1066 | }
|
---|
1067 | }
|
---|
1068 | else if ( !_tcsicmp(argv[1], _T("netprovider"))
|
---|
1069 | && argc >= 3)
|
---|
1070 | {
|
---|
1071 | _TCHAR szProvider[_MAX_PATH] = { 0 }; /* The network provider name for the registry. */
|
---|
1072 | if ( !_tcsicmp(argv[2], _T("add"))
|
---|
1073 | && argc >= 4)
|
---|
1074 | {
|
---|
1075 | int iOrder = 0;
|
---|
1076 | if (argc > 4)
|
---|
1077 | iOrder = _ttoi(argv[4]);
|
---|
1078 | _sntprintf(szProvider, sizeof(szProvider) / sizeof(TCHAR), _T("%ws"), argv[3]);
|
---|
1079 | rc = AddNetworkProvider(szProvider, iOrder);
|
---|
1080 | }
|
---|
1081 | else if ( !_tcsicmp(argv[2], _T("remove"))
|
---|
1082 | && argc >= 4)
|
---|
1083 | {
|
---|
1084 | _sntprintf(szProvider, sizeof(szProvider) / sizeof(TCHAR), _T("%ws"), argv[3]);
|
---|
1085 | rc = RemoveNetworkProvider(szProvider);
|
---|
1086 | }
|
---|
1087 | }
|
---|
1088 | else if ( !_tcsicmp(argv[1], _T("service"))
|
---|
1089 | && argc >= 3)
|
---|
1090 | {
|
---|
1091 | if ( !_tcsicmp(argv[2], _T("create"))
|
---|
1092 | && argc >= 8)
|
---|
1093 | {
|
---|
1094 | rc = CreateService(argv[3],
|
---|
1095 | argv[4],
|
---|
1096 | _ttoi(argv[5]),
|
---|
1097 | _ttoi(argv[6]),
|
---|
1098 | argv[7],
|
---|
1099 | (argc > 8) ? argv[8] : NULL,
|
---|
1100 | (argc > 9) ? argv[9] : NULL,
|
---|
1101 | (argc > 10) ? argv[10] : NULL,
|
---|
1102 | (argc > 11) ? argv[11] : NULL);
|
---|
1103 | }
|
---|
1104 | else if ( !_tcsicmp(argv[2], _T("delete"))
|
---|
1105 | && argc == 4)
|
---|
1106 | {
|
---|
1107 | rc = DelService(argv[3]);
|
---|
1108 | }
|
---|
1109 | }
|
---|
1110 | else if ( !_tcsicmp(argv[1], _T("registry"))
|
---|
1111 | && argc >= 3)
|
---|
1112 | {
|
---|
1113 | /** @todo add a handleRegistry(argc, argv) method to keep things cleaner */
|
---|
1114 | if ( !_tcsicmp(argv[2], _T("addmultisz"))
|
---|
1115 | && argc == 7)
|
---|
1116 | {
|
---|
1117 | rc = RegistryAddStringToMultiSZ(argv[3], argv[4], argv[5], _ttoi(argv[6]));
|
---|
1118 | }
|
---|
1119 | else if ( !_tcsicmp(argv[2], _T("delmultisz"))
|
---|
1120 | && argc == 6)
|
---|
1121 | {
|
---|
1122 | rc = RegistryRemoveStringFromMultiSZ(argv[3], argv[4], argv[5]);
|
---|
1123 | }
|
---|
1124 | else if ( !_tcsicmp(argv[2], _T("write"))
|
---|
1125 | && argc >= 8)
|
---|
1126 | {
|
---|
1127 | HKEY hRootKey = HKEY_LOCAL_MACHINE; /** @todo needs to be expanded (argv[3]) */
|
---|
1128 | DWORD dwValSize;
|
---|
1129 | BYTE *pbVal = NULL;
|
---|
1130 | DWORD dwVal;
|
---|
1131 |
|
---|
1132 | if (argc > 8)
|
---|
1133 | {
|
---|
1134 | if (!_tcsicmp(argv[8], _T("dword")))
|
---|
1135 | {
|
---|
1136 | dwVal = _ttol(argv[7]);
|
---|
1137 | pbVal = (BYTE*)&dwVal;
|
---|
1138 | dwValSize = sizeof(DWORD);
|
---|
1139 | }
|
---|
1140 | }
|
---|
1141 | if (pbVal == NULL) /* By default interpret value as string */
|
---|
1142 | {
|
---|
1143 | pbVal = (BYTE*)argv[7];
|
---|
1144 | dwValSize = _tcslen(argv[7]);
|
---|
1145 | }
|
---|
1146 | if (argc > 9)
|
---|
1147 | dwValSize = _ttol(argv[9]); /* Get the size in bytes of the value we want to write */
|
---|
1148 | rc = RegistryWrite(hRootKey,
|
---|
1149 | argv[4], /* Sub key */
|
---|
1150 | argv[5], /* Value name */
|
---|
1151 | REG_BINARY, /** @todo needs to be expanded (argv[6]) */
|
---|
1152 | pbVal, /* The value itself */
|
---|
1153 | dwValSize); /* Size of the value */
|
---|
1154 | }
|
---|
1155 | #if 0
|
---|
1156 | else if (!_tcsicmp(argv[2], _T("read")))
|
---|
1157 | {
|
---|
1158 | }
|
---|
1159 | else if (!_tcsicmp(argv[2], _T("del")))
|
---|
1160 | {
|
---|
1161 | }
|
---|
1162 | #endif
|
---|
1163 | }
|
---|
1164 | else if (!_tcsicmp(argv[1], _T("--version")))
|
---|
1165 | {
|
---|
1166 | _tprintf(_T("%d.%d.%d.%d\n"), VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
|
---|
1167 | rc = EXIT_OK;
|
---|
1168 | }
|
---|
1169 | else if ( !_tcsicmp(argv[1], _T("--help"))
|
---|
1170 | || !_tcsicmp(argv[1], _T("/help"))
|
---|
1171 | || !_tcsicmp(argv[1], _T("/h"))
|
---|
1172 | || !_tcsicmp(argv[1], _T("/?")))
|
---|
1173 | {
|
---|
1174 | PrintHelp();
|
---|
1175 | rc = EXIT_OK;
|
---|
1176 | }
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | if (rc == EXIT_USAGE)
|
---|
1180 | _tprintf(_T("No or wrong parameters given! Please consult the help (\"--help\" or \"/?\") for more information.\n"));
|
---|
1181 |
|
---|
1182 | return rc;
|
---|
1183 | }
|
---|
1184 |
|
---|