VirtualBox

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

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

Windows Guest Additions installer: Log driver installation.

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

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