VirtualBox

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

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

VBoxDrvInst.cpp: changed loadInstalledDll to fail of GetModuleFileName fails. Also converted it to unicode since we cannot make the same assumption regarding user controlled install/temp dirs as we about the microsoft dictated c:\windows\system32 dir.

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