VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Installer/VBoxDrvInst.cpp@ 44432

Last change on this file since 44432 was 44432, checked in by vboxsync, 12 years ago

Reverted r83294 - doesn't work with DefaultInstall.Service sections.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 45.7 KB
Line 
1/* $Id: VBoxDrvInst.cpp 44432 2013-01-28 16:11:29Z 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 */
44typedef struct {
45 PWSTR pApplicationId;
46 PWSTR pDisplayName;
47 PWSTR pProductName;
48 PWSTR pMfgName;
49} INSTALLERINFO, *PINSTALLERINFO;
50typedef const PINSTALLERINFO PCINSTALLERINFO;
51
52typedef enum {
53 DIFXAPI_SUCCESS,
54 DIFXAPI_INFO,
55 DIFXAPI_WARNING,
56 DIFXAPI_ERROR
57} DIFXAPI_LOG;
58
59typedef void (WINAPI * DIFXLOGCALLBACK_W)( DIFXAPI_LOG Event, DWORD Error, PCWSTR EventDescription, PVOID CallbackContext);
60typedef void ( __cdecl* DIFXAPILOGCALLBACK_W)( DIFXAPI_LOG Event, DWORD Error, PCWSTR EventDescription, PVOID CallbackContext);
61
62typedef DWORD (WINAPI *fnDriverPackageInstall) (PCTSTR DriverPackageInfPath, DWORD Flags, PCINSTALLERINFO pInstallerInfo, BOOL *pNeedReboot);
63fnDriverPackageInstall g_pfnDriverPackageInstall = NULL;
64
65typedef DWORD (WINAPI *fnDriverPackageUninstall) (PCTSTR DriverPackageInfPath, DWORD Flags, PCINSTALLERINFO pInstallerInfo, BOOL *pNeedReboot);
66fnDriverPackageUninstall g_pfnDriverPackageUninstall = NULL;
67
68typedef VOID (WINAPI *fnDIFXAPISetLogCallback) (DIFXAPILOGCALLBACK_W LogCallback, PVOID CallbackContext);
69fnDIFXAPISetLogCallback 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
98bool 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 */
123void 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 */
144int 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 /*
241 * Don't add DRIVER_PACKAGE_SILENT to dwFlags here, otherwise
242 * installation will fail because we (still) don't have WHQL certified
243 * drivers. See CERT_E_WRONG_USAGE on MSDN for more information.
244 */
245 }
246
247 BOOL fReboot;
248 DWORD dwRet = fInstall ?
249 g_pfnDriverPackageInstall(szDriverInf, dwFlags, &instInfo, &fReboot)
250 : g_pfnDriverPackageUninstall(szDriverInf, dwFlags, &instInfo, &fReboot);
251 if (dwRet != ERROR_SUCCESS)
252 {
253 switch (dwRet)
254 {
255 case CRYPT_E_FILE_ERROR:
256 _tprintf(_T("ERROR: The catalog file for the specified driver package was not found!\n"));
257 break;
258
259 case ERROR_ACCESS_DENIED:
260 _tprintf(_T("ERROR: Caller is not in Administrators group to (un)install this driver package!\n"));
261 break;
262
263 case ERROR_BAD_ENVIRONMENT:
264 _tprintf(_T("ERROR: The current Microsoft Windows version does not support this operation!\n"));
265 break;
266
267 case ERROR_CANT_ACCESS_FILE:
268 _tprintf(_T("ERROR: The driver package files could not be accessed!\n"));
269 break;
270
271 case ERROR_DEPENDENT_APPLICATIONS_EXIST:
272 _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"));
273 break;
274
275 case ERROR_DRIVER_PACKAGE_NOT_IN_STORE:
276 _tprintf(_T("ERROR: There is no INF file in the DIFx driver store that corresponds to the INF file %ws!\n"), szDriverInf);
277 break;
278
279 case ERROR_FILE_NOT_FOUND:
280 _tprintf(_T("ERROR: File not found! File = %ws\n"), szDriverInf);
281 break;
282
283 case ERROR_IN_WOW64:
284 _tprintf(_T("ERROR: The calling application is a 32-bit application attempting to execute in a 64-bit environment, which is not allowed!\n"));
285 break;
286
287 case ERROR_INVALID_FLAGS:
288 _tprintf(_T("ERROR: The flags specified are invalid!\n"));
289 break;
290
291 case ERROR_INSTALL_FAILURE:
292 _tprintf(_T("ERROR: The (un)install operation failed! Consult the Setup API logs for more information.\n"));
293 break;
294
295 case ERROR_NO_MORE_ITEMS:
296 _tprintf(
297 _T(
298 "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"));
299 break;
300
301 case ERROR_NO_DRIVER_SELECTED:
302 _tprintf(_T("ERROR: No driver in .INF-file selected!\n"));
303 break;
304
305 case ERROR_SECTION_NOT_FOUND:
306 _tprintf(_T("ERROR: Section in .INF-file was not found!\n"));
307 break;
308
309 case ERROR_SHARING_VIOLATION:
310 _tprintf(_T("ERROR: A component of the driver package in the DIFx driver store is locked by a thread or process\n"));
311 break;
312
313 /*
314 * ! sig: Verifying file against specific Authenticode(tm) catalog failed! (0x800b0109)
315 * ! sig: Error 0x800b0109: A certificate chain processed, but terminated in a root certificate which is not trusted by the trust provider.
316 * !!! sto: No error message will be displayed as client is running in non-interactive mode.
317 * !!! ndv: Driver package failed signature validation. Error = 0xE0000247
318 */
319 case ERROR_DRIVER_STORE_ADD_FAILED:
320 _tprintf(_T("ERROR: Adding driver to the driver store failed!!\n"));
321 break;
322
323 case ERROR_UNSUPPORTED_TYPE:
324 _tprintf(_T("ERROR: The driver package type is not supported of INF %ws!\n"), szDriverInf);
325 break;
326
327 default:
328 {
329 /* Try error lookup with GetErrorMsg(). */
330 TCHAR szErrMsg[1024];
331 GetErrorMsg(dwRet, szErrMsg, sizeof(szErrMsg));
332 _tprintf(_T("ERROR (%08x): %ws\n"), dwRet, szErrMsg);
333 break;
334 }
335 }
336 hr = HRESULT_FROM_WIN32(dwRet);
337 }
338 g_pfnDIFXAPISetLogCallback(NULL, NULL);
339 if (phFile)
340 fclose(phFile);
341 if (SUCCEEDED(hr))
342 {
343 _tprintf(_T("Driver was installed successfully!\n"));
344 if (fReboot)
345 _tprintf(_T("A reboot is needed to complete the driver (un)installation!\n"));
346 }
347 }
348 }
349
350 if (NULL != hDIFxAPI)
351 FreeLibrary(hDIFxAPI);
352
353 return SUCCEEDED(hr) ? EXIT_OK : EXIT_FAIL;
354}
355
356/**
357 * Executes a sepcified .INF section to install/uninstall drivers and/or services.
358 *
359 * @return Exit code (EXIT_OK, EXIT_FAIL)
360 * @param pszSection Section to execute; usually it's "DefaultInstall".
361 * @param iMode Execution mode to use (see MSDN).
362 * @param pszInf Full qualified path of the .INF file to use.
363 */
364int ExecuteInfFile(const _TCHAR *pszSection, int iMode, const _TCHAR *pszInf)
365{
366 _tprintf(_T("Executing INF-File: %ws (Section: %ws) ...\n"), pszInf, pszSection);
367
368 /* Executed by the installer that already has proper privileges. */
369 _TCHAR szCommandLine[_MAX_PATH + 1] = { 0 };
370 swprintf(szCommandLine, sizeof(szCommandLine), TEXT( "%ws %d %ws" ), pszSection, iMode, pszInf);
371
372#ifdef DEBUG
373 _tprintf (_T( "Commandline: %ws\n"), szCommandLine);
374#endif
375
376 InstallHinfSection(NULL, NULL, szCommandLine, SW_SHOW);
377 /* No return value given! */
378
379 return EXIT_OK;
380}
381
382/**
383 * Adds a string entry to a MULTI_SZ registry list.
384 *
385 * @return Exit code (EXIT_OK, EXIT_FAIL)
386 * @param pszSubKey Sub key containing the list.
387 * @param pszKeyValue The actual key name of the list.
388 * @param pszValueToRemove The value to add to the list.
389 * @param uiOrder Position (zero-based) of where to add the value to the list.
390 */
391int RegistryAddStringToMultiSZ(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToAdd, unsigned int uiOrder)
392{
393#ifdef DEBUG
394 _tprintf(_T("AddStringToMultiSZ: Adding MULTI_SZ string %ws to %ws\\%ws (Order = %d)\n"), pszValueToAdd, pszSubKey, pszKeyValue, uiOrder);
395#endif
396
397 HKEY hKey = NULL;
398 DWORD disp, dwType;
399 LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, &disp);
400 if (lRet != ERROR_SUCCESS)
401 _tprintf(_T("AddStringToMultiSZ: RegCreateKeyEx %ts failed with error %ld!\n"), pszSubKey, lRet);
402
403 if (lRet == ERROR_SUCCESS)
404 {
405 TCHAR szKeyValue[512] = { 0 };
406 TCHAR szNewKeyValue[512] = { 0 };
407 DWORD cbKeyValue = sizeof(szKeyValue);
408
409 lRet = RegQueryValueEx(hKey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
410 if ( lRet != ERROR_SUCCESS
411 || dwType != REG_MULTI_SZ)
412 {
413 _tprintf(_T("AddStringToMultiSZ: RegQueryValueEx failed with error %ld, key type = 0x%x!\n"), lRet, dwType);
414 }
415 else
416 {
417
418 /* Look if the network provider is already in the list. */
419 int iPos = 0;
420 size_t cb = 0;
421
422 /* Replace delimiting "\0"'s with "," to make tokenizing work. */
423 for (int i = 0; i < cbKeyValue / sizeof(TCHAR); i++)
424 if (szKeyValue[i] == '\0') szKeyValue[i] = ',';
425
426 TCHAR *pszToken = wcstok(szKeyValue, _T(","));
427 TCHAR *pszNewToken = NULL;
428 TCHAR *pNewKeyValuePos = szNewKeyValue;
429 while (pszToken != NULL)
430 {
431 pszNewToken = wcstok(NULL, _T(","));
432
433 /* Append new value (at beginning if iOrder=0). */
434 if (iPos == uiOrder)
435 {
436 memcpy(pNewKeyValuePos, pszValueToAdd, wcslen(pszValueToAdd)*sizeof(TCHAR));
437
438 cb += (wcslen(pszValueToAdd) + 1) * sizeof(TCHAR); /* Add trailing zero as well. */
439 pNewKeyValuePos += wcslen(pszValueToAdd) + 1;
440 iPos++;
441 }
442
443 if (0 != wcsicmp(pszToken, pszValueToAdd))
444 {
445 memcpy(pNewKeyValuePos, pszToken, wcslen(pszToken)*sizeof(TCHAR));
446 cb += (wcslen(pszToken) + 1) * sizeof(TCHAR); /* Add trailing zero as well. */
447 pNewKeyValuePos += wcslen(pszToken) + 1;
448 iPos++;
449 }
450
451 pszToken = pszNewToken;
452 }
453
454 /* Append as last item if needed. */
455 if (uiOrder >= iPos)
456 {
457 memcpy(pNewKeyValuePos, pszValueToAdd, wcslen(pszValueToAdd)*sizeof(TCHAR));
458 cb += wcslen(pszValueToAdd) * sizeof(TCHAR); /* Add trailing zero as well. */
459 }
460
461 lRet = RegSetValueExW(hKey, pszKeyValue, 0, REG_MULTI_SZ, (LPBYTE)szNewKeyValue, (DWORD)cb);
462 if (lRet != ERROR_SUCCESS)
463 _tprintf(_T("AddStringToMultiSZ: RegSetValueEx failed with error %ld!\n"), lRet);
464 }
465
466 RegCloseKey(hKey);
467 #ifdef DEBUG
468 if (lRet == ERROR_SUCCESS)
469 _tprintf(_T("AddStringToMultiSZ: Value %ws successfully written!\n"), pszValueToAdd);
470 #endif
471 }
472
473 return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
474}
475
476/**
477 * Removes a string entry from a MULTI_SZ registry list.
478 *
479 * @return Exit code (EXIT_OK, EXIT_FAIL)
480 * @param pszSubKey Sub key containing the list.
481 * @param pszKeyValue The actual key name of the list.
482 * @param pszValueToRemove The value to remove from the list.
483 */
484int RegistryRemoveStringFromMultiSZ(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToRemove)
485{
486 // @todo Make string sizes dynamically allocated!
487
488 const TCHAR *pszKey = pszSubKey;
489#ifdef DEBUG
490 _tprintf(_T("RemoveStringFromMultiSZ: Removing MULTI_SZ string: %ws from %ws\\%ws ...\n"), pszValueToRemove, pszSubKey, pszKeyValue);
491#endif
492
493 HKEY hkey;
494 DWORD disp, dwType;
495 LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hkey, &disp);
496 if (lRet != ERROR_SUCCESS)
497 _tprintf(_T("RemoveStringFromMultiSZ: RegCreateKeyEx %ts failed with error %ld!\n"), pszKey, lRet);
498
499 if (lRet == ERROR_SUCCESS)
500 {
501 TCHAR szKeyValue[1024];
502 DWORD cbKeyValue = sizeof(szKeyValue);
503
504 lRet = RegQueryValueEx(hkey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
505 if ( lRet != ERROR_SUCCESS
506 || dwType != REG_MULTI_SZ)
507 {
508 _tprintf(_T("RemoveStringFromMultiSZ: RegQueryValueEx failed with %d, key type = 0x%x!\n"), lRet, dwType);
509 }
510 else
511 {
512 #ifdef DEBUG
513 _tprintf(_T("RemoveStringFromMultiSZ: Current key len: %ld\n"), cbKeyValue);
514 #endif
515
516 TCHAR szCurString[1024] = { 0 };
517 TCHAR szFinalString[1024] = { 0 };
518 int iIndex = 0;
519 int iNewIndex = 0;
520 for (int i = 0; i < cbKeyValue / sizeof(TCHAR); i++)
521 {
522 if (szKeyValue[i] != _T('\0'))
523 szCurString[iIndex++] = szKeyValue[i];
524
525 if ( (!szKeyValue[i] == _T('\0'))
526 && (szKeyValue[i + 1] == _T('\0')))
527 {
528 if (NULL == wcsstr(szCurString, pszValueToRemove))
529 {
530 wcscat(&szFinalString[iNewIndex], szCurString);
531
532 if (iNewIndex == 0)
533 iNewIndex = iIndex;
534 else iNewIndex += iIndex;
535
536 szFinalString[++iNewIndex] = _T('\0');
537 }
538
539 iIndex = 0;
540 ZeroMemory(szCurString, sizeof(szCurString));
541 }
542 }
543 szFinalString[++iNewIndex] = _T('\0');
544 #ifdef DEBUG
545 _tprintf(_T("RemoveStringFromMultiSZ: New key value: %ws (%u bytes)\n"),
546 szFinalString, iNewIndex * sizeof(TCHAR));
547 #endif
548
549 lRet = RegSetValueExW(hkey, pszKeyValue, 0, REG_MULTI_SZ, (LPBYTE)szFinalString, iNewIndex * sizeof(TCHAR));
550 if (lRet != ERROR_SUCCESS)
551 _tprintf(_T("RemoveStringFromMultiSZ: RegSetValueEx failed with %d!\n"), lRet);
552 }
553
554 RegCloseKey(hkey);
555 #ifdef DEBUG
556 if (lRet == ERROR_SUCCESS)
557 _tprintf(_T("RemoveStringFromMultiSZ: Value %ws successfully removed!\n"), pszValueToRemove);
558 #endif
559 }
560
561 return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
562}
563
564/**
565 * Adds a string to a registry string list (STRING_SZ).
566 * Only operates in HKLM for now, needs to be extended later for
567 * using other hives. Only processes lists with a "," separator
568 * at the moment.
569 *
570 * @return Exit code (EXIT_OK, EXIT_FAIL)
571 * @param pszSubKey Sub key containing the list.
572 * @param pszKeyValue The actual key name of the list.
573 * @param pszValueToAdd The value to add to the list.
574 * @param uiOrder Position (zero-based) of where to add the value to the list.
575 * @param dwFlags Flags.
576 */
577int RegistryAddStringToList(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToAdd,
578 unsigned int uiOrder, DWORD dwFlags)
579{
580 HKEY hKey = NULL;
581 DWORD disp, dwType;
582 LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, &disp);
583 if (lRet != ERROR_SUCCESS)
584 _tprintf(_T("RegistryAddStringToList: RegCreateKeyEx %ts failed with error %ld!\n"), pszSubKey, lRet);
585
586 TCHAR szKeyValue[512] = { 0 };
587 TCHAR szNewKeyValue[512] = { 0 };
588 DWORD cbKeyValue = sizeof(szKeyValue);
589
590 lRet = RegQueryValueEx(hKey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
591 if ( lRet != ERROR_SUCCESS
592 || dwType != REG_SZ)
593 {
594 _tprintf(_T("RegistryAddStringToList: RegQueryValueEx failed with %d, key type = 0x%x!\n"), lRet, dwType);
595 }
596
597 if (lRet == ERROR_SUCCESS)
598 {
599 #ifdef DEBUG
600 _tprintf(_T("RegistryAddStringToList: Key value: %ws\n"), szKeyValue);
601 #endif
602
603 /* Create entire new list. */
604 unsigned int iPos = 0;
605 TCHAR *pszToken = wcstok(szKeyValue, _T(","));
606 TCHAR *pszNewToken = NULL;
607 while (pszToken != NULL)
608 {
609 pszNewToken = wcstok(NULL, _T(","));
610
611 /* Append new provider name (at beginning if iOrder=0). */
612 if (iPos == uiOrder)
613 {
614 wcscat(szNewKeyValue, pszValueToAdd);
615 wcscat(szNewKeyValue, _T(","));
616 iPos++;
617 }
618
619 BOOL fAddToList = FALSE;
620 if ( !wcsicmp(pszToken, pszValueToAdd)
621 && (dwFlags & VBOX_REG_STRINGLIST_ALLOW_DUPLICATES))
622 fAddToList = TRUE;
623 else if (wcsicmp(pszToken, pszValueToAdd))
624 fAddToList = TRUE;
625
626 if (fAddToList)
627 {
628 wcscat(szNewKeyValue, pszToken);
629 wcscat(szNewKeyValue, _T(","));
630 iPos++;
631 }
632
633 #ifdef DEBUG
634 _tprintf (_T("RegistryAddStringToList: Temp new key value: %ws\n"), szNewKeyValue);
635 #endif
636 pszToken = pszNewToken;
637 }
638
639 /* Append as last item if needed. */
640 if (uiOrder >= iPos)
641 wcscat(szNewKeyValue, pszValueToAdd);
642
643 /* Last char a delimiter? Cut off ... */
644 if (szNewKeyValue[wcslen(szNewKeyValue) - 1] == ',')
645 szNewKeyValue[wcslen(szNewKeyValue) - 1] = '\0';
646
647 size_t iNewLen = (wcslen(szNewKeyValue) * sizeof(WCHAR)) + sizeof(WCHAR);
648
649 #ifdef DEBUG
650 _tprintf(_T("RegistryAddStringToList: New provider list: %ws (%u bytes)\n"), szNewKeyValue, iNewLen);
651 #endif
652
653 lRet = RegSetValueExW(hKey, pszKeyValue, 0, REG_SZ, (LPBYTE)szNewKeyValue, (DWORD)iNewLen);
654 if (lRet != ERROR_SUCCESS)
655 _tprintf(_T("RegistryAddStringToList: RegSetValueEx failed with %ld!\n"), lRet);
656 }
657
658 RegCloseKey(hKey);
659 return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
660}
661
662/**
663 * Removes a string from a registry string list (STRING_SZ).
664 * Only operates in HKLM for now, needs to be extended later for
665 * using other hives. Only processes lists with a "," separator
666 * at the moment.
667 *
668 * @return Exit code (EXIT_OK, EXIT_FAIL)
669 * @param pszSubKey Sub key containing the list.
670 * @param pszKeyValue The actual key name of the list.
671 * @param pszValueToRemove The value to remove from the list.
672 */
673int RegistryRemoveStringFromList(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToRemove)
674{
675 HKEY hKey = NULL;
676 DWORD disp, dwType;
677 LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, &disp);
678 if (lRet != ERROR_SUCCESS)
679 _tprintf(_T("RegistryRemoveStringFromList: RegCreateKeyEx %ts failed with error %ld!\n"), pszSubKey, lRet);
680
681 TCHAR szKeyValue[512] = { 0 };
682 TCHAR szNewKeyValue[512] = { 0 };
683 DWORD cbKeyValue = sizeof(szKeyValue);
684
685 lRet = RegQueryValueEx(hKey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
686 if ( lRet != ERROR_SUCCESS
687 || dwType != REG_SZ)
688 {
689 _tprintf(_T("RegistryRemoveStringFromList: RegQueryValueEx failed with %d, key type = 0x%x!\n"), lRet, dwType);
690 }
691
692 if (lRet == ERROR_SUCCESS)
693 {
694 #ifdef DEBUG
695 _tprintf(_T("RegistryRemoveStringFromList: Key value: %ws\n"), szKeyValue);
696 #endif
697
698 /* Create entire new list. */
699 int iPos = 0;
700
701 TCHAR *pszToken = wcstok(szKeyValue, _T(","));
702 TCHAR *pszNewToken = NULL;
703 while (pszToken != NULL)
704 {
705 pszNewToken = wcstok(NULL, _T(","));
706
707 /* Append all list values as long as it's not the
708 * value we want to remove. */
709 if (wcsicmp(pszToken, pszValueToRemove))
710 {
711 wcscat(szNewKeyValue, pszToken);
712 wcscat(szNewKeyValue, _T(","));
713 iPos++;
714 }
715
716 #ifdef DEBUG
717 _tprintf (_T("RegistryRemoveStringFromList: Temp new key value: %ws\n"), szNewKeyValue);
718 #endif
719 pszToken = pszNewToken;
720 }
721
722 /* Last char a delimiter? Cut off ... */
723 if (szNewKeyValue[wcslen(szNewKeyValue) - 1] == ',')
724 szNewKeyValue[wcslen(szNewKeyValue) - 1] = '\0';
725
726 size_t iNewLen = (wcslen(szNewKeyValue) * sizeof(WCHAR)) + sizeof(WCHAR);
727
728 #ifdef DEBUG
729 _tprintf(_T("RegistryRemoveStringFromList: New provider list: %ws (%u bytes)\n"), szNewKeyValue, iNewLen);
730 #endif
731
732 lRet = RegSetValueExW(hKey, pszKeyValue, 0, REG_SZ, (LPBYTE)szNewKeyValue, (DWORD)iNewLen);
733 if (lRet != ERROR_SUCCESS)
734 _tprintf(_T("RegistryRemoveStringFromList: RegSetValueEx failed with %ld!\n"), lRet);
735 }
736
737 RegCloseKey(hKey);
738 return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
739}
740
741/**
742 * Adds a network provider with a specified order to the system.
743 *
744 * @return Exit code (EXIT_OK, EXIT_FAIL)
745 * @param pszProvider Name of network provider to add.
746 * @param uiOrder Position in list (zero-based) of where to add.
747 */
748int AddNetworkProvider(const TCHAR *pszProvider, unsigned int uiOrder)
749{
750 _tprintf(_T("Adding network provider \"%ws\" (Order = %u) ...\n"), pszProvider, uiOrder);
751 int rc = RegistryAddStringToList(_T("System\\CurrentControlSet\\Control\\NetworkProvider\\Order"),
752 _T("ProviderOrder"),
753 pszProvider, uiOrder, VBOX_REG_STRINGLIST_NONE /* No flags set */);
754 if (rc == EXIT_OK)
755 _tprintf(_T("Network provider successfully added!\n"));
756 return rc;
757}
758
759/**
760 * Removes a network provider from the system.
761 *
762 * @return Exit code (EXIT_OK, EXIT_FAIL)
763 * @param pszProvider Name of network provider to remove.
764 */
765int RemoveNetworkProvider(const TCHAR *pszProvider)
766{
767 _tprintf(_T("Removing network provider \"%ws\" ...\n"), pszProvider);
768 int rc = RegistryRemoveStringFromList(_T("System\\CurrentControlSet\\Control\\NetworkProvider\\Order"),
769 _T("ProviderOrder"),
770 pszProvider);
771 if (rc == EXIT_OK)
772 _tprintf(_T("Network provider successfully removed!\n"));
773 return rc;
774}
775
776int CreateService(const TCHAR *pszStartStopName,
777 const TCHAR *pszDisplayName,
778 int iServiceType,
779 int iStartType,
780 const TCHAR *pszBinPath,
781 const TCHAR *pszLoadOrderGroup,
782 const TCHAR *pszDependencies,
783 const TCHAR *pszLogonUser,
784 const TCHAR *pszLogonPassword)
785{
786 int rc = ERROR_SUCCESS;
787
788 _tprintf(_T("Installing service %ws (%ws) ...\n"), pszDisplayName, pszStartStopName);
789
790 SC_HANDLE hSCManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);
791 if (hSCManager == NULL)
792 {
793 _tprintf(_T("Could not get handle to SCM! Error: %ld\n"), GetLastError());
794 return EXIT_FAIL;
795 }
796
797 /* Fixup end of multistring. */
798 TCHAR szDepend[ _MAX_PATH ] = { 0 }; /* @todo Use dynamically allocated string here! */
799 if (pszDependencies != NULL)
800 {
801 _tcsnccpy (szDepend, pszDependencies, wcslen(pszDependencies));
802 DWORD len = (DWORD)wcslen (szDepend);
803 szDepend [len + 1] = 0;
804
805 /* Replace comma separator on null separator. */
806 for (DWORD i = 0; i < len; i++)
807 {
808 if (',' == szDepend [i])
809 szDepend [i] = 0;
810 }
811 }
812
813 DWORD dwTag = 0xDEADBEAF;
814 SC_HANDLE hService = CreateService (hSCManager, /* SCManager database handle. */
815 pszStartStopName, /* Name of service. */
816 pszDisplayName, /* Name to display. */
817 SERVICE_ALL_ACCESS, /* Desired access. */
818 iServiceType, /* Service type. */
819 iStartType, /* Start type. */
820 SERVICE_ERROR_NORMAL, /* Error control type. */
821 pszBinPath, /* Service's binary. */
822 pszLoadOrderGroup, /* Ordering group. */
823 (pszLoadOrderGroup != NULL) ? &dwTag : NULL, /* Tag identifier. */
824 (pszDependencies != NULL) ? szDepend : NULL, /* Dependencies. */
825 (pszLogonUser != NULL) ? pszLogonUser: NULL, /* Account. */
826 (pszLogonPassword != NULL) ? pszLogonPassword : NULL); /* Password. */
827 if (NULL == hService)
828 {
829 DWORD dwErr = GetLastError();
830 switch (dwErr)
831 {
832
833 case ERROR_SERVICE_EXISTS:
834 {
835 _tprintf(_T("Service already exists. No installation required. Updating the service config.\n"));
836
837 hService = OpenService (hSCManager, /* SCManager database handle. */
838 pszStartStopName, /* Name of service. */
839 SERVICE_ALL_ACCESS); /* Desired access. */
840 if (NULL == hService)
841 {
842 dwErr = GetLastError();
843 _tprintf(_T("Could not open service! Error: %ld\n"), dwErr);
844 }
845 else
846 {
847 BOOL fResult = ChangeServiceConfig (hService, /* Service handle. */
848 iServiceType, /* Service type. */
849 iStartType, /* Start type. */
850 SERVICE_ERROR_NORMAL, /* Error control type. */
851 pszBinPath, /* Service's binary. */
852 pszLoadOrderGroup, /* Ordering group. */
853 (pszLoadOrderGroup != NULL) ? &dwTag : NULL, /* Tag identifier. */
854 (pszDependencies != NULL) ? szDepend : NULL, /* Dependencies. */
855 (pszLogonUser != NULL) ? pszLogonUser: NULL, /* Account. */
856 (pszLogonPassword != NULL) ? pszLogonPassword : NULL, /* Password. */
857 pszDisplayName); /* Name to display. */
858 if (fResult)
859 _tprintf(_T("The service config has been successfully updated.\n"));
860 else
861 {
862 dwErr = GetLastError();
863 _tprintf(_T("Could not change service config! Error: %ld\n"), dwErr);
864 }
865 CloseServiceHandle(hService);
866 }
867
868 /*
869 * This entire branch do not return an error to avoid installations failures,
870 * if updating service parameters. Better to have a running system with old
871 * parameters and the failure information in the installation log.
872 */
873 break;
874 }
875
876 case ERROR_INVALID_PARAMETER:
877
878 _tprintf(_T("Invalid parameter specified!\n"));
879 rc = EXIT_FAIL;
880 break;
881
882 default:
883
884 _tprintf(_T("Could not create service! Error: %ld\n"), dwErr);
885 rc = EXIT_FAIL;
886 break;
887 }
888
889 if (rc == EXIT_FAIL)
890 goto cleanup;
891 }
892 else
893 {
894 CloseServiceHandle (hService);
895 _tprintf(_T("Installation of service successful!\n"));
896 }
897
898cleanup:
899
900 if (hSCManager != NULL)
901 CloseServiceHandle (hSCManager);
902
903 return rc;
904}
905
906int DelService(const TCHAR *pszStartStopName)
907{
908 int rc = ERROR_SUCCESS;
909
910 _tprintf(_T("Deleting service '%ws' ...\n"), pszStartStopName);
911
912 SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
913 SC_HANDLE hService = NULL;
914 if (hSCManager == NULL)
915 {
916 _tprintf(_T("Could not get handle to SCM! Error: %ld\n"), GetLastError());
917 rc = EXIT_FAIL;
918 }
919 else
920 {
921 hService = OpenService(hSCManager, pszStartStopName, SERVICE_ALL_ACCESS);
922 if (NULL == hService)
923 {
924 _tprintf(_T("Could not open service '%ws'! Error: %ld\n"), pszStartStopName, GetLastError());
925 rc = EXIT_FAIL;
926 }
927 }
928
929 if (hService != NULL)
930 {
931 if (LockServiceDatabase(hSCManager))
932 {
933 if (FALSE == DeleteService(hService))
934 {
935 DWORD dwErr = GetLastError();
936 switch (dwErr)
937 {
938
939 case ERROR_SERVICE_MARKED_FOR_DELETE:
940
941 _tprintf(_T("Service '%ws' already marked for deletion.\n"), pszStartStopName);
942 break;
943
944 default:
945
946 _tprintf(_T("Could not delete service '%ws'! Error: %ld\n"), pszStartStopName, GetLastError());
947 rc = EXIT_FAIL;
948 break;
949 }
950 }
951 else
952 {
953 _tprintf(_T("Service '%ws' successfully removed!\n"), pszStartStopName);
954 }
955 UnlockServiceDatabase(hSCManager);
956 }
957 else
958 {
959 _tprintf(_T("Unable to lock service database! Error: %ld\n"), GetLastError());
960 rc = EXIT_FAIL;
961 }
962 CloseServiceHandle(hService);
963 }
964
965 if (hSCManager != NULL)
966 CloseServiceHandle(hSCManager);
967
968 return rc;
969}
970
971DWORD RegistryWrite(HKEY hRootKey,
972 const _TCHAR *pszSubKey,
973 const _TCHAR *pszValueName,
974 DWORD dwType,
975 const BYTE *pbData,
976 DWORD cbData)
977{
978 DWORD lRet;
979 HKEY hKey;
980 lRet = RegCreateKeyEx (hRootKey,
981 pszSubKey,
982 0, /* Reserved */
983 NULL, /* lpClass [in, optional] */
984 0, /* dwOptions [in] */
985 KEY_WRITE,
986 NULL, /* lpSecurityAttributes [in, optional] */
987 &hKey,
988 NULL); /* lpdwDisposition [out, optional] */
989 if (lRet != ERROR_SUCCESS)
990 {
991 _tprintf(_T("Could not open registry key! Error: %ld\n"), GetLastError());
992 }
993 else
994 {
995 lRet = RegSetValueEx(hKey, pszValueName, 0, dwType, (BYTE*)pbData, cbData);
996 if (lRet != ERROR_SUCCESS)
997 _tprintf(_T("Could not write to registry! Error: %ld\n"), GetLastError());
998 RegCloseKey(hKey);
999
1000 }
1001 return lRet;
1002}
1003
1004void PrintHelp(void)
1005{
1006 _tprintf(_T("VirtualBox Guest Additions Installation Helper for Windows\n"));
1007 _tprintf(_T("Version: %d.%d.%d.%d\n\n"), VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
1008 _tprintf(_T("Syntax:\n"));
1009 _tprintf(_T("\n"));
1010 _tprintf(_T("Drivers:\n"));
1011 _tprintf(_T("\tVBoxDrvInst driver install <inf-file> [log file]\n"));
1012 _tprintf(_T("\tVBoxDrvInst driver uninstall <inf-file> [log file]\n"));
1013 _tprintf(_T("\tVBoxDrvInst driver executeinf <inf-file>\n"));
1014 _tprintf(_T("\n"));
1015 _tprintf(_T("Network Provider:\n"));
1016 _tprintf(_T("\tVBoxDrvInst netprovider add <name> [order]\n"));
1017 _tprintf(_T("\tVBoxDrvInst netprovider remove <name>\n"));
1018 _tprintf(_T("\n"));
1019 _tprintf(_T("Registry:\n"));
1020 _tprintf(_T("\tVBoxDrvInst registry write <root> <sub key>\n")
1021 _T("\t <key name> <key type> <value>\n")
1022 _T("\t [type] [size]\n"));
1023 _tprintf(_T("\tVBoxDrvInst registry addmultisz <root> <sub key>\n")
1024 _T("\t <value> [order]\n"));
1025 _tprintf(_T("\tVBoxDrvInst registry delmultisz <root> <sub key>\n")
1026 _T("\t <key name> <value to remove>\n"));
1027 /** @todo Add "service" category! */
1028 _tprintf(_T("\n"));
1029}
1030
1031int __cdecl _tmain(int argc, _TCHAR *argv[])
1032{
1033 int rc = EXIT_USAGE;
1034
1035 OSVERSIONINFO OSinfo;
1036 OSinfo.dwOSVersionInfoSize = sizeof(OSinfo);
1037 GetVersionEx(&OSinfo);
1038
1039 if (argc >= 2)
1040 {
1041 if ( !_tcsicmp(argv[1], _T("driver"))
1042 && argc >= 3)
1043 {
1044 _TCHAR szINF[_MAX_PATH] = { 0 }; /* Complete path to INF file.*/
1045 if ( ( !_tcsicmp(argv[2], _T("install"))
1046 || !_tcsicmp(argv[2], _T("uninstall")))
1047 && argc >= 4)
1048 {
1049 if (OSinfo.dwMajorVersion < 5)
1050 {
1051 _tprintf(_T("ERROR: Platform not supported for driver (un)installation!\n"));
1052 rc = EXIT_FAIL;
1053 }
1054 else
1055 {
1056 _sntprintf(szINF, sizeof(szINF) / sizeof(TCHAR), _T("%ws"), argv[3]);
1057
1058 _TCHAR szLogFile[_MAX_PATH] = { 0 };
1059 if (argc > 4)
1060 _sntprintf(szLogFile, sizeof(szLogFile) / sizeof(TCHAR), _T("%ws"), argv[4]);
1061 rc = VBoxInstallDriver(!_tcsicmp(argv[2], _T("install")) ? TRUE : FALSE, szINF,
1062 FALSE /* Not silent */, szLogFile[0] != NULL ? szLogFile : NULL);
1063 }
1064 }
1065 else if ( !_tcsicmp(argv[2], _T("executeinf"))
1066 && argc == 4)
1067 {
1068 _sntprintf(szINF, sizeof(szINF) / sizeof(TCHAR), _T("%ws"), argv[3]);
1069 rc = ExecuteInfFile(_T("DefaultInstall"), 132, szINF);
1070 }
1071 }
1072 else if ( !_tcsicmp(argv[1], _T("netprovider"))
1073 && argc >= 3)
1074 {
1075 _TCHAR szProvider[_MAX_PATH] = { 0 }; /* The network provider name for the registry. */
1076 if ( !_tcsicmp(argv[2], _T("add"))
1077 && argc >= 4)
1078 {
1079 int iOrder = 0;
1080 if (argc > 4)
1081 iOrder = _ttoi(argv[4]);
1082 _sntprintf(szProvider, sizeof(szProvider) / sizeof(TCHAR), _T("%ws"), argv[3]);
1083 rc = AddNetworkProvider(szProvider, iOrder);
1084 }
1085 else if ( !_tcsicmp(argv[2], _T("remove"))
1086 && argc >= 4)
1087 {
1088 _sntprintf(szProvider, sizeof(szProvider) / sizeof(TCHAR), _T("%ws"), argv[3]);
1089 rc = RemoveNetworkProvider(szProvider);
1090 }
1091 }
1092 else if ( !_tcsicmp(argv[1], _T("service"))
1093 && argc >= 3)
1094 {
1095 if ( !_tcsicmp(argv[2], _T("create"))
1096 && argc >= 8)
1097 {
1098 rc = CreateService(argv[3],
1099 argv[4],
1100 _ttoi(argv[5]),
1101 _ttoi(argv[6]),
1102 argv[7],
1103 (argc > 8) ? argv[8] : NULL,
1104 (argc > 9) ? argv[9] : NULL,
1105 (argc > 10) ? argv[10] : NULL,
1106 (argc > 11) ? argv[11] : NULL);
1107 }
1108 else if ( !_tcsicmp(argv[2], _T("delete"))
1109 && argc == 4)
1110 {
1111 rc = DelService(argv[3]);
1112 }
1113 }
1114 else if ( !_tcsicmp(argv[1], _T("registry"))
1115 && argc >= 3)
1116 {
1117 /** @todo add a handleRegistry(argc, argv) method to keep things cleaner */
1118 if ( !_tcsicmp(argv[2], _T("addmultisz"))
1119 && argc == 7)
1120 {
1121 rc = RegistryAddStringToMultiSZ(argv[3], argv[4], argv[5], _ttoi(argv[6]));
1122 }
1123 else if ( !_tcsicmp(argv[2], _T("delmultisz"))
1124 && argc == 6)
1125 {
1126 rc = RegistryRemoveStringFromMultiSZ(argv[3], argv[4], argv[5]);
1127 }
1128 else if ( !_tcsicmp(argv[2], _T("write"))
1129 && argc >= 8)
1130 {
1131 HKEY hRootKey = HKEY_LOCAL_MACHINE; /** @todo needs to be expanded (argv[3]) */
1132 DWORD dwValSize;
1133 BYTE *pbVal = NULL;
1134 DWORD dwVal;
1135
1136 if (argc > 8)
1137 {
1138 if (!_tcsicmp(argv[8], _T("dword")))
1139 {
1140 dwVal = _ttol(argv[7]);
1141 pbVal = (BYTE*)&dwVal;
1142 dwValSize = sizeof(DWORD);
1143 }
1144 }
1145 if (pbVal == NULL) /* By default interpret value as string */
1146 {
1147 pbVal = (BYTE*)argv[7];
1148 dwValSize = _tcslen(argv[7]);
1149 }
1150 if (argc > 9)
1151 dwValSize = _ttol(argv[9]); /* Get the size in bytes of the value we want to write */
1152 rc = RegistryWrite(hRootKey,
1153 argv[4], /* Sub key */
1154 argv[5], /* Value name */
1155 REG_BINARY, /** @todo needs to be expanded (argv[6]) */
1156 pbVal, /* The value itself */
1157 dwValSize); /* Size of the value */
1158 }
1159#if 0
1160 else if (!_tcsicmp(argv[2], _T("read")))
1161 {
1162 }
1163 else if (!_tcsicmp(argv[2], _T("del")))
1164 {
1165 }
1166#endif
1167 }
1168 else if (!_tcsicmp(argv[1], _T("--version")))
1169 {
1170 _tprintf(_T("%d.%d.%d.%d\n"), VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
1171 rc = EXIT_OK;
1172 }
1173 else if ( !_tcsicmp(argv[1], _T("--help"))
1174 || !_tcsicmp(argv[1], _T("/help"))
1175 || !_tcsicmp(argv[1], _T("/h"))
1176 || !_tcsicmp(argv[1], _T("/?")))
1177 {
1178 PrintHelp();
1179 rc = EXIT_OK;
1180 }
1181 }
1182
1183 if (rc == EXIT_USAGE)
1184 _tprintf(_T("No or wrong parameters given! Please consult the help (\"--help\" or \"/?\") for more information.\n"));
1185
1186 return rc;
1187}
1188
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette