VirtualBox

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

最後變更 在這個檔案從35654是 35649,由 vboxsync 提交於 14 年 前

VBoxDrvInst: Now dynamically uses the DIFx API for (un)installing drivers, lot of clean up, some documentation.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 44.7 KB
 
1/* $Id: VBoxDrvInst.cpp 35649 2011-01-20 13:45:52Z vboxsync $ */
2/** @file
3 * VBoxDrvInst - Driver and service installation helper for Windows guests.
4 */
5
6/*
7 * Copyright (C) 2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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
23#ifndef UNICODE
24#define UNICODE
25#endif
26
27#include <VBox/version.h>
28
29#include <windows.h>
30#include <setupapi.h>
31#include <stdio.h>
32#include <tchar.h>
33
34/*******************************************************************************
35* Defines *
36*******************************************************************************/
37
38/* Exit codes */
39#define EXIT_OK (0)
40#define EXIT_REBOOT (1)
41#define EXIT_FAIL (2)
42#define EXIT_USAGE (3)
43
44/* Prototypes */
45typedef struct {
46 PWSTR pApplicationId;
47 PWSTR pDisplayName;
48 PWSTR pProductName;
49 PWSTR pMfgName;
50} INSTALLERINFO, *PINSTALLERINFO;
51typedef const PINSTALLERINFO PCINSTALLERINFO;
52
53typedef enum {
54 DIFXAPI_SUCCESS,
55 DIFXAPI_INFO,
56 DIFXAPI_WARNING,
57 DIFXAPI_ERROR
58} DIFXAPI_LOG;
59
60typedef void (WINAPI * DIFXLOGCALLBACK_W)( DIFXAPI_LOG Event, DWORD Error, PCWSTR EventDescription, PVOID CallbackContext);
61typedef void ( __cdecl* DIFXAPILOGCALLBACK_W)( DIFXAPI_LOG Event, DWORD Error, PCWSTR EventDescription, PVOID CallbackContext);
62
63typedef DWORD (WINAPI *fnDriverPackageInstall) (PCTSTR DriverPackageInfPath, DWORD Flags, PCINSTALLERINFO pInstallerInfo, BOOL *pNeedReboot);
64fnDriverPackageInstall g_pfnDriverPackageInstall = NULL;
65
66typedef DWORD (WINAPI *fnDriverPackageUninstall) (PCTSTR DriverPackageInfPath, DWORD Flags, PCINSTALLERINFO pInstallerInfo, BOOL *pNeedReboot);
67fnDriverPackageUninstall g_pfnDriverPackageUninstall = NULL;
68
69typedef VOID (WINAPI *fnDIFXAPISetLogCallback) (DIFXAPILOGCALLBACK_W LogCallback, PVOID CallbackContext);
70fnDIFXAPISetLogCallback g_pfnDIFXAPISetLogCallback = NULL;
71
72/* Defines */
73#define DRIVER_PACKAGE_REPAIR 0x00000001
74#define DRIVER_PACKAGE_SILENT 0x00000002
75#define DRIVER_PACKAGE_FORCE 0x00000004
76#define DRIVER_PACKAGE_ONLY_IF_DEVICE_PRESENT 0x00000008
77#define DRIVER_PACKAGE_LEGACY_MODE 0x00000010
78#define DRIVER_PACKAGE_DELETE_FILES 0x00000020
79
80/* DIFx error codes */
81#define ERROR_DEPENDENT_APPLICATIONS_EXIST \
82 (APPLICATION_ERROR_MASK|ERROR_SEVERITY_ERROR|0x300)
83#define ERROR_DRIVER_STORE_ADD_FAILED \
84 (APPLICATION_ERROR_MASK | ERROR_SEVERITY_ERROR | 0x0247L)
85#define ERROR_DRIVER_PACKAGE_NOT_IN_STORE \
86 (APPLICATION_ERROR_MASK|ERROR_SEVERITY_ERROR|0x302)
87
88/* Registry string list flags */
89#define VBOX_REG_STRINGLIST_NONE 0x00000000 /* No flags set. */
90#define VBOX_REG_STRINGLIST_ALLOW_DUPLICATES 0x00000001 /* Allows duplicates in list when adding a value. */
91
92#ifdef DEBUG
93 #define VBOX_DRVINST_LOGFILE "C:\\Temp\\VBoxDrvInstDIFx.log"
94#endif
95
96bool GetErrorMsg(DWORD dwLastError, _TCHAR *pszMsg, DWORD dwBufSize)
97{
98 if (::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwLastError, 0, pszMsg, dwBufSize / sizeof(TCHAR), NULL) == 0)
99 {
100 _stprintf(pszMsg, _T("Unknown error!\n"), dwLastError);
101 return false;
102 }
103 else
104 {
105 _TCHAR *p = _tcschr(pszMsg, _T('\r'));
106
107 if (p != NULL)
108 *p = _T('\0');
109 }
110
111 return true;
112}
113
114/**
115 * Log callback for DIFxAPI calls.
116 *
117 * @param Event The event's structure to log.
118 * @param dwError The event's error level.
119 * @param pEventDescription The event's text description.
120 * @param pCallbackContext User-supplied callback context.
121 */
122void LogCallback(DIFXAPI_LOG Event, DWORD dwError, PCWSTR pEventDescription, PVOID pCallbackContext)
123{
124#ifdef DEBUG
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#else
130 if (dwError > 0)
131 _tprintf(_T("ERROR: (%u) %u - %ws\n"), Event, dwError, pEventDescription);
132#endif
133
134 if (pCallbackContext)
135 fwprintf((FILE*)pCallbackContext, _T("(%u) %u - %s\n"), Event, dwError, pEventDescription);
136}
137
138/**
139 * (Un)Installs a driver from/to the system.
140 *
141 * @return Exit code (EXIT_OK, EXIT_FAIL)
142 * @param fInstall Flag indicating whether to install (TRUE) or uninstall (FALSE) a driver.
143 * @param pszDriverPath Full qualified path to the driver's .INF file (+ driver files).
144 * @param fSilent Flag indicating a silent installation (TRUE) or not (FALSE).
145 */
146int VBoxInstallDriver(const BOOL fInstall, const _TCHAR *pszDriverPath, BOOL fSilent)
147{
148 HRESULT hr = ERROR_SUCCESS;
149 HMODULE hDIFxAPI = LoadLibrary(_T("DIFxAPI.dll"));
150 if (NULL == hDIFxAPI)
151 {
152 _tprintf(_T("ERROR: Unable to locate DIFxAPI.dll!\n"));
153 hr = ERROR_FILE_NOT_FOUND;
154 }
155 else
156 {
157 if (fInstall)
158 {
159 g_pfnDriverPackageInstall = (fnDriverPackageInstall)GetProcAddress(hDIFxAPI, "DriverPackageInstallW");
160 if (g_pfnDriverPackageInstall == NULL)
161 {
162 _tprintf(_T("ERROR: Unable to retrieve entry point for DriverPackageInstallW!\n"));
163 hr = ERROR_PROC_NOT_FOUND;
164 }
165 }
166 else
167 {
168 g_pfnDriverPackageUninstall = (fnDriverPackageUninstall)GetProcAddress(hDIFxAPI, "DriverPackageUninstallW");
169 if (g_pfnDriverPackageUninstall == NULL)
170 {
171 _tprintf(_T("ERROR: Unable to retrieve entry point for DriverPackageUninstallW!\n"));
172 hr = ERROR_PROC_NOT_FOUND;
173 }
174 }
175
176 if (SUCCEEDED(hr))
177 {
178 g_pfnDIFXAPISetLogCallback = (fnDIFXAPISetLogCallback)GetProcAddress(hDIFxAPI, "DIFXAPISetLogCallbackW");
179 if (g_pfnDIFXAPISetLogCallback == NULL)
180 {
181 _tprintf(_T("ERROR: Unable to retrieve entry point for DIFXAPISetLogCallbackW!\n"));
182 hr = ERROR_PROC_NOT_FOUND;
183 }
184 }
185 }
186
187 if (SUCCEEDED(hr))
188 {
189 FILE *fh = NULL;
190#ifdef DEBUG
191 char szLogFile[MAX_PATH + 1];
192 sprintf(szLogFile, VBOX_DRVINST_LOGFILE);
193 fh = fopen(szLogFile, "a");
194 if (!fh)
195 _tprintf(_T("ERROR: Unable to create log file!\n"));
196 else
197 _tprintf(_T("Logging enabled ...\n"));
198#endif
199 g_pfnDIFXAPISetLogCallback(LogCallback, fh);
200
201 INSTALLERINFO instInfo =
202 {
203 TEXT("{7d2c708d-c202-40ab-b3e8-de21da1dc629}"), /* Our GUID for representing this installation tool. */
204 TEXT("VirtualBox Guest Additions Install Helper"),
205 TEXT("VirtualBox Guest Additions"), /** @todo Add version! */
206 TEXT("Oracle Corporation")
207 };
208
209 _TCHAR szDriverInf[MAX_PATH + 1];
210 if (0 == GetFullPathNameW(pszDriverPath, MAX_PATH, szDriverInf, NULL))
211 {
212 _tprintf(_T("ERROR: INF-Path too long / could not be retrieved!\n"));
213 hr = ERROR_INVALID_PARAMETER;
214 }
215 else
216 {
217 if (fInstall)
218 _tprintf(_T("Installing driver ...\n"));
219 else
220 _tprintf(_T("Uninstalling driver ...\n"));
221 _tprintf(_T("INF-File: %ws\n"), szDriverInf);
222
223 DWORD dwFlags = DRIVER_PACKAGE_FORCE;
224 if (!fInstall)
225 dwFlags |= DRIVER_PACKAGE_DELETE_FILES;
226
227 OSVERSIONINFO osi;
228 memset(&osi, 0, sizeof(OSVERSIONINFO));
229 osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
230 if ( (GetVersionEx(&osi) != 0)
231 && (osi.dwPlatformId == VER_PLATFORM_WIN32_NT)
232 && (osi.dwMajorVersion < 6))
233 {
234 if (fInstall)
235 {
236 _tprintf(_T("Using legacy mode for install ...\n"));
237 dwFlags |= DRIVER_PACKAGE_LEGACY_MODE;
238 }
239 }
240
241 if (fSilent)
242 {
243 _tprintf(_T("Installation is silent ...\n"));
244 dwFlags |= DRIVER_PACKAGE_SILENT;
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 = ERROR_INSTALL_FAILURE;
337 }
338 g_pfnDIFXAPISetLogCallback(NULL, NULL);
339#ifdef DEBUG
340 if (fh)
341 fclose(fh);
342#endif
343 if (SUCCEEDED(hr))
344 {
345 if (fReboot)
346 _tprintf(_T("A reboot is needed to complete the driver (un)installation!\n"));
347 }
348 }
349 }
350
351 if (NULL != hDIFxAPI)
352 FreeLibrary(hDIFxAPI);
353
354 return SUCCEEDED(hr) ? EXIT_OK : EXIT_FAIL;
355}
356
357/**
358 * Executes a sepcified .INF section to install/uninstall drivers and/or services.
359 *
360 * @return Exit code (EXIT_OK, EXIT_FAIL)
361 * @param pszSection Section to execute; usually it's "DefaultInstall".
362 * @param iMode Execution mode to use (see MSDN).
363 * @param pszInf Full qualified path of the .INF file to use.
364 */
365int ExecuteInfFile(const _TCHAR *pszSection, int iMode, const _TCHAR *pszInf)
366{
367 _tprintf(_T("Executing INF-File: %ws (Section: %ws) ...\n"), pszInf, pszSection);
368
369 /* Executed by the installer that already has proper privileges. */
370 _TCHAR szCommandLine[_MAX_PATH + 1] = { 0 };
371 swprintf(szCommandLine, sizeof(szCommandLine), TEXT( "%ws %d %ws" ), pszSection, iMode, pszInf);
372
373#ifdef DEBUG
374 _tprintf (_T( "Commandline: %ws\n"), szCommandLine);
375#endif
376
377 InstallHinfSection(NULL, NULL, szCommandLine, SW_SHOW);
378 /* No return value given! */
379
380 return EXIT_OK;
381}
382
383/**
384 * Adds a string entry to a MULTI_SZ registry list.
385 *
386 * @return Exit code (EXIT_OK, EXIT_FAIL)
387 * @param pszSubKey Sub key containing the list.
388 * @param pszKeyValue The actual key name of the list.
389 * @param pszValueToRemove The value to add to the list.
390 * @param uiOrder Position (zero-based) of where to add the value to the list.
391 */
392int RegistryAddStringToMultiSZ(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToAdd, unsigned int uiOrder)
393{
394#ifdef DEBUG
395 _tprintf(_T("AddStringToMultiSZ: Adding MULTI_SZ string %ws to %ws\\%ws (Order = %d)\n"), pszValueToAdd, pszSubKey, pszKeyValue, uiOrder);
396#endif
397
398 HKEY hKey = NULL;
399 DWORD disp, dwType;
400 LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, &disp);
401 if (lRet != ERROR_SUCCESS)
402 _tprintf(_T("AddStringToMultiSZ: RegCreateKeyEx %ts failed with error %ld!\n"), pszSubKey, lRet);
403
404 if (lRet == ERROR_SUCCESS)
405 {
406 TCHAR szKeyValue[512] = { 0 };
407 TCHAR szNewKeyValue[512] = { 0 };
408 DWORD cbKeyValue = sizeof(szKeyValue);
409
410 lRet = RegQueryValueEx(hKey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
411 if ( lRet != ERROR_SUCCESS
412 || dwType != REG_MULTI_SZ)
413 {
414 _tprintf(_T("AddStringToMultiSZ: RegQueryValueEx failed with error %ld, key type = 0x%x!\n"), lRet, dwType);
415 }
416 else
417 {
418
419 /* Look if the network provider is already in the list. */
420 int iPos = 0;
421 size_t cb = 0;
422
423 /* Replace delimiting "\0"'s with "," to make tokenizing work. */
424 for (int i = 0; i < cbKeyValue / sizeof(TCHAR); i++)
425 if (szKeyValue[i] == '\0') szKeyValue[i] = ',';
426
427 TCHAR *pszToken = wcstok(szKeyValue, _T(","));
428 TCHAR *pszNewToken = NULL;
429 TCHAR *pNewKeyValuePos = szNewKeyValue;
430 while (pszToken != NULL)
431 {
432 pszNewToken = wcstok(NULL, _T(","));
433
434 /* Append new value (at beginning if iOrder=0). */
435 if (iPos == uiOrder)
436 {
437 memcpy(pNewKeyValuePos, pszValueToAdd, wcslen(pszValueToAdd)*sizeof(TCHAR));
438
439 cb += (wcslen(pszValueToAdd) + 1) * sizeof(TCHAR); /* Add trailing zero as well. */
440 pNewKeyValuePos += wcslen(pszValueToAdd) + 1;
441 iPos++;
442 }
443
444 if (0 != wcsicmp(pszToken, pszValueToAdd))
445 {
446 memcpy(pNewKeyValuePos, pszToken, wcslen(pszToken)*sizeof(TCHAR));
447 cb += (wcslen(pszToken) + 1) * sizeof(TCHAR); /* Add trailing zero as well. */
448 pNewKeyValuePos += wcslen(pszToken) + 1;
449 iPos++;
450 }
451
452 pszToken = pszNewToken;
453 }
454
455 /* Append as last item if needed. */
456 if (uiOrder >= iPos)
457 {
458 memcpy(pNewKeyValuePos, pszValueToAdd, wcslen(pszValueToAdd)*sizeof(TCHAR));
459 cb += wcslen(pszValueToAdd) * sizeof(TCHAR); /* Add trailing zero as well. */
460 }
461
462 lRet = RegSetValueExW(hKey, pszKeyValue, 0, REG_MULTI_SZ, (LPBYTE)szNewKeyValue, (DWORD)cb);
463 if (lRet != ERROR_SUCCESS)
464 _tprintf(_T("AddStringToMultiSZ: RegSetValueEx failed with error %ld!\n"), lRet);
465 }
466
467 RegCloseKey(hKey);
468 #ifdef DEBUG
469 if (lRet == ERROR_SUCCESS)
470 _tprintf(_T("AddStringToMultiSZ: Value %ws successfully written!\n"), pszValueToAdd);
471 #endif
472 }
473
474 return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
475}
476
477/**
478 * Removes a string entry from a MULTI_SZ registry list.
479 *
480 * @return Exit code (EXIT_OK, EXIT_FAIL)
481 * @param pszSubKey Sub key containing the list.
482 * @param pszKeyValue The actual key name of the list.
483 * @param pszValueToRemove The value to remove from the list.
484 */
485int RegistryRemoveStringFromMultiSZ(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToRemove)
486{
487 // @todo Make string sizes dynamically allocated!
488
489 const TCHAR *pszKey = pszSubKey;
490#ifdef DEBUG
491 _tprintf(_T("RemoveStringFromMultiSZ: Removing MULTI_SZ string: %ws from %ws\\%ws ...\n"), pszValueToRemove, pszSubKey, pszKeyValue);
492#endif
493
494 HKEY hkey;
495 DWORD disp, dwType;
496 LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hkey, &disp);
497 if (lRet != ERROR_SUCCESS)
498 _tprintf(_T("RemoveStringFromMultiSZ: RegCreateKeyEx %ts failed with error %ld!\n"), pszKey, lRet);
499
500 if (lRet == ERROR_SUCCESS)
501 {
502 TCHAR szKeyValue[1024];
503 DWORD cbKeyValue = sizeof(szKeyValue);
504
505 lRet = RegQueryValueEx(hkey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
506 if ( lRet != ERROR_SUCCESS
507 || dwType != REG_MULTI_SZ)
508 {
509 _tprintf(_T("RemoveStringFromMultiSZ: RegQueryValueEx failed with %d, key type = 0x%x!\n"), lRet, dwType);
510 }
511 else
512 {
513 #ifdef DEBUG
514 _tprintf(_T("RemoveStringFromMultiSZ: Current key len: %ld\n"), cbKeyValue);
515 #endif
516
517 TCHAR szCurString[1024] = { 0 };
518 TCHAR szFinalString[1024] = { 0 };
519 int iIndex = 0;
520 int iNewIndex = 0;
521 for (int i = 0; i < cbKeyValue / sizeof(TCHAR); i++)
522 {
523 if (szKeyValue[i] != _T('\0'))
524 szCurString[iIndex++] = szKeyValue[i];
525
526 if ( (!szKeyValue[i] == _T('\0'))
527 && (szKeyValue[i + 1] == _T('\0')))
528 {
529 if (NULL == wcsstr(szCurString, pszValueToRemove))
530 {
531 wcscat(&szFinalString[iNewIndex], szCurString);
532
533 if (iNewIndex == 0)
534 iNewIndex = iIndex;
535 else iNewIndex += iIndex;
536
537 szFinalString[++iNewIndex] = _T('\0');
538 }
539
540 iIndex = 0;
541 ZeroMemory(szCurString, sizeof(szCurString));
542 }
543 }
544 szFinalString[++iNewIndex] = _T('\0');
545 #ifdef DEBUG
546 _tprintf(_T("RemoveStringFromMultiSZ: New key value: %ws (%u bytes)\n"),
547 szFinalString, iNewIndex * sizeof(TCHAR));
548 #endif
549
550 lRet = RegSetValueExW(hkey, pszKeyValue, 0, REG_MULTI_SZ, (LPBYTE)szFinalString, iNewIndex * sizeof(TCHAR));
551 if (lRet != ERROR_SUCCESS)
552 _tprintf(_T("RemoveStringFromMultiSZ: RegSetValueEx failed with %d!\n"), lRet);
553 }
554
555 RegCloseKey(hkey);
556 #ifdef DEBUG
557 if (lRet == ERROR_SUCCESS)
558 _tprintf(_T("RemoveStringFromMultiSZ: Value %ws successfully removed!\n"), pszValueToRemove);
559 #endif
560 }
561
562 return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
563}
564
565/**
566 * Adds a string to a registry string list (STRING_SZ).
567 * Only operates in HKLM for now, needs to be extended later for
568 * using other hives. Only processes lists with a "," separator
569 * at the moment.
570 *
571 * @return Exit code (EXIT_OK, EXIT_FAIL)
572 * @param pszSubKey Sub key containing the list.
573 * @param pszKeyValue The actual key name of the list.
574 * @param pszValueToAdd The value to add to the list.
575 * @param uiOrder Position (zero-based) of where to add the value to the list.
576 * @param dwFlags Flags.
577 */
578int RegistryAddStringToList(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToAdd,
579 unsigned int uiOrder, DWORD dwFlags)
580{
581 HKEY hKey = NULL;
582 DWORD disp, dwType;
583 LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, &disp);
584 if (lRet != ERROR_SUCCESS)
585 _tprintf(_T("RegistryAddStringToList: RegCreateKeyEx %ts failed with error %ld!\n"), pszSubKey, lRet);
586
587 TCHAR szKeyValue[512] = { 0 };
588 TCHAR szNewKeyValue[512] = { 0 };
589 DWORD cbKeyValue = sizeof(szKeyValue);
590
591 lRet = RegQueryValueEx(hKey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
592 if ( lRet != ERROR_SUCCESS
593 || dwType != REG_SZ)
594 {
595 _tprintf(_T("RegistryAddStringToList: RegQueryValueEx failed with %d, key type = 0x%x!\n"), lRet, dwType);
596 }
597
598 if (lRet == ERROR_SUCCESS)
599 {
600 #ifdef DEBUG
601 _tprintf(_T("RegistryAddStringToList: Key value: %ws\n"), szKeyValue);
602 #endif
603
604 /* Create entire new list. */
605 unsigned int iPos = 0;
606 TCHAR *pszToken = wcstok(szKeyValue, _T(","));
607 TCHAR *pszNewToken = NULL;
608 while (pszToken != NULL)
609 {
610 pszNewToken = wcstok(NULL, _T(","));
611
612 /* Append new provider name (at beginning if iOrder=0). */
613 if (iPos == uiOrder)
614 {
615 wcscat(szNewKeyValue, pszValueToAdd);
616 wcscat(szNewKeyValue, _T(","));
617 iPos++;
618 }
619
620 BOOL fAddToList = FALSE;
621 if ( !wcsicmp(pszToken, pszValueToAdd)
622 && (dwFlags & VBOX_REG_STRINGLIST_ALLOW_DUPLICATES))
623 fAddToList = TRUE;
624 else if (wcsicmp(pszToken, pszValueToAdd))
625 fAddToList = TRUE;
626
627 if (fAddToList)
628 {
629 wcscat(szNewKeyValue, pszToken);
630 wcscat(szNewKeyValue, _T(","));
631 iPos++;
632 }
633
634 #ifdef DEBUG
635 _tprintf (_T("RegistryAddStringToList: Temp new key value: %ws\n"), szNewKeyValue);
636 #endif
637 pszToken = pszNewToken;
638 }
639
640 /* Append as last item if needed. */
641 if (uiOrder >= iPos)
642 wcscat(szNewKeyValue, pszValueToAdd);
643
644 /* Last char a delimiter? Cut off ... */
645 if (szNewKeyValue[wcslen(szNewKeyValue) - 1] == ',')
646 szNewKeyValue[wcslen(szNewKeyValue) - 1] = '\0';
647
648 size_t iNewLen = (wcslen(szNewKeyValue) * sizeof(WCHAR)) + sizeof(WCHAR);
649
650 #ifdef DEBUG
651 _tprintf(_T("RegistryAddStringToList: New provider list: %ws (%u bytes)\n"), szNewKeyValue, iNewLen);
652 #endif
653
654 lRet = RegSetValueExW(hKey, pszKeyValue, 0, REG_SZ, (LPBYTE)szNewKeyValue, (DWORD)iNewLen);
655 if (lRet != ERROR_SUCCESS)
656 _tprintf(_T("RegistryAddStringToList: RegSetValueEx failed with %ld!\n"), lRet);
657 }
658
659 RegCloseKey(hKey);
660 return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
661}
662
663/**
664 * Removes a string from a registry string list (STRING_SZ).
665 * Only operates in HKLM for now, needs to be extended later for
666 * using other hives. Only processes lists with a "," separator
667 * at the moment.
668 *
669 * @return Exit code (EXIT_OK, EXIT_FAIL)
670 * @param pszSubKey Sub key containing the list.
671 * @param pszKeyValue The actual key name of the list.
672 * @param pszValueToRemove The value to remove from the list.
673 */
674int RegistryRemoveStringFromList(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToRemove)
675{
676 HKEY hKey = NULL;
677 DWORD disp, dwType;
678 LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, &disp);
679 if (lRet != ERROR_SUCCESS)
680 _tprintf(_T("RegistryRemoveStringFromList: RegCreateKeyEx %ts failed with error %ld!\n"), pszSubKey, lRet);
681
682 TCHAR szKeyValue[512] = { 0 };
683 TCHAR szNewKeyValue[512] = { 0 };
684 DWORD cbKeyValue = sizeof(szKeyValue);
685
686 lRet = RegQueryValueEx(hKey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
687 if ( lRet != ERROR_SUCCESS
688 || dwType != REG_SZ)
689 {
690 _tprintf(_T("RegistryRemoveStringFromList: RegQueryValueEx failed with %d, key type = 0x%x!\n"), lRet, dwType);
691 }
692
693 if (lRet == ERROR_SUCCESS)
694 {
695 #ifdef DEBUG
696 _tprintf(_T("RegistryRemoveStringFromList: Key value: %ws\n"), szKeyValue);
697 #endif
698
699 /* Create entire new list. */
700 int iPos = 0;
701
702 TCHAR *pszToken = wcstok(szKeyValue, _T(","));
703 TCHAR *pszNewToken = NULL;
704 while (pszToken != NULL)
705 {
706 pszNewToken = wcstok(NULL, _T(","));
707
708 /* Append all list values as long as it's not the
709 * value we want to remove. */
710 if (wcsicmp(pszToken, pszValueToRemove))
711 {
712 wcscat(szNewKeyValue, pszToken);
713 wcscat(szNewKeyValue, _T(","));
714 iPos++;
715 }
716
717 #ifdef DEBUG
718 _tprintf (_T("RegistryRemoveStringFromList: Temp new key value: %ws\n"), szNewKeyValue);
719 #endif
720 pszToken = pszNewToken;
721 }
722
723 /* Last char a delimiter? Cut off ... */
724 if (szNewKeyValue[wcslen(szNewKeyValue) - 1] == ',')
725 szNewKeyValue[wcslen(szNewKeyValue) - 1] = '\0';
726
727 size_t iNewLen = (wcslen(szNewKeyValue) * sizeof(WCHAR)) + sizeof(WCHAR);
728
729 #ifdef DEBUG
730 _tprintf(_T("RegistryRemoveStringFromList: New provider list: %ws (%u bytes)\n"), szNewKeyValue, iNewLen);
731 #endif
732
733 lRet = RegSetValueExW(hKey, pszKeyValue, 0, REG_SZ, (LPBYTE)szNewKeyValue, (DWORD)iNewLen);
734 if (lRet != ERROR_SUCCESS)
735 _tprintf(_T("RegistryRemoveStringFromList: RegSetValueEx failed with %ld!\n"), lRet);
736 }
737
738 RegCloseKey(hKey);
739 return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
740}
741
742/**
743 * Adds a network provider with a specified order to the system.
744 *
745 * @return Exit code (EXIT_OK, EXIT_FAIL)
746 * @param pszProvider Name of network provider to add.
747 * @param uiOrder Position in list (zero-based) of where to add.
748 */
749int AddNetworkProvider(const TCHAR *pszProvider, unsigned int uiOrder)
750{
751 _tprintf(_T("Adding network provider \"%ws\" (Order = %u) ...\n"), pszProvider, uiOrder);
752 int rc = RegistryAddStringToList(_T("System\\CurrentControlSet\\Control\\NetworkProvider\\Order"),
753 _T("ProviderOrder"),
754 pszProvider, uiOrder, VBOX_REG_STRINGLIST_NONE /* No flags set */);
755 if (rc == EXIT_OK)
756 _tprintf(_T("Network provider successfully added!\n"));
757 return rc;
758}
759
760/**
761 * Removes a network provider from the system.
762 *
763 * @return Exit code (EXIT_OK, EXIT_FAIL)
764 * @param pszProvider Name of network provider to remove.
765 */
766int RemoveNetworkProvider(const TCHAR *pszProvider)
767{
768 _tprintf(_T("Removing network provider \"%ws\" ...\n"), pszProvider);
769 int rc = RegistryRemoveStringFromList(_T("System\\CurrentControlSet\\Control\\NetworkProvider\\Order"),
770 _T("ProviderOrder"),
771 pszProvider);
772 if (rc == EXIT_OK)
773 _tprintf(_T("Network provider successfully removed!\n"));
774 return rc;
775}
776
777int CreateService(const TCHAR *pszStartStopName,
778 const TCHAR *pszDisplayName,
779 int iServiceType,
780 int iStartType,
781 const TCHAR *pszBinPath,
782 const TCHAR *pszLoadOrderGroup,
783 const TCHAR *pszDependencies,
784 const TCHAR *pszLogonUser,
785 const TCHAR *pszLogonPassword)
786{
787 int rc = ERROR_SUCCESS;
788
789 _tprintf(_T("Installing service %ws (%ws) ...\n"), pszDisplayName, pszStartStopName);
790
791 SC_HANDLE hSCManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);
792 if (hSCManager == NULL)
793 {
794 _tprintf(_T("Could not get handle to SCM! Error: %ld\n"), GetLastError());
795 return EXIT_FAIL;
796 }
797
798 /* Fixup end of multistring. */
799 TCHAR szDepend[ _MAX_PATH ] = { 0 }; /* @todo Use dynamically allocated string here! */
800 if (pszDependencies != NULL)
801 {
802 _tcsnccpy (szDepend, pszDependencies, wcslen(pszDependencies));
803 DWORD len = (DWORD)wcslen (szDepend);
804 szDepend [len + 1] = 0;
805
806 /* Replace comma separator on null separator. */
807 for (DWORD i = 0; i < len; i++)
808 {
809 if (',' == szDepend [i])
810 szDepend [i] = 0;
811 }
812 }
813
814 DWORD dwTag = 0xDEADBEAF;
815 SC_HANDLE hService = CreateService (hSCManager, /* SCManager database handle. */
816 pszStartStopName, /* Name of service. */
817 pszDisplayName, /* Name to display. */
818 SERVICE_ALL_ACCESS, /* Desired access. */
819 iServiceType, /* Service type. */
820 iStartType, /* Start type. */
821 SERVICE_ERROR_NORMAL, /* Error control type. */
822 pszBinPath, /* Service's binary. */
823 pszLoadOrderGroup, /* Ordering group. */
824 (pszLoadOrderGroup != NULL) ? &dwTag : NULL, /* Tag identifier. */
825 (pszDependencies != NULL) ? szDepend : NULL, /* Dependencies. */
826 (pszLogonUser != NULL) ? pszLogonUser: NULL, /* Account. */
827 (pszLogonPassword != NULL) ? pszLogonPassword : NULL); /* Password. */
828 if (NULL == hService)
829 {
830 DWORD dwErr = GetLastError();
831 switch (dwErr)
832 {
833
834 case ERROR_SERVICE_EXISTS:
835 {
836 _tprintf(_T("Service already exists. No installation required. Updating the service config.\n"));
837
838 hService = OpenService (hSCManager, /* SCManager database handle. */
839 pszStartStopName, /* Name of service. */
840 SERVICE_ALL_ACCESS); /* Desired access. */
841 if (NULL == hService)
842 {
843 dwErr = GetLastError();
844 _tprintf(_T("Could not open service! Error: %ld\n"), dwErr);
845 }
846 else
847 {
848 BOOL fResult = ChangeServiceConfig (hService, /* Service handle. */
849 iServiceType, /* Service type. */
850 iStartType, /* Start type. */
851 SERVICE_ERROR_NORMAL, /* Error control type. */
852 pszBinPath, /* Service's binary. */
853 pszLoadOrderGroup, /* Ordering group. */
854 (pszLoadOrderGroup != NULL) ? &dwTag : NULL, /* Tag identifier. */
855 (pszDependencies != NULL) ? szDepend : NULL, /* Dependencies. */
856 (pszLogonUser != NULL) ? pszLogonUser: NULL, /* Account. */
857 (pszLogonPassword != NULL) ? pszLogonPassword : NULL, /* Password. */
858 pszDisplayName); /* Name to display. */
859 if (fResult)
860 _tprintf(_T("The service config has been successfully updated.\n"));
861 else
862 {
863 dwErr = GetLastError();
864 _tprintf(_T("Could not change service config! Error: %ld\n"), dwErr);
865 }
866 CloseServiceHandle(hService);
867 }
868
869 /*
870 * This entire branch do not return an error to avoid installations failures,
871 * if updating service parameters. Better to have a running system with old
872 * parameters and the failure information in the installation log.
873 */
874 break;
875 }
876
877 case ERROR_INVALID_PARAMETER:
878
879 _tprintf(_T("Invalid parameter specified!\n"));
880 rc = EXIT_FAIL;
881 break;
882
883 default:
884
885 _tprintf(_T("Could not create service! Error: %ld\n"), dwErr);
886 rc = EXIT_FAIL;
887 break;
888 }
889
890 if (rc == EXIT_FAIL)
891 goto cleanup;
892 }
893 else
894 {
895 CloseServiceHandle (hService);
896 _tprintf(_T("Installation of service successful!\n"));
897 }
898
899cleanup:
900
901 if (hSCManager != NULL)
902 CloseServiceHandle (hSCManager);
903
904 return rc;
905}
906
907int DelService(const TCHAR *pszStartStopName)
908{
909 int rc = ERROR_SUCCESS;
910
911 _tprintf(_T("Deleting service '%ws' ...\n"), pszStartStopName);
912
913 SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
914 SC_HANDLE hService = NULL;
915 if (hSCManager == NULL)
916 {
917 _tprintf(_T("Could not get handle to SCM! Error: %ld\n"), GetLastError());
918 rc = EXIT_FAIL;
919 }
920 else
921 {
922 hService = OpenService(hSCManager, pszStartStopName, SERVICE_ALL_ACCESS);
923 if (NULL == hService)
924 {
925 _tprintf(_T("Could not open service '%ws'! Error: %ld\n"), pszStartStopName, GetLastError());
926 rc = EXIT_FAIL;
927 }
928 }
929
930 if (hService != NULL)
931 {
932 if (LockServiceDatabase(hSCManager))
933 {
934 if (FALSE == DeleteService(hService))
935 {
936 DWORD dwErr = GetLastError();
937 switch (dwErr)
938 {
939
940 case ERROR_SERVICE_MARKED_FOR_DELETE:
941
942 _tprintf(_T("Service '%ws' already marked for deletion.\n"), pszStartStopName);
943 break;
944
945 default:
946
947 _tprintf(_T("Could not delete service '%ws'! Error: %ld\n"), pszStartStopName, GetLastError());
948 rc = EXIT_FAIL;
949 break;
950 }
951 }
952 else
953 {
954 _tprintf(_T("Service '%ws' successfully removed!\n"), pszStartStopName);
955 }
956 UnlockServiceDatabase(hSCManager);
957 }
958 else
959 {
960 _tprintf(_T("Unable to lock service database! Error: %ld\n"), GetLastError());
961 rc = EXIT_FAIL;
962 }
963 CloseServiceHandle(hService);
964 }
965
966 if (hSCManager != NULL)
967 CloseServiceHandle(hSCManager);
968
969 return rc;
970}
971
972DWORD RegistryWrite(HKEY hRootKey,
973 const _TCHAR *pszSubKey,
974 const _TCHAR *pszValueName,
975 DWORD dwType,
976 const BYTE *pbData,
977 DWORD cbData)
978{
979 DWORD lRet;
980 HKEY hKey;
981 lRet = RegCreateKeyEx (hRootKey,
982 pszSubKey,
983 0, /* Reserved */
984 NULL, /* lpClass [in, optional] */
985 0, /* dwOptions [in] */
986 KEY_WRITE,
987 NULL, /* lpSecurityAttributes [in, optional] */
988 &hKey,
989 NULL); /* lpdwDisposition [out, optional] */
990 if (lRet != ERROR_SUCCESS)
991 {
992 _tprintf(_T("Could not open registry key! Error: %ld\n"), GetLastError());
993 }
994 else
995 {
996 lRet = RegSetValueEx(hKey, pszValueName, 0, dwType, (BYTE*)pbData, cbData);
997 if (lRet != ERROR_SUCCESS)
998 _tprintf(_T("Could not write to registry! Error: %ld\n"), GetLastError());
999 RegCloseKey(hKey);
1000
1001 }
1002 return lRet;
1003}
1004
1005void PrintHelp(void)
1006{
1007 _tprintf(_T("VirtualBox Guest Additions Installation Helper for Windows\n"));
1008 _tprintf(_T("Version: %d.%d.%d.%d\n\n"), VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
1009 _tprintf(_T("Syntax:\n"));
1010 _tprintf(_T("\n"));
1011 _tprintf(_T("Drivers:\n"));
1012 _tprintf(_T("\tVBoxDrvInst driver install <inf-file>\n"));
1013 _tprintf(_T("\tVBoxDrvInst driver uninstall <inf-file>\n"));
1014 _tprintf(_T("\tVBoxDrvInst driver executeinf <inf-file>\n"));
1015 _tprintf(_T("\n"));
1016 _tprintf(_T("Network Provider:\n"));
1017 _tprintf(_T("\tVBoxDrvInst netprovider add <name> [order]\n"));
1018 _tprintf(_T("\tVBoxDrvInst netprovider remove <name>\n"));
1019 _tprintf(_T("\n"));
1020 _tprintf(_T("Registry:\n"));
1021 _tprintf(_T("\tVBoxDrvInst registry write <root> <sub key>\n")
1022 _T("\t <key name> <key type> <value>\n")
1023 _T("\t [type] [size]\n"));
1024 _tprintf(_T("\tVBoxDrvInst registry addmultisz <root> <sub key>\n")
1025 _T("\t <value> [order]\n"));
1026 _tprintf(_T("\tVBoxDrvInst registry delmultisz <root> <sub key>\n")
1027 _T("\t <key name> <value to remove>\n"));
1028 /** @todo Add "service" category! */
1029 _tprintf(_T("\n"));
1030}
1031
1032int __cdecl _tmain(int argc, _TCHAR *argv[])
1033{
1034 int rc = EXIT_USAGE;
1035
1036 OSVERSIONINFO OSinfo;
1037 OSinfo.dwOSVersionInfoSize = sizeof(OSinfo);
1038 GetVersionEx(&OSinfo);
1039
1040 if (argc >= 2)
1041 {
1042 if ( !_tcsicmp(argv[1], _T("driver"))
1043 && argc >= 3)
1044 {
1045 _TCHAR szINF[_MAX_PATH] = { 0 }; /* Complete path to INF file.*/
1046 if ( ( !_tcsicmp(argv[2], _T("install"))
1047 || !_tcsicmp(argv[2], _T("uninstall")))
1048 && argc == 4)
1049 {
1050 if (OSinfo.dwMajorVersion < 5)
1051 {
1052 _tprintf(_T("ERROR: Platform not supported for driver (un)installation!\n"));
1053 rc = EXIT_FAIL;
1054 }
1055 else
1056 {
1057 _stprintf(szINF, _T("%ws"), argv[3]);
1058 rc = VBoxInstallDriver(!_tcsicmp(argv[2], _T("install")) ? TRUE : FALSE, szINF, FALSE);
1059 }
1060 }
1061 else if ( !_tcsicmp(argv[2], _T("executeinf"))
1062 && argc == 4)
1063 {
1064 _stprintf(szINF, _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 _stprintf(szProvider, _T("%ws"), argv[3]);
1079 rc = AddNetworkProvider(szProvider, iOrder);
1080 }
1081 else if ( !_tcsicmp(argv[2], _T("remove"))
1082 && argc >= 4)
1083 {
1084 _stprintf(szProvider, _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
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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