VirtualBox

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

Last change on this file since 93115 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 50.5 KB
Line 
1/* $Id: VBoxDrvInst.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * VBoxDrvInst - Driver and service installation helper for Windows guests.
4 */
5
6/*
7 * Copyright (C) 2011-2022 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 <iprt/cdefs.h>
27#include <VBox/version.h>
28
29#include <iprt/win/windows.h>
30#include <iprt/win/setupapi.h>
31#include <stdio.h>
32#include <tchar.h>
33
34
35/*********************************************************************************************************************************
36* Defines *
37*********************************************************************************************************************************/
38
39/* Exit codes */
40#define EXIT_OK (0)
41#define EXIT_REBOOT (1)
42#define EXIT_FAIL (2)
43#define EXIT_USAGE (3)
44
45/* Prototypes */
46typedef struct {
47 PWSTR pApplicationId;
48 PWSTR pDisplayName;
49 PWSTR pProductName;
50 PWSTR pMfgName;
51} INSTALLERINFO, *PINSTALLERINFO;
52typedef const PINSTALLERINFO PCINSTALLERINFO;
53
54typedef enum {
55 DIFXAPI_SUCCESS,
56 DIFXAPI_INFO,
57 DIFXAPI_WARNING,
58 DIFXAPI_ERROR
59} DIFXAPI_LOG;
60
61typedef void (WINAPI * DIFXLOGCALLBACK_W)( DIFXAPI_LOG Event, DWORD Error, PCWSTR EventDescription, PVOID CallbackContext);
62typedef void ( __cdecl* DIFXAPILOGCALLBACK_W)( DIFXAPI_LOG Event, DWORD Error, PCWSTR EventDescription, PVOID CallbackContext);
63
64typedef DWORD (WINAPI *fnDriverPackageInstall) (PCTSTR DriverPackageInfPath, DWORD Flags, PCINSTALLERINFO pInstallerInfo, BOOL *pNeedReboot);
65fnDriverPackageInstall g_pfnDriverPackageInstall = NULL;
66
67typedef DWORD (WINAPI *fnDriverPackageUninstall) (PCTSTR DriverPackageInfPath, DWORD Flags, PCINSTALLERINFO pInstallerInfo, BOOL *pNeedReboot);
68fnDriverPackageUninstall g_pfnDriverPackageUninstall = NULL;
69
70typedef VOID (WINAPI *fnDIFXAPISetLogCallback) (DIFXAPILOGCALLBACK_W LogCallback, PVOID CallbackContext);
71fnDIFXAPISetLogCallback g_pfnDIFXAPISetLogCallback = NULL;
72
73/* Defines */
74#define DRIVER_PACKAGE_REPAIR 0x00000001
75#define DRIVER_PACKAGE_SILENT 0x00000002
76#define DRIVER_PACKAGE_FORCE 0x00000004
77#define DRIVER_PACKAGE_ONLY_IF_DEVICE_PRESENT 0x00000008
78#define DRIVER_PACKAGE_LEGACY_MODE 0x00000010
79#define DRIVER_PACKAGE_DELETE_FILES 0x00000020
80
81/* DIFx error codes */
82/** @todo any reason why we're not using difxapi.h instead of these redefinitions? */
83#ifndef ERROR_DRIVER_STORE_ADD_FAILED
84# define ERROR_DRIVER_STORE_ADD_FAILED \
85 (APPLICATION_ERROR_MASK | ERROR_SEVERITY_ERROR | 0x0247L)
86#endif
87#define ERROR_DEPENDENT_APPLICATIONS_EXIST \
88 (APPLICATION_ERROR_MASK|ERROR_SEVERITY_ERROR|0x300)
89#define ERROR_DRIVER_PACKAGE_NOT_IN_STORE \
90 (APPLICATION_ERROR_MASK|ERROR_SEVERITY_ERROR|0x302)
91
92/* Registry string list flags */
93#define VBOX_REG_STRINGLIST_NONE 0x00000000 /* No flags set. */
94#define VBOX_REG_STRINGLIST_ALLOW_DUPLICATES 0x00000001 /* Allows duplicates in list when adding a value. */
95
96#ifdef DEBUG
97# define VBOX_DRVINST_LOGFILE "C:\\Temp\\VBoxDrvInstDIFx.log"
98#endif
99
100/** @todo Get rid of all that TCHAR crap! Use WCHAR wherever possible. */
101
102bool GetErrorMsg(DWORD dwLastError, _TCHAR *pszMsg, DWORD dwBufSize)
103{
104 if (::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwLastError, 0, pszMsg, dwBufSize / sizeof(TCHAR), NULL) == 0)
105 {
106 _sntprintf(pszMsg, dwBufSize / sizeof(TCHAR), _T("Unknown error!\n"));
107 return false;
108 }
109 _TCHAR *p = _tcschr(pszMsg, _T('\r'));
110 if (p != NULL)
111 *p = _T('\0');
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: %lu - %ws\n"), Event, dwError, pEventDescription);
129
130 if (pCallbackContext)
131 fwprintf((FILE*)pCallbackContext, _T("(%u) %lu - %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 case ERROR_NO_SUCH_DEVINST:
370 _tprintf(_T("INFO: The driver package was installed but no matching devices found in the device tree (ERROR_NO_SUCH_DEVINST).\n"));
371 break;
372
373 default:
374 {
375 /* Try error lookup with GetErrorMsg(). */
376 TCHAR szErrMsg[1024];
377 GetErrorMsg(dwRet, szErrMsg, sizeof(szErrMsg));
378 _tprintf(_T("ERROR (%08lx): %ws\n"), dwRet, szErrMsg);
379 break;
380 }
381 }
382
383 if (dwRet == ERROR_NO_SUCH_DEVINST)
384 {
385 /* GA installer should ignore this error code and continue */
386 hr = S_OK;
387 }
388 else
389 hr = HRESULT_FROM_WIN32(dwRet);
390 }
391 g_pfnDIFXAPISetLogCallback(NULL, NULL);
392 if (phFile)
393 fclose(phFile);
394 if (SUCCEEDED(hr))
395 {
396 _tprintf(_T("Driver was %sinstalled successfully!\n"), fInstall ? _T("") : _T("un"));
397 if (fReboot)
398 _tprintf(_T("A reboot is needed to complete the driver %sinstallation!\n"), fInstall ? _T("") : _T("un"));
399 }
400 }
401 }
402
403 if (NULL != hDIFxAPI)
404 FreeLibrary(hDIFxAPI);
405
406 return SUCCEEDED(hr) ? EXIT_OK : EXIT_FAIL;
407}
408
409static UINT WINAPI vboxDrvInstExecuteInfFileCallback(PVOID Context,
410 UINT Notification,
411 UINT_PTR Param1,
412 UINT_PTR Param2) RT_NOTHROW_DEF
413{
414#ifdef DEBUG
415 _tprintf (_T( "Got installation notification %u\n"), Notification);
416#endif
417
418 switch (Notification)
419 {
420 case SPFILENOTIFY_NEEDMEDIA:
421 _tprintf (_T( "Requesting installation media ...\n"));
422 break;
423
424 case SPFILENOTIFY_STARTCOPY:
425 _tprintf (_T( "Copying driver files to destination ...\n"));
426 break;
427
428 case SPFILENOTIFY_TARGETNEWER:
429 case SPFILENOTIFY_TARGETEXISTS:
430 return TRUE;
431 }
432
433 return SetupDefaultQueueCallback(Context, Notification, Param1, Param2);
434}
435
436/**
437 * Executes a sepcified .INF section to install/uninstall drivers and/or services.
438 *
439 * @return Exit code (EXIT_OK, EXIT_FAIL)
440 * @param pszSection Section to execute; usually it's "DefaultInstall".
441 * @param iMode Execution mode to use (see MSDN).
442 * @param pszInf Full qualified path of the .INF file to use.
443 */
444int ExecuteInfFile(const _TCHAR *pszSection, int iMode, const _TCHAR *pszInf)
445{
446 RT_NOREF(iMode);
447 _tprintf(_T("Installing from INF-File: %ws (Section: %ws) ...\n"),
448 pszInf, pszSection);
449
450 UINT uErrorLine = 0;
451 HINF hINF = SetupOpenInfFile(pszInf, NULL, INF_STYLE_WIN4, &uErrorLine);
452 if (hINF != INVALID_HANDLE_VALUE)
453 {
454 PVOID pvQueue = SetupInitDefaultQueueCallback(NULL);
455
456 BOOL fSuccess = SetupInstallFromInfSection(NULL,
457 hINF,
458 pszSection,
459 SPINST_ALL,
460 HKEY_LOCAL_MACHINE,
461 NULL,
462 SP_COPY_NEWER_OR_SAME | SP_COPY_NOSKIP,
463 vboxDrvInstExecuteInfFileCallback,
464 pvQueue,
465 NULL,
466 NULL
467 );
468 if (fSuccess)
469 {
470 _tprintf (_T( "File installation stage successful\n"));
471
472 fSuccess = SetupInstallServicesFromInfSection(hINF,
473 L"DefaultInstall.Services",
474 0 /* Flags */);
475 if (fSuccess)
476 {
477 _tprintf (_T( "Service installation stage successful. Installation completed\n"));
478 }
479 else
480 {
481 DWORD dwErr = GetLastError();
482 switch (dwErr)
483 {
484 case ERROR_SUCCESS_REBOOT_REQUIRED:
485 _tprintf (_T( "A reboot is required to complete the installation\n"));
486 break;
487
488 case ERROR_SECTION_NOT_FOUND:
489 break;
490
491 default:
492 _tprintf (_T( "Error %ld while installing service\n"), dwErr);
493 break;
494 }
495 }
496 }
497 else
498 _tprintf (_T( "Error %ld while installing files\n"), GetLastError());
499
500 if (pvQueue)
501 SetupTermDefaultQueueCallback(pvQueue);
502
503 SetupCloseInfFile(hINF);
504 }
505 else
506 _tprintf (_T( "Unable to open %ws: %ld (error line %u)\n"),
507 pszInf, GetLastError(), uErrorLine);
508
509 return EXIT_OK;
510}
511
512/**
513 * Adds a string entry to a MULTI_SZ registry list.
514 *
515 * @return Exit code (EXIT_OK, EXIT_FAIL)
516 * @param pszSubKey Sub key containing the list.
517 * @param pszKeyValue The actual key name of the list.
518 * @param pszValueToAdd The value to add to the list.
519 * @param uiOrder Position (zero-based) of where to add the value to the list.
520 */
521int RegistryAddStringToMultiSZ(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToAdd, unsigned int uiOrder)
522{
523#ifdef DEBUG
524 _tprintf(_T("AddStringToMultiSZ: Adding MULTI_SZ string %ws to %ws\\%ws (Order = %d)\n"), pszValueToAdd, pszSubKey, pszKeyValue, uiOrder);
525#endif
526
527 HKEY hKey = NULL;
528 DWORD disp, dwType;
529 LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, &disp);
530 if (lRet != ERROR_SUCCESS)
531 _tprintf(_T("AddStringToMultiSZ: RegCreateKeyEx %s failed with error %ld!\n"), pszSubKey, lRet);
532
533 if (lRet == ERROR_SUCCESS)
534 {
535 TCHAR szKeyValue[512] = { 0 };
536 TCHAR szNewKeyValue[512] = { 0 };
537 DWORD cbKeyValue = sizeof(szKeyValue);
538
539 lRet = RegQueryValueEx(hKey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
540 if ( lRet != ERROR_SUCCESS
541 || dwType != REG_MULTI_SZ)
542 {
543 _tprintf(_T("AddStringToMultiSZ: RegQueryValueEx failed with error %ld, key type = %#lx!\n"), lRet, dwType);
544 }
545 else
546 {
547
548 /* Look if the network provider is already in the list. */
549 unsigned int iPos = 0;
550 size_t cb = 0;
551
552 /* Replace delimiting "\0"'s with "," to make tokenizing work. */
553 for (unsigned i = 0; i < cbKeyValue / sizeof(TCHAR); i++)
554 if (szKeyValue[i] == '\0') szKeyValue[i] = ',';
555
556 TCHAR *pszToken = wcstok(szKeyValue, _T(","));
557 TCHAR *pszNewToken = NULL;
558 TCHAR *pNewKeyValuePos = szNewKeyValue;
559 while (pszToken != NULL)
560 {
561 pszNewToken = wcstok(NULL, _T(","));
562
563 /* Append new value (at beginning if iOrder=0). */
564 if (iPos == uiOrder)
565 {
566 memcpy(pNewKeyValuePos, pszValueToAdd, wcslen(pszValueToAdd)*sizeof(TCHAR));
567
568 cb += (wcslen(pszValueToAdd) + 1) * sizeof(TCHAR); /* Add trailing zero as well. */
569 pNewKeyValuePos += wcslen(pszValueToAdd) + 1;
570 iPos++;
571 }
572
573 if (0 != wcsicmp(pszToken, pszValueToAdd))
574 {
575 memcpy(pNewKeyValuePos, pszToken, wcslen(pszToken)*sizeof(TCHAR));
576 cb += (wcslen(pszToken) + 1) * sizeof(TCHAR); /* Add trailing zero as well. */
577 pNewKeyValuePos += wcslen(pszToken) + 1;
578 iPos++;
579 }
580
581 pszToken = pszNewToken;
582 }
583
584 /* Append as last item if needed. */
585 if (uiOrder >= iPos)
586 {
587 memcpy(pNewKeyValuePos, pszValueToAdd, wcslen(pszValueToAdd)*sizeof(TCHAR));
588 cb += wcslen(pszValueToAdd) * sizeof(TCHAR); /* Add trailing zero as well. */
589 }
590
591 lRet = RegSetValueExW(hKey, pszKeyValue, 0, REG_MULTI_SZ, (LPBYTE)szNewKeyValue, (DWORD)cb);
592 if (lRet != ERROR_SUCCESS)
593 _tprintf(_T("AddStringToMultiSZ: RegSetValueEx failed with error %ld!\n"), lRet);
594 }
595
596 RegCloseKey(hKey);
597 #ifdef DEBUG
598 if (lRet == ERROR_SUCCESS)
599 _tprintf(_T("AddStringToMultiSZ: Value %ws successfully written!\n"), pszValueToAdd);
600 #endif
601 }
602
603 return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
604}
605
606/**
607 * Removes a string entry from a MULTI_SZ registry list.
608 *
609 * @return Exit code (EXIT_OK, EXIT_FAIL)
610 * @param pszSubKey Sub key containing the list.
611 * @param pszKeyValue The actual key name of the list.
612 * @param pszValueToRemove The value to remove from the list.
613 */
614int RegistryRemoveStringFromMultiSZ(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToRemove)
615{
616 /// @todo Make string sizes dynamically allocated!
617
618 const TCHAR *pszKey = pszSubKey;
619#ifdef DEBUG
620 _tprintf(_T("RemoveStringFromMultiSZ: Removing MULTI_SZ string: %ws from %ws\\%ws ...\n"), pszValueToRemove, pszSubKey, pszKeyValue);
621#endif
622
623 HKEY hkey;
624 DWORD disp, dwType;
625 LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hkey, &disp);
626 if (lRet != ERROR_SUCCESS)
627 _tprintf(_T("RemoveStringFromMultiSZ: RegCreateKeyEx %s failed with error %ld!\n"), pszKey, lRet);
628
629 if (lRet == ERROR_SUCCESS)
630 {
631 TCHAR szKeyValue[1024];
632 DWORD cbKeyValue = sizeof(szKeyValue);
633
634 lRet = RegQueryValueEx(hkey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
635 if ( lRet != ERROR_SUCCESS
636 || dwType != REG_MULTI_SZ)
637 {
638 _tprintf(_T("RemoveStringFromMultiSZ: RegQueryValueEx failed with %ld, key type = %#lx!\n"), lRet, dwType);
639 }
640 else
641 {
642#ifdef DEBUG
643 _tprintf(_T("RemoveStringFromMultiSZ: Current key len: %ld\n"), cbKeyValue);
644#endif
645
646 TCHAR szCurString[1024] = { 0 };
647 TCHAR szFinalString[1024] = { 0 };
648 int iIndex = 0;
649 int iNewIndex = 0;
650 for (unsigned i = 0; i < cbKeyValue / sizeof(TCHAR); i++)
651 {
652 if (szKeyValue[i] != _T('\0'))
653 szCurString[iIndex++] = szKeyValue[i];
654
655 if ( (!szKeyValue[i] == _T('\0'))
656 && (szKeyValue[i + 1] == _T('\0')))
657 {
658 if (NULL == wcsstr(szCurString, pszValueToRemove))
659 {
660 wcscat(&szFinalString[iNewIndex], szCurString);
661
662 if (iNewIndex == 0)
663 iNewIndex = iIndex;
664 else iNewIndex += iIndex;
665
666 szFinalString[++iNewIndex] = _T('\0');
667 }
668
669 iIndex = 0;
670 ZeroMemory(szCurString, sizeof(szCurString));
671 }
672 }
673 szFinalString[++iNewIndex] = _T('\0');
674#ifdef DEBUG
675 _tprintf(_T("RemoveStringFromMultiSZ: New key value: %ws (%u bytes)\n"),
676 szFinalString, (unsigned)(iNewIndex * sizeof(TCHAR)));
677#endif
678
679 lRet = RegSetValueExW(hkey, pszKeyValue, 0, REG_MULTI_SZ, (LPBYTE)szFinalString, iNewIndex * sizeof(TCHAR));
680 if (lRet != ERROR_SUCCESS)
681 _tprintf(_T("RemoveStringFromMultiSZ: RegSetValueEx failed with %ld!\n"), lRet);
682 }
683
684 RegCloseKey(hkey);
685#ifdef DEBUG
686 if (lRet == ERROR_SUCCESS)
687 _tprintf(_T("RemoveStringFromMultiSZ: Value %ws successfully removed!\n"), pszValueToRemove);
688#endif
689 }
690
691 return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
692}
693
694/**
695 * Adds a string to a registry string list (STRING_SZ).
696 * Only operates in HKLM for now, needs to be extended later for
697 * using other hives. Only processes lists with a "," separator
698 * at the moment.
699 *
700 * @return Exit code (EXIT_OK, EXIT_FAIL)
701 * @param pszSubKey Sub key containing the list.
702 * @param pszKeyValue The actual key name of the list.
703 * @param pszValueToAdd The value to add to the list.
704 * @param uiOrder Position (zero-based) of where to add the value to the list.
705 * @param dwFlags Flags.
706 */
707int RegistryAddStringToList(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToAdd,
708 unsigned int uiOrder, DWORD dwFlags)
709{
710 HKEY hKey = NULL;
711 DWORD disp, dwType;
712 LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, &disp);
713 if (lRet != ERROR_SUCCESS)
714 _tprintf(_T("RegistryAddStringToList: RegCreateKeyEx %s failed with error %ld!\n"), pszSubKey, lRet);
715
716 TCHAR szKeyValue[512] = { 0 };
717 TCHAR szNewKeyValue[512] = { 0 };
718 DWORD cbKeyValue = sizeof(szKeyValue);
719
720 lRet = RegQueryValueEx(hKey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
721 if ( lRet != ERROR_SUCCESS
722 || dwType != REG_SZ)
723 {
724 _tprintf(_T("RegistryAddStringToList: RegQueryValueEx failed with %ld, key type = %#lx!\n"), lRet, dwType);
725 }
726
727 if (lRet == ERROR_SUCCESS)
728 {
729#ifdef DEBUG
730 _tprintf(_T("RegistryAddStringToList: Key value: %ws\n"), szKeyValue);
731#endif
732
733 /* Create entire new list. */
734 unsigned int iPos = 0;
735 TCHAR *pszToken = wcstok(szKeyValue, _T(","));
736 TCHAR *pszNewToken = NULL;
737 while (pszToken != NULL)
738 {
739 pszNewToken = wcstok(NULL, _T(","));
740
741 /* Append new provider name (at beginning if iOrder=0). */
742 if (iPos == uiOrder)
743 {
744 wcscat(szNewKeyValue, pszValueToAdd);
745 wcscat(szNewKeyValue, _T(","));
746 iPos++;
747 }
748
749 BOOL fAddToList = FALSE;
750 if ( !wcsicmp(pszToken, pszValueToAdd)
751 && (dwFlags & VBOX_REG_STRINGLIST_ALLOW_DUPLICATES))
752 fAddToList = TRUE;
753 else if (wcsicmp(pszToken, pszValueToAdd))
754 fAddToList = TRUE;
755
756 if (fAddToList)
757 {
758 wcscat(szNewKeyValue, pszToken);
759 wcscat(szNewKeyValue, _T(","));
760 iPos++;
761 }
762
763#ifdef DEBUG
764 _tprintf (_T("RegistryAddStringToList: Temp new key value: %ws\n"), szNewKeyValue);
765#endif
766 pszToken = pszNewToken;
767 }
768
769 /* Append as last item if needed. */
770 if (uiOrder >= iPos)
771 wcscat(szNewKeyValue, pszValueToAdd);
772
773 /* Last char a delimiter? Cut off ... */
774 if (szNewKeyValue[wcslen(szNewKeyValue) - 1] == ',')
775 szNewKeyValue[wcslen(szNewKeyValue) - 1] = '\0';
776
777 size_t iNewLen = (wcslen(szNewKeyValue) * sizeof(WCHAR)) + sizeof(WCHAR);
778
779#ifdef DEBUG
780 _tprintf(_T("RegistryAddStringToList: New provider list: %ws (%u bytes)\n"), szNewKeyValue, (unsigned)iNewLen);
781#endif
782
783 lRet = RegSetValueExW(hKey, pszKeyValue, 0, REG_SZ, (LPBYTE)szNewKeyValue, (DWORD)iNewLen);
784 if (lRet != ERROR_SUCCESS)
785 _tprintf(_T("RegistryAddStringToList: RegSetValueEx failed with %ld!\n"), lRet);
786 }
787
788 RegCloseKey(hKey);
789 return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
790}
791
792/**
793 * Removes a string from a registry string list (STRING_SZ).
794 * Only operates in HKLM for now, needs to be extended later for
795 * using other hives. Only processes lists with a "," separator
796 * at the moment.
797 *
798 * @return Exit code (EXIT_OK, EXIT_FAIL)
799 * @param pszSubKey Sub key containing the list.
800 * @param pszKeyValue The actual key name of the list.
801 * @param pszValueToRemove The value to remove from the list.
802 */
803int RegistryRemoveStringFromList(const TCHAR *pszSubKey, const TCHAR *pszKeyValue, const TCHAR *pszValueToRemove)
804{
805 HKEY hKey = NULL;
806 DWORD disp, dwType;
807 LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, &disp);
808 if (lRet != ERROR_SUCCESS)
809 _tprintf(_T("RegistryRemoveStringFromList: RegCreateKeyEx %s failed with error %ld!\n"), pszSubKey, lRet);
810
811 TCHAR szKeyValue[512] = { 0 };
812 TCHAR szNewKeyValue[512] = { 0 };
813 DWORD cbKeyValue = sizeof(szKeyValue);
814
815 lRet = RegQueryValueEx(hKey, pszKeyValue, NULL, &dwType, (LPBYTE)szKeyValue, &cbKeyValue);
816 if ( lRet != ERROR_SUCCESS
817 || dwType != REG_SZ)
818 {
819 _tprintf(_T("RegistryRemoveStringFromList: RegQueryValueEx failed with %ld, key type = %#lx!\n"), lRet, dwType);
820 }
821
822 if (lRet == ERROR_SUCCESS)
823 {
824 #ifdef DEBUG
825 _tprintf(_T("RegistryRemoveStringFromList: Key value: %ws\n"), szKeyValue);
826 #endif
827
828 /* Create entire new list. */
829 int iPos = 0;
830
831 TCHAR *pszToken = wcstok(szKeyValue, _T(","));
832 TCHAR *pszNewToken = NULL;
833 while (pszToken != NULL)
834 {
835 pszNewToken = wcstok(NULL, _T(","));
836
837 /* Append all list values as long as it's not the
838 * value we want to remove. */
839 if (wcsicmp(pszToken, pszValueToRemove))
840 {
841 wcscat(szNewKeyValue, pszToken);
842 wcscat(szNewKeyValue, _T(","));
843 iPos++;
844 }
845
846 #ifdef DEBUG
847 _tprintf (_T("RegistryRemoveStringFromList: Temp new key value: %ws\n"), szNewKeyValue);
848 #endif
849 pszToken = pszNewToken;
850 }
851
852 /* Last char a delimiter? Cut off ... */
853 if (szNewKeyValue[wcslen(szNewKeyValue) - 1] == ',')
854 szNewKeyValue[wcslen(szNewKeyValue) - 1] = '\0';
855
856 size_t iNewLen = (wcslen(szNewKeyValue) * sizeof(WCHAR)) + sizeof(WCHAR);
857
858 #ifdef DEBUG
859 _tprintf(_T("RegistryRemoveStringFromList: New provider list: %ws (%u bytes)\n"), szNewKeyValue, (unsigned)iNewLen);
860 #endif
861
862 lRet = RegSetValueExW(hKey, pszKeyValue, 0, REG_SZ, (LPBYTE)szNewKeyValue, (DWORD)iNewLen);
863 if (lRet != ERROR_SUCCESS)
864 _tprintf(_T("RegistryRemoveStringFromList: RegSetValueEx failed with %ld!\n"), lRet);
865 }
866
867 RegCloseKey(hKey);
868 return (lRet == ERROR_SUCCESS) ? EXIT_OK : EXIT_FAIL;
869}
870
871/**
872 * Adds a network provider with a specified order to the system.
873 *
874 * @return Exit code (EXIT_OK, EXIT_FAIL)
875 * @param pszProvider Name of network provider to add.
876 * @param uiOrder Position in list (zero-based) of where to add.
877 */
878int AddNetworkProvider(const TCHAR *pszProvider, unsigned int uiOrder)
879{
880 _tprintf(_T("Adding network provider \"%ws\" (Order = %u) ...\n"), pszProvider, uiOrder);
881 int rc = RegistryAddStringToList(_T("System\\CurrentControlSet\\Control\\NetworkProvider\\Order"),
882 _T("ProviderOrder"),
883 pszProvider, uiOrder, VBOX_REG_STRINGLIST_NONE /* No flags set */);
884 if (rc == EXIT_OK)
885 _tprintf(_T("Network provider successfully added!\n"));
886 return rc;
887}
888
889/**
890 * Removes a network provider from the system.
891 *
892 * @return Exit code (EXIT_OK, EXIT_FAIL)
893 * @param pszProvider Name of network provider to remove.
894 */
895int RemoveNetworkProvider(const TCHAR *pszProvider)
896{
897 _tprintf(_T("Removing network provider \"%ws\" ...\n"), pszProvider);
898 int rc = RegistryRemoveStringFromList(_T("System\\CurrentControlSet\\Control\\NetworkProvider\\Order"),
899 _T("ProviderOrder"),
900 pszProvider);
901 if (rc == EXIT_OK)
902 _tprintf(_T("Network provider successfully removed!\n"));
903 return rc;
904}
905
906int CreateService(const TCHAR *pszStartStopName,
907 const TCHAR *pszDisplayName,
908 int iServiceType,
909 int iStartType,
910 const TCHAR *pszBinPath,
911 const TCHAR *pszLoadOrderGroup,
912 const TCHAR *pszDependencies,
913 const TCHAR *pszLogonUser,
914 const TCHAR *pszLogonPassword)
915{
916 int rc = ERROR_SUCCESS;
917
918 _tprintf(_T("Installing service %ws (%ws) ...\n"), pszDisplayName, pszStartStopName);
919
920 SC_HANDLE hSCManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);
921 if (hSCManager == NULL)
922 {
923 _tprintf(_T("Could not get handle to SCM! Error: %ld\n"), GetLastError());
924 return EXIT_FAIL;
925 }
926
927 /* Fixup end of multistring. */
928 TCHAR szDepend[ _MAX_PATH ] = { 0 }; /** @todo Use dynamically allocated string here! */
929 if (pszDependencies != NULL)
930 {
931 _tcsnccpy (szDepend, pszDependencies, wcslen(pszDependencies));
932 DWORD len = (DWORD)wcslen (szDepend);
933 szDepend [len + 1] = 0;
934
935 /* Replace comma separator on null separator. */
936 for (DWORD i = 0; i < len; i++)
937 {
938 if (',' == szDepend [i])
939 szDepend [i] = 0;
940 }
941 }
942
943 DWORD dwTag = 0xDEADBEAF;
944 SC_HANDLE hService = CreateService (hSCManager, /* SCManager database handle. */
945 pszStartStopName, /* Name of service. */
946 pszDisplayName, /* Name to display. */
947 SERVICE_ALL_ACCESS, /* Desired access. */
948 iServiceType, /* Service type. */
949 iStartType, /* Start type. */
950 SERVICE_ERROR_NORMAL, /* Error control type. */
951 pszBinPath, /* Service's binary. */
952 pszLoadOrderGroup, /* Ordering group. */
953 (pszLoadOrderGroup != NULL) ? &dwTag : NULL, /* Tag identifier. */
954 (pszDependencies != NULL) ? szDepend : NULL, /* Dependencies. */
955 (pszLogonUser != NULL) ? pszLogonUser: NULL, /* Account. */
956 (pszLogonPassword != NULL) ? pszLogonPassword : NULL); /* Password. */
957 if (NULL == hService)
958 {
959 DWORD dwErr = GetLastError();
960 switch (dwErr)
961 {
962
963 case ERROR_SERVICE_EXISTS:
964 {
965 _tprintf(_T("Service already exists. No installation required. Updating the service config.\n"));
966
967 hService = OpenService (hSCManager, /* SCManager database handle. */
968 pszStartStopName, /* Name of service. */
969 SERVICE_ALL_ACCESS); /* Desired access. */
970 if (NULL == hService)
971 {
972 dwErr = GetLastError();
973 _tprintf(_T("Could not open service! Error: %ld\n"), dwErr);
974 }
975 else
976 {
977 BOOL fResult = ChangeServiceConfig (hService, /* Service handle. */
978 iServiceType, /* Service type. */
979 iStartType, /* Start type. */
980 SERVICE_ERROR_NORMAL, /* Error control type. */
981 pszBinPath, /* Service's binary. */
982 pszLoadOrderGroup, /* Ordering group. */
983 (pszLoadOrderGroup != NULL) ? &dwTag : NULL, /* Tag identifier. */
984 (pszDependencies != NULL) ? szDepend : NULL, /* Dependencies. */
985 (pszLogonUser != NULL) ? pszLogonUser: NULL, /* Account. */
986 (pszLogonPassword != NULL) ? pszLogonPassword : NULL, /* Password. */
987 pszDisplayName); /* Name to display. */
988 if (fResult)
989 _tprintf(_T("The service config has been successfully updated.\n"));
990 else
991 {
992 dwErr = GetLastError();
993 _tprintf(_T("Could not change service config! Error: %ld\n"), dwErr);
994 }
995 CloseServiceHandle(hService);
996 }
997
998 /*
999 * This entire branch do not return an error to avoid installations failures,
1000 * if updating service parameters. Better to have a running system with old
1001 * parameters and the failure information in the installation log.
1002 */
1003 break;
1004 }
1005
1006 case ERROR_INVALID_PARAMETER:
1007
1008 _tprintf(_T("Invalid parameter specified!\n"));
1009 rc = EXIT_FAIL;
1010 break;
1011
1012 default:
1013
1014 _tprintf(_T("Could not create service! Error: %ld\n"), dwErr);
1015 rc = EXIT_FAIL;
1016 break;
1017 }
1018
1019 if (rc == EXIT_FAIL)
1020 goto cleanup;
1021 }
1022 else
1023 {
1024 CloseServiceHandle (hService);
1025 _tprintf(_T("Installation of service successful!\n"));
1026 }
1027
1028cleanup:
1029
1030 if (hSCManager != NULL)
1031 CloseServiceHandle (hSCManager);
1032
1033 return rc;
1034}
1035
1036int DelService(const TCHAR *pszStartStopName)
1037{
1038 int rc = ERROR_SUCCESS;
1039
1040 _tprintf(_T("Deleting service '%ws' ...\n"), pszStartStopName);
1041
1042 SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
1043 SC_HANDLE hService = NULL;
1044 if (hSCManager == NULL)
1045 {
1046 _tprintf(_T("Could not get handle to SCM! Error: %ld\n"), GetLastError());
1047 rc = EXIT_FAIL;
1048 }
1049 else
1050 {
1051 hService = OpenService(hSCManager, pszStartStopName, SERVICE_ALL_ACCESS);
1052 if (NULL == hService)
1053 {
1054 _tprintf(_T("Could not open service '%ws'! Error: %ld\n"), pszStartStopName, GetLastError());
1055 rc = EXIT_FAIL;
1056 }
1057 }
1058
1059 if (hService != NULL)
1060 {
1061 SC_LOCK hSCLock = LockServiceDatabase(hSCManager);
1062 if (hSCLock != NULL)
1063 {
1064 if (FALSE == DeleteService(hService))
1065 {
1066 DWORD dwErr = GetLastError();
1067 switch (dwErr)
1068 {
1069
1070 case ERROR_SERVICE_MARKED_FOR_DELETE:
1071
1072 _tprintf(_T("Service '%ws' already marked for deletion.\n"), pszStartStopName);
1073 break;
1074
1075 default:
1076
1077 _tprintf(_T("Could not delete service '%ws'! Error: %ld\n"), pszStartStopName, GetLastError());
1078 rc = EXIT_FAIL;
1079 break;
1080 }
1081 }
1082 else
1083 {
1084 _tprintf(_T("Service '%ws' successfully removed!\n"), pszStartStopName);
1085 }
1086 UnlockServiceDatabase(hSCLock);
1087 }
1088 else
1089 {
1090 _tprintf(_T("Unable to lock service database! Error: %ld\n"), GetLastError());
1091 rc = EXIT_FAIL;
1092 }
1093 CloseServiceHandle(hService);
1094 }
1095
1096 if (hSCManager != NULL)
1097 CloseServiceHandle(hSCManager);
1098
1099 return rc;
1100}
1101
1102DWORD RegistryWrite(HKEY hRootKey,
1103 const _TCHAR *pszSubKey,
1104 const _TCHAR *pszValueName,
1105 DWORD dwType,
1106 const BYTE *pbData,
1107 DWORD cbData)
1108{
1109 DWORD lRet;
1110 HKEY hKey;
1111 lRet = RegCreateKeyEx (hRootKey,
1112 pszSubKey,
1113 0, /* Reserved */
1114 NULL, /* lpClass [in, optional] */
1115 0, /* dwOptions [in] */
1116 KEY_WRITE,
1117 NULL, /* lpSecurityAttributes [in, optional] */
1118 &hKey,
1119 NULL); /* lpdwDisposition [out, optional] */
1120 if (lRet != ERROR_SUCCESS)
1121 {
1122 _tprintf(_T("Could not open registry key! Error: %ld\n"), GetLastError());
1123 }
1124 else
1125 {
1126 lRet = RegSetValueEx(hKey, pszValueName, 0, dwType, (BYTE*)pbData, cbData);
1127 if (lRet != ERROR_SUCCESS)
1128 _tprintf(_T("Could not write to registry! Error: %ld\n"), GetLastError());
1129 RegCloseKey(hKey);
1130
1131 }
1132 return lRet;
1133}
1134
1135void PrintHelp(void)
1136{
1137 _tprintf(_T("VirtualBox Guest Additions Installation Helper for Windows\n"));
1138 _tprintf(_T("Version: %d.%d.%d.%d\n\n"), VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
1139 _tprintf(_T("Syntax:\n"));
1140 _tprintf(_T("\n"));
1141 _tprintf(_T("Drivers:\n"));
1142 _tprintf(_T("\tVBoxDrvInst driver install <inf-file> [log file]\n"));
1143 _tprintf(_T("\tVBoxDrvInst driver uninstall <inf-file> [log file]\n"));
1144 _tprintf(_T("\tVBoxDrvInst driver executeinf <inf-file>\n"));
1145 _tprintf(_T("\n"));
1146 _tprintf(_T("Network Provider:\n"));
1147 _tprintf(_T("\tVBoxDrvInst netprovider add <name> [order]\n"));
1148 _tprintf(_T("\tVBoxDrvInst netprovider remove <name>\n"));
1149 _tprintf(_T("\n"));
1150 _tprintf(_T("Registry:\n"));
1151 _tprintf(_T("\tVBoxDrvInst registry write <root> <sub key>\n")
1152 _T("\t <key name> <key type> <value>\n")
1153 _T("\t [type] [size]\n"));
1154 _tprintf(_T("\tVBoxDrvInst registry addmultisz <root> <sub key>\n")
1155 _T("\t <value> [order]\n"));
1156 _tprintf(_T("\tVBoxDrvInst registry delmultisz <root> <sub key>\n")
1157 _T("\t <key name> <value to remove>\n"));
1158 /** @todo Add "service" category! */
1159 _tprintf(_T("\n"));
1160}
1161
1162int __cdecl _tmain(int argc, _TCHAR *argv[])
1163{
1164 int rc = EXIT_USAGE;
1165
1166 OSVERSIONINFO OSinfo;
1167 OSinfo.dwOSVersionInfoSize = sizeof(OSinfo);
1168 GetVersionEx(&OSinfo);
1169
1170 if (argc >= 2)
1171 {
1172 if ( !_tcsicmp(argv[1], _T("driver"))
1173 && argc >= 3)
1174 {
1175 _TCHAR szINF[_MAX_PATH] = { 0 }; /* Complete path to INF file.*/
1176 if ( ( !_tcsicmp(argv[2], _T("install"))
1177 || !_tcsicmp(argv[2], _T("uninstall")))
1178 && argc >= 4)
1179 {
1180 if (OSinfo.dwMajorVersion < 5)
1181 {
1182 _tprintf(_T("ERROR: Platform not supported for driver (un)installation!\n"));
1183 rc = EXIT_FAIL;
1184 }
1185 else
1186 {
1187 _sntprintf(szINF, sizeof(szINF) / sizeof(TCHAR), _T("%ws"), argv[3]);
1188
1189 _TCHAR szLogFile[_MAX_PATH] = { 0 };
1190 if (argc > 4)
1191 _sntprintf(szLogFile, sizeof(szLogFile) / sizeof(TCHAR), _T("%ws"), argv[4]);
1192 rc = VBoxInstallDriver(!_tcsicmp(argv[2], _T("install")) ? TRUE : FALSE, szINF,
1193 FALSE /* Not silent */, szLogFile[0] != NULL ? szLogFile : NULL);
1194 }
1195 }
1196 else if ( !_tcsicmp(argv[2], _T("executeinf"))
1197 && argc == 4)
1198 {
1199 _sntprintf(szINF, sizeof(szINF) / sizeof(TCHAR), _T("%ws"), argv[3]);
1200 rc = ExecuteInfFile(_T("DefaultInstall"), 132, szINF);
1201 }
1202 }
1203 else if ( !_tcsicmp(argv[1], _T("netprovider"))
1204 && argc >= 3)
1205 {
1206 _TCHAR szProvider[_MAX_PATH] = { 0 }; /* The network provider name for the registry. */
1207 if ( !_tcsicmp(argv[2], _T("add"))
1208 && argc >= 4)
1209 {
1210 int iOrder = 0;
1211 if (argc > 4)
1212 iOrder = _ttoi(argv[4]);
1213 _sntprintf(szProvider, sizeof(szProvider) / sizeof(TCHAR), _T("%ws"), argv[3]);
1214 rc = AddNetworkProvider(szProvider, iOrder);
1215 }
1216 else if ( !_tcsicmp(argv[2], _T("remove"))
1217 && argc >= 4)
1218 {
1219 _sntprintf(szProvider, sizeof(szProvider) / sizeof(TCHAR), _T("%ws"), argv[3]);
1220 rc = RemoveNetworkProvider(szProvider);
1221 }
1222 }
1223 else if ( !_tcsicmp(argv[1], _T("service"))
1224 && argc >= 3)
1225 {
1226 if ( !_tcsicmp(argv[2], _T("create"))
1227 && argc >= 8)
1228 {
1229 rc = CreateService(argv[3],
1230 argv[4],
1231 _ttoi(argv[5]),
1232 _ttoi(argv[6]),
1233 argv[7],
1234 (argc > 8) ? argv[8] : NULL,
1235 (argc > 9) ? argv[9] : NULL,
1236 (argc > 10) ? argv[10] : NULL,
1237 (argc > 11) ? argv[11] : NULL);
1238 }
1239 else if ( !_tcsicmp(argv[2], _T("delete"))
1240 && argc == 4)
1241 {
1242 rc = DelService(argv[3]);
1243 }
1244 }
1245 else if ( !_tcsicmp(argv[1], _T("registry"))
1246 && argc >= 3)
1247 {
1248 /** @todo add a handleRegistry(argc, argv) method to keep things cleaner */
1249 if ( !_tcsicmp(argv[2], _T("addmultisz"))
1250 && argc == 7)
1251 {
1252 rc = RegistryAddStringToMultiSZ(argv[3], argv[4], argv[5], _ttoi(argv[6]));
1253 }
1254 else if ( !_tcsicmp(argv[2], _T("delmultisz"))
1255 && argc == 6)
1256 {
1257 rc = RegistryRemoveStringFromMultiSZ(argv[3], argv[4], argv[5]);
1258 }
1259 else if ( !_tcsicmp(argv[2], _T("write"))
1260 && argc >= 8)
1261 {
1262 HKEY hRootKey = HKEY_LOCAL_MACHINE; /** @todo needs to be expanded (argv[3]) */
1263 DWORD dwValSize = 0;
1264 BYTE *pbVal = NULL;
1265 DWORD dwVal;
1266
1267 if (argc > 8)
1268 {
1269 if (!_tcsicmp(argv[8], _T("dword")))
1270 {
1271 dwVal = _ttol(argv[7]);
1272 pbVal = (BYTE*)&dwVal;
1273 dwValSize = sizeof(DWORD);
1274 }
1275 }
1276 if (pbVal == NULL) /* By default interpret value as string */
1277 {
1278 pbVal = (BYTE *)argv[7];
1279 dwValSize = (DWORD)_tcslen(argv[7]);
1280 }
1281 if (argc > 9)
1282 dwValSize = _ttol(argv[9]); /* Get the size in bytes of the value we want to write */
1283 rc = RegistryWrite(hRootKey,
1284 argv[4], /* Sub key */
1285 argv[5], /* Value name */
1286 REG_BINARY, /** @todo needs to be expanded (argv[6]) */
1287 pbVal, /* The value itself */
1288 dwValSize); /* Size of the value */
1289 }
1290#if 0
1291 else if (!_tcsicmp(argv[2], _T("read")))
1292 {
1293 }
1294 else if (!_tcsicmp(argv[2], _T("del")))
1295 {
1296 }
1297#endif
1298 }
1299 else if (!_tcsicmp(argv[1], _T("--version")))
1300 {
1301 _tprintf(_T("%d.%d.%d.%d\n"), VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
1302 rc = EXIT_OK;
1303 }
1304 else if ( !_tcsicmp(argv[1], _T("--help"))
1305 || !_tcsicmp(argv[1], _T("/help"))
1306 || !_tcsicmp(argv[1], _T("/h"))
1307 || !_tcsicmp(argv[1], _T("/?")))
1308 {
1309 PrintHelp();
1310 rc = EXIT_OK;
1311 }
1312 }
1313
1314 if (rc == EXIT_USAGE)
1315 _tprintf(_T("No or wrong parameters given! Please consult the help (\"--help\" or \"/?\") for more information.\n"));
1316
1317 return rc;
1318}
1319
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