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