VirtualBox

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

Last change on this file since 85959 was 85121, checked in by vboxsync, 4 years ago

iprt/cdefs.h: Refactored the typedef use of DECLCALLBACK as well as DECLCALLBACKMEMBER to wrap the whole expression, similar to the DECLR?CALLBACKMEMBER macros. This allows adding a throw() at the end when compiling with the VC++ compiler to indicate that the callbacks won't throw anything, so we can stop supressing the C5039 warning about passing functions that can potential throw C++ exceptions to extern C code that can't necessarily cope with such (unwind,++). Introduced a few _EX variations that allows specifying different/no calling convention too, as that's handy when dynamically resolving host APIs. Fixed numerous places missing DECLCALLBACK and such. Left two angry @todos regarding use of CreateThread. bugref:9794

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