Changeset 96572 in vbox for trunk/src/VBox/HostDrivers/VBoxNetFlt
- Timestamp:
- Sep 1, 2022 8:36:22 PM (2 years ago)
- Location:
- trunk/src/VBox/HostDrivers/VBoxNetFlt
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/HostDrivers/VBoxNetFlt/Makefile.kmk
r96407 r96572 243 243 # WinNetConfig - static library with host network interface config API (for the installer) 244 244 # 245 LIBRARIES.win += WinNetConfig 246 WinNetConfig_TEMPLATE = VBoxR3StaticDllNoAsan 247 WinNetConfig_SDKS = ReorderCompilerIncs $(VBOX_WINPSDK) $(VBOX_WINDDK) 248 WinNetConfig_DEFS = _WIN32_WINNT=0x0501 _UNICODE UNICODE 249 WinNetConfig_INCS.win = $(PATH_TOOL_$(VBOX_VCC_TOOL)_INC) 250 WinNetConfig_SOURCES = win/cfg/VBoxNetCfg.cpp 251 252 # 253 # WinNetConfigNonStatic - static library with host network interface config API 254 # 255 LIBRARIES.win += WinNetConfigNonStatic 256 WinNetConfigNonStatic_TEMPLATE = VBoxR3Dll 257 WinNetConfigNonStatic_SDKS = ReorderCompilerIncs $(VBOX_WINPSDK) $(VBOX_WINDDK) 258 WinNetConfigNonStatic_DEFS = _WIN32_WINNT=0x0501 _UNICODE UNICODE 259 WinNetConfigNonStatic_INCS.win = $(PATH_TOOL_$(VBOX_VCC_TOOL)_INC) 260 WinNetConfigNonStatic_SOURCES = win/cfg/VBoxNetCfg.cpp 245 LIBRARIES.win += WinNetConfigSharedStatic 246 WinNetConfigSharedStatic_TEMPLATE = VBoxR3StaticDllNoAsan 247 WinNetConfigSharedStatic_SDKS = ReorderCompilerIncs $(VBOX_WINPSDK) $(VBOX_WINDDK) 248 WinNetConfigSharedStatic_DEFS = _UNICODE UNICODE 249 WinNetConfigSharedStatic_SOURCES = \ 250 win/cfg/VBoxNetCfg.cpp \ 251 ../../Main/glue/string.cpp 252 253 # Version for DLLs: 254 LIBRARIES.win += WinNetConfigDll 255 WinNetConfigDll_TEMPLATE = VBoxR3Dll 256 WinNetConfigDll_EXTENDS = WinNetConfigSharedStatic 257 258 # Version for EXEs: 259 LIBRARIES.win += WinNetConfigExe 260 WinNetConfigExe_TEMPLATE = VBOXR3EXE 261 WinNetConfigExe_EXTENDS = WinNetConfigSharedStatic 261 262 262 263 … … 265 266 # 266 267 TEMPLATE_VBoxNetFltR3 = Template for NetFltInstall, NetFltUninstall, NetAdpInstall, NetAdpUninstall, ++ 267 TEMPLATE_VBoxNetFltR3_EXTENDS = VBOXR3STATIC 268 TEMPLATE_VBoxNetFltR3_SDKS = $(TEMPLATE_VBOXR3STATIC_SDKS) ReorderCompilerIncs $(VBOX_WINPSDK) $(VBOX_WINDDK) VBOX_NTDLL VBOX_WIN_NEWDEV 269 TEMPLATE_VBoxNetFltR3_CXXFLAGS = $(TEMPLATE_VBOXR3STATIC_CXXFLAGS) -Gz 270 TEMPLATE_VBoxNetFltR3_CFLAGS = $(TEMPLATE_VBOXR3STATIC_CFLAGS) -Gz 271 TEMPLATE_VBoxNetFltR3_LIBS = $(TEMPLATE_VBOXR3STATIC_LIBS) \ 272 $(WinNetConfig_1_TARGET) \ 273 $(PATH_STAGE_LIB)/VBoxDrvCfg$(VBOX_SUFF_LIB) \ 268 TEMPLATE_VBoxNetFltR3_EXTENDS = VBOXR3EXE 269 TEMPLATE_VBoxNetFltR3_SDKS = $(TEMPLATE_VBOXR3EXE_SDKS) ReorderCompilerIncs $(VBOX_WINPSDK) $(VBOX_WINDDK) VBOX_NTDLL VBOX_WIN_NEWDEV 270 TEMPLATE_VBoxNetFltR3_LIBS = $(TEMPLATE_VBOXR3EXE_LIBS) \ 271 $(WinNetConfigExe_1_TARGET) \ 272 $(PATH_STAGE_LIB)/VBoxDrvCfgExe$(VBOX_SUFF_LIB) \ 273 $(LIB_RUNTIME) \ 274 274 $(PATH_TOOL_$(VBOX_VCC_TOOL)_LIB)/comsupp.lib \ 275 275 $(PATH_SDK_$(VBOX_WINPSDK)_LIB)/WbemUuid.Lib … … 343 343 endif 344 344 VBoxNetFltNobj_SDKS = ReorderCompilerIncs $(VBOX_WINPSDK) $(VBOX_WINDDK) 345 VBoxNetFltNobj_DEFS = _WIN32_WINNT=0x0500WIN32 _ATL_STATIC_REGISTRY345 VBoxNetFltNobj_DEFS = WIN32 _ATL_STATIC_REGISTRY 346 346 VBoxNetFltNobj_INCS = \ 347 347 $(VBoxNetFltNobj_0_OUTDIR) -
trunk/src/VBox/HostDrivers/VBoxNetFlt/win/cfg/VBoxNetCfg.cpp
r96407 r96572 3 3 * VBoxNetCfg.cpp - Network Configuration API. 4 4 */ 5 5 6 /* 6 7 * Copyright (C) 2011-2022 Oracle and/or its affiliates. … … 33 34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 34 35 */ 36 37 38 /********************************************************************************************************************************* 39 * Header Files * 40 *********************************************************************************************************************************/ 41 #define _WIN32_DCOM 42 35 43 #include "VBox/VBoxNetCfg-win.h" 36 44 #include "VBox/VBoxDrvCfg-win.h" 37 45 38 #define _WIN32_DCOM39 40 46 #include <devguid.h> 41 #include <stdio.h>42 47 #include <regstr.h> 43 48 #include <iprt/win/shlobj.h> 44 49 #include <cfgmgr32.h> 45 #include <tchar.h>46 50 #include <iprt/win/objbase.h> 47 51 48 #include <crtdbg.h>49 #include <stdlib.h>50 #include <string.h>51 52 52 #include <Wbemidl.h> 53 #include <comutil.h>54 53 55 54 #include <iprt/win/winsock2.h> … … 59 58 #include <iprt/win/iphlpapi.h> 60 59 61 #include <set> 62 60 #include <iprt/asm.h> 61 #include <iprt/assertcompile.h> 62 #include <iprt/mem.h> 63 #include <iprt/list.h> 64 #include <iprt/rand.h> 65 #include <iprt/string.h> 66 #include <iprt/utf16.h> 67 #include <VBox/com/string.h> 68 69 70 /********************************************************************************************************************************* 71 * Defined Constants And Macros * 72 *********************************************************************************************************************************/ 63 73 #ifndef Assert /** @todo r=bird: where would this be defined? */ 64 74 //# ifdef DEBUG … … 70 80 # define AssertMsg(expr, msg) do{}while (0) 71 81 #endif 82 83 #define NonStandardLog DoLogging 84 #define NonStandardLogFlow(x) DoLogging x 85 86 #define SetErrBreak(strAndArgs) \ 87 if (1) { \ 88 hrc = E_FAIL; \ 89 NonStandardLog strAndArgs; \ 90 bstrError.printfNoThrow strAndArgs; \ 91 break; \ 92 } else do {} while (0) 93 94 95 #define VBOXNETCFGWIN_NETADP_ID_SZ "sun_VBoxNetAdp" 96 #define VBOXNETCFGWIN_NETADP_ID_WSZ RT_CONCAT(L,VBOXNETCFGWIN_NETADP_ID_SZ) 97 #define DRIVERHWID VBOXNETCFGWIN_NETADP_ID_WSZ 98 99 /* We assume the following name matches the device description in vboxnetadp6.inf */ 100 #define HOSTONLY_ADAPTER_NAME_SZ "VirtualBox Host-Only Ethernet Adapter" 101 #define HOSTONLY_ADAPTER_NAME_WSZ RT_CONCAT(L,HOSTONLY_ADAPTER_NAME_SZ) 102 103 #define VBOX_CONNECTION_NAME_SZ "VirtualBox Host-Only Network" 104 #define VBOX_CONNECTION_NAME_WSZ RT_CONCAT(L,VBOX_CONNECTION_NAME_SZ) 105 106 #define VBOXNETCFGWIN_NETLWF_ID L"oracle_VBoxNetLwf" 107 108 109 110 /********************************************************************************************************************************* 111 * Global Variables * 112 *********************************************************************************************************************************/ 72 113 static PFNVBOXNETCFGLOGGER volatile g_pfnLogger = NULL; 73 114 74 static void DoLogging(const char *pszString, ...);75 #define NonStandardLog DoLogging76 #define NonStandardLogFlow(x) DoLogging x77 78 #define DbgLog /** @todo r=bird: What does this do? */79 80 #define VBOX_NETCFG_LOCK_TIME_OUT 5000 /** @todo r=bird: What does this do? */81 82 #define VBOXNETCFGWIN_NETADP_ID L"sun_VBoxNetAdp"83 84 115 /* 85 * Wrappers for HelpAPI functions86 */116 * Wrappers for HelpAPI functions 117 */ 87 118 typedef void FNINITIALIZEIPINTERFACEENTRY( _Inout_ PMIB_IPINTERFACE_ROW row); 88 119 typedef FNINITIALIZEIPINTERFACEENTRY *PFNINITIALIZEIPINTERFACEENTRY; … … 94 125 typedef FNSETIPINTERFACEENTRY *PFNSETIPINTERFACEENTRY; 95 126 96 static PFNINITIALIZEIPINTERFACEENTRY g_pfnInitializeIpInterfaceEntry = NULL; 97 static PFNGETIPINTERFACEENTRY g_pfnGetIpInterfaceEntry = NULL; 98 static PFNSETIPINTERFACEENTRY g_pfnSetIpInterfaceEntry = NULL; 99 127 128 /********************************************************************************************************************************* 129 * Internal Functions * 130 *********************************************************************************************************************************/ 131 static PFNINITIALIZEIPINTERFACEENTRY g_pfnInitializeIpInterfaceEntry = NULL; 132 static PFNGETIPINTERFACEENTRY g_pfnGetIpInterfaceEntry = NULL; 133 static PFNSETIPINTERFACEENTRY g_pfnSetIpInterfaceEntry = NULL; 134 135 static void DoLogging(const char *pszString, ...); 100 136 101 137 /* 102 * Forward declaration for using vboxNetCfgWinSetupMetric() 103 */ 104 HRESULT vboxNetCfgWinSetupMetric(IN NET_LUID* pLuid); 105 HRESULT vboxNetCfgWinGetInterfaceLUID(IN HKEY hKey, OUT NET_LUID* pLUID); 106 107 108 /* 109 * For some weird reason we do not want to use IPRT here, hence the following 110 * function provides a replacement for BstrFmt. 138 * Forward declaration for using vboxNetCfgWinSetupMetric() 111 139 */ 112 static bstr_t bstr_printf(const char *cszFmt, ...) 113 { 114 char szBuffer[4096]; 115 szBuffer[sizeof(szBuffer) - 1] = 0; /* Make sure the string will be null-terminated */ 116 va_list va; 117 va_start(va, cszFmt); 118 _vsnprintf(szBuffer, sizeof(szBuffer) - 1, cszFmt, va); 119 va_end(va); 120 return bstr_t(szBuffer); 121 } 140 static HRESULT vboxNetCfgWinSetupMetric(IN NET_LUID *pLuid); 141 static HRESULT vboxNetCfgWinGetInterfaceLUID(IN HKEY hKey, OUT NET_LUID *pLUID); 142 143 122 144 123 145 static HRESULT vboxNetCfgWinINetCfgLock(IN INetCfg *pNetCfg, … … 127 149 { 128 150 INetCfgLock *pLock; 129 HRESULT hr = pNetCfg->QueryInterface(IID_INetCfgLock, (PVOID *)&pLock);151 HRESULT hr = pNetCfg->QueryInterface(IID_INetCfgLock, (PVOID *)&pLock); 130 152 if (FAILED(hr)) 131 153 { 132 NonStandardLogFlow(("QueryInterface failed , hr (0x%x)\n", hr));154 NonStandardLogFlow(("QueryInterface failed: %Rhrc\n", hr)); 133 155 return hr; 134 156 } … … 136 158 hr = pLock->AcquireWriteLock(cmsTimeout, pszwClientDescription, ppszwClientDescription); 137 159 if (hr == S_FALSE) 138 {139 160 NonStandardLogFlow(("Write lock busy\n")); 140 }141 161 else if (FAILED(hr)) 142 { 143 NonStandardLogFlow(("AcquireWriteLock failed, hr (0x%x)\n", hr)); 144 } 162 NonStandardLogFlow(("AcquireWriteLock failed: %Rhrc\n", hr)); 145 163 146 164 pLock->Release(); … … 151 169 { 152 170 INetCfgLock *pLock; 153 HRESULT hr = pNetCfg->QueryInterface(IID_INetCfgLock, (PVOID *)&pLock);171 HRESULT hr = pNetCfg->QueryInterface(IID_INetCfgLock, (PVOID *)&pLock); 154 172 if (FAILED(hr)) 155 173 { 156 NonStandardLogFlow(("QueryInterface failed , hr (0x%x)\n", hr));174 NonStandardLogFlow(("QueryInterface failed: %Rhrc\n", hr)); 157 175 return hr; 158 176 } … … 160 178 hr = pLock->ReleaseWriteLock(); 161 179 if (FAILED(hr)) 162 NonStandardLogFlow(("ReleaseWriteLock failed , hr (0x%x)\n", hr));180 NonStandardLogFlow(("ReleaseWriteLock failed: %Rhrc\n", hr)); 163 181 164 182 pLock->Release(); … … 172 190 OUT LPWSTR *ppszwClientDescription) 173 191 { 174 INetCfg *pNetCfg ;175 HRESULT hr = CoCreateInstance(CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER, IID_INetCfg, (PVOID *)&pNetCfg);192 INetCfg *pNetCfg = NULL; 193 HRESULT hr = CoCreateInstance(CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER, IID_INetCfg, (PVOID *)&pNetCfg); 176 194 if (FAILED(hr)) 177 195 { 178 NonStandardLogFlow(("CoCreateInstance failed , hr (0x%x)\n", hr));196 NonStandardLogFlow(("CoCreateInstance failed: %Rhrc\n", hr)); 179 197 return hr; 180 198 } … … 198 216 return S_OK; 199 217 } 200 else 201 NonStandardLogFlow(("Initialize failed, hr (0x%x)\n", hr)); 218 NonStandardLogFlow(("Initialize failed: %Rhrc\n", hr)); 202 219 } 203 220 … … 217 234 if (FAILED(hr)) 218 235 { 219 NonStandardLogFlow(("Uninitialize failed , hr (0x%x)\n", hr));236 NonStandardLogFlow(("Uninitialize failed: %Rhrc\n", hr)); 220 237 /* Try to release the write lock below. */ 221 238 } … … 225 242 HRESULT hr2 = vboxNetCfgWinINetCfgUnlock(pNetCfg); 226 243 if (FAILED(hr2)) 227 NonStandardLogFlow(("vboxNetCfgWinINetCfgUnlock failed , hr (0x%x)\n", hr2));244 NonStandardLogFlow(("vboxNetCfgWinINetCfgUnlock failed: %Rhrc\n", hr2)); 228 245 if (SUCCEEDED(hr)) 229 246 hr = hr2; … … 241 258 if (FAILED(hr)) 242 259 { 243 NonStandardLogFlow(("Reset failed , hr (0x%x)\n", hr));260 NonStandardLogFlow(("Reset failed: %Rhrc\n", hr)); 244 261 return hr; 245 262 } 246 263 247 INetCfgComponent *pNcc ;264 INetCfgComponent *pNcc = NULL; 248 265 while ((hr = pEnumNcc->Next(1, &pNcc, NULL)) == S_OK) 249 266 { 250 ULONG uComponentStatus ;267 ULONG uComponentStatus = 0; 251 268 hr = pNcc->GetDeviceStatus(&uComponentStatus); 252 269 if (SUCCEEDED(hr)) … … 267 284 } 268 285 else 269 NonStandardLogFlow(("GetInstanceGuid failed , hr (0x%x)\n", hr));286 NonStandardLogFlow(("GetInstanceGuid failed: %Rhrc\n", hr)); 270 287 } 271 288 } … … 281 298 OUT INetCfgComponent **ppncc) 282 299 { 283 IEnumNetCfgComponent *pEnumNcc ;300 IEnumNetCfgComponent *pEnumNcc = NULL; 284 301 HRESULT hr = pNc->EnumComponents(pguidClass, &pEnumNcc); 285 286 302 if (SUCCEEDED(hr)) 287 303 { 288 304 hr = vboxNetCfgWinGetComponentByGuidEnum(pEnumNcc, pComponentGuid, ppncc); 289 305 if (hr == S_FALSE) 290 {291 306 NonStandardLogFlow(("Component not found\n")); 292 }293 307 else if (FAILED(hr)) 294 { 295 NonStandardLogFlow(("vboxNetCfgWinGetComponentByGuidEnum failed, hr (0x%x)\n", hr)); 296 } 308 NonStandardLogFlow(("vboxNetCfgWinGetComponentByGuidEnum failed: %Rhrc\n", hr)); 297 309 pEnumNcc->Release(); 298 310 } 299 311 else 300 NonStandardLogFlow(("EnumComponents failed , hr (0x%x)\n", hr));312 NonStandardLogFlow(("EnumComponents failed: %Rhrc\n", hr)); 301 313 return hr; 302 314 } … … 304 316 static HRESULT vboxNetCfgWinQueryInstaller(IN INetCfg *pNetCfg, IN const GUID *pguidClass, INetCfgClassSetup **ppSetup) 305 317 { 306 HRESULT hr = pNetCfg->QueryNetCfgClass(pguidClass, IID_INetCfgClassSetup, (void **)ppSetup);318 HRESULT hr = pNetCfg->QueryNetCfgClass(pguidClass, IID_INetCfgClassSetup, (void **)ppSetup); 307 319 if (FAILED(hr)) 308 NonStandardLogFlow(("QueryNetCfgClass failed , hr (0x%x)\n", hr));309 return hr; 310 } 311 312 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinInstallComponent(IN INetCfg *pNetCfg, IN LPCWSTR pszwComponentId, IN const GUID *pguidClass,313 OUT INetCfgComponent **ppComponent)320 NonStandardLogFlow(("QueryNetCfgClass failed: %Rhrc\n", hr)); 321 return hr; 322 } 323 324 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinInstallComponent(IN INetCfg *pNetCfg, IN LPCWSTR pszwComponentId, 325 IN const GUID *pguidClass, OUT INetCfgComponent **ppComponent) 314 326 { 315 327 INetCfgClassSetup *pSetup; … … 317 329 if (FAILED(hr)) 318 330 { 319 NonStandardLogFlow(("vboxNetCfgWinQueryInstaller failed , hr (0x%x)\n", hr));331 NonStandardLogFlow(("vboxNetCfgWinQueryInstaller failed: %Rhrc\n", hr)); 320 332 return hr; 321 333 } 322 334 323 335 OBO_TOKEN Token; 324 ZeroMemory(&Token, sizeof (Token));336 RT_ZERO(Token); 325 337 Token.Type = OBO_USER; 326 INetCfgComponent* pTempComponent = NULL; 327 338 339 INetCfgComponent *pTempComponent = NULL; 328 340 hr = pSetup->Install(pszwComponentId, &Token, 329 341 0, /* IN DWORD dwSetupFlags */ … … 336 348 if (pTempComponent != NULL) 337 349 { 338 HKEY hkey = (HKEY)INVALID_HANDLE_VALUE;339 HRESULT res;340 341 350 /* 342 * Set default metric value of interface to fix multicast issue 343 * See @bugref{6379} for details. 344 */ 345 res = pTempComponent->OpenParamKey(&hkey); 351 * Set default metric value of interface to fix multicast issue 352 * See @bugref{6379} for details. 353 */ 354 HKEY hKey = (HKEY)INVALID_HANDLE_VALUE; 355 HRESULT hrc2 = pTempComponent->OpenParamKey(&hKey); 346 356 347 357 /* Set default metric value for host-only interface only */ 348 if ( SUCCEEDED(res) 349 && hkey != INVALID_HANDLE_VALUE 350 && wcsnicmp(pszwComponentId, VBOXNETCFGWIN_NETADP_ID, 256) == 0) 358 if ( SUCCEEDED(hrc2) 359 && hKey != (HKEY)INVALID_HANDLE_VALUE 360 /* Original was weird: && wcsnicmp(pszwComponentId, VBOXNETCFGWIN_NETADP_ID_WSZ, 256) == 0) */ 361 && RTUtf16ICmpAscii(pszwComponentId, VBOXNETCFGWIN_NETADP_ID_SZ) == 0) 351 362 { 352 363 NET_LUID luid; 353 res = vboxNetCfgWinGetInterfaceLUID(hkey, &luid);364 hrc2 = vboxNetCfgWinGetInterfaceLUID(hKey, &luid); 354 365 355 366 /* Close the key as soon as possible. See @bugref{7973}. */ 356 RegCloseKey (hkey);357 h key = (HKEY)INVALID_HANDLE_VALUE;358 359 if (FAILED( res))367 RegCloseKey(hKey); 368 hKey = (HKEY)INVALID_HANDLE_VALUE; 369 370 if (FAILED(hrc2)) 360 371 { 361 372 /* … … 363 374 * So we will not break installation process due to this error. 364 375 */ 365 NonStandardLogFlow(("VBoxNetCfgWinInstallComponent Warning! " 366 "vboxNetCfgWinGetInterfaceLUID failed, default metric " 367 "for new interface will not be set, hr (0x%x)\n", res)); 376 NonStandardLogFlow(("VBoxNetCfgWinInstallComponent Warning! vboxNetCfgWinGetInterfaceLUID failed, default metric for new interface will not be set: %Rhrc\n", hrc2)); 368 377 } 369 378 else 370 379 { 371 res= vboxNetCfgWinSetupMetric(&luid);372 if (FAILED( res))380 hrc2 = vboxNetCfgWinSetupMetric(&luid); 381 if (FAILED(hrc2)) 373 382 { 374 383 /* … … 376 385 * So we will not break installation process due to this error. 377 386 */ 378 NonStandardLogFlow(("VBoxNetCfgWinInstallComponent Warning! " 379 "vboxNetCfgWinSetupMetric failed, default metric " 380 "for new interface will not be set, hr (0x%x)\n", res)); 387 NonStandardLogFlow(("VBoxNetCfgWinInstallComponent Warning! vboxNetCfgWinSetupMetric failed, default metric for new interface will not be set: %Rhrc\n", hrc2)); 381 388 } 382 389 } 383 390 } 384 if (hkey != INVALID_HANDLE_VALUE) 385 { 386 RegCloseKey (hkey); 387 hkey = (HKEY)INVALID_HANDLE_VALUE; 388 } 391 if (hKey != (HKEY)INVALID_HANDLE_VALUE) 392 RegCloseKey(hKey); 389 393 if (ppComponent != NULL) 390 394 *ppComponent = pTempComponent; … … 394 398 395 399 /* ignore the apply failure */ 396 HRESULT tmpHr= pNetCfg->Apply();397 Assert( tmpHr== S_OK);398 if ( tmpHr!= S_OK)399 NonStandardLogFlow(("Apply failed , hr (0x%x)\n", tmpHr));400 HRESULT hrc3 = pNetCfg->Apply(); 401 Assert(hrc3 == S_OK); 402 if (hrc3 != S_OK) 403 NonStandardLogFlow(("Apply failed: %Rhrc\n", hrc3)); 400 404 } 401 405 else 402 NonStandardLogFlow(("Install failed , hr (0x%x)\n", hr));406 NonStandardLogFlow(("Install failed: %Rhrc\n", hr)); 403 407 404 408 pSetup->Release(); … … 407 411 408 412 static HRESULT vboxNetCfgWinInstallInfAndComponent(IN INetCfg *pNetCfg, IN LPCWSTR pszwComponentId, IN const GUID *pguidClass, 409 IN LPCWSTR const *ap InfPaths, IN UINT cInfPaths,413 IN LPCWSTR const *apwszInfPaths, IN UINT cInfPaths, 410 414 OUT INetCfgComponent **ppComponent) 411 415 { 412 HRESULT hr = S_OK;413 UINT cFilesProcessed = 0;414 415 416 NonStandardLogFlow(("Installing %u INF files ...\n", cInfPaths)); 416 417 418 HRESULT hr = S_OK; 419 UINT cFilesProcessed = 0; 417 420 for (; cFilesProcessed < cInfPaths; cFilesProcessed++) 418 421 { 419 NonStandardLogFlow(("Installing INF file \"% ws\" ...\n", apInfPaths[cFilesProcessed]));420 hr = VBoxDrvCfgInfInstall(ap InfPaths[cFilesProcessed]);422 NonStandardLogFlow(("Installing INF file \"%ls\" ...\n", apwszInfPaths[cFilesProcessed])); 423 hr = VBoxDrvCfgInfInstall(apwszInfPaths[cFilesProcessed]); 421 424 if (FAILED(hr)) 422 425 { 423 NonStandardLogFlow(("VBoxNetCfgWinInfInstall failed , hr (0x%x)\n", hr));426 NonStandardLogFlow(("VBoxNetCfgWinInfInstall failed: %Rhrc\n", hr)); 424 427 break; 425 428 } … … 430 433 hr = VBoxNetCfgWinInstallComponent(pNetCfg, pszwComponentId, pguidClass, ppComponent); 431 434 if (FAILED(hr)) 432 NonStandardLogFlow(("VBoxNetCfgWinInstallComponent failed , hr (0x%x)\n", hr));435 NonStandardLogFlow(("VBoxNetCfgWinInstallComponent failed: %Rhrc\n", hr)); 433 436 } 434 437 … … 439 442 do 440 443 { 441 HRESULT hr2 = VBoxDrvCfgInfUninstall(ap InfPaths[cFilesProcessed], 0);444 HRESULT hr2 = VBoxDrvCfgInfUninstall(apwszInfPaths[cFilesProcessed], 0); 442 445 if (FAILED(hr2)) 443 NonStandardLogFlow(("VBoxDrvCfgInfUninstall failed , hr (0x%x)\n", hr2));446 NonStandardLogFlow(("VBoxDrvCfgInfUninstall failed: %Rhrc\n", hr2)); 444 447 /* Keep going. */ 445 448 if (!cFilesProcessed) … … 459 462 if (FAILED(hr)) 460 463 { 461 NonStandardLogFlow(("GetClassGuid failed , hr (0x%x)\n", hr));464 NonStandardLogFlow(("GetClassGuid failed: %Rhrc\n", hr)); 462 465 return hr; 463 466 } … … 467 470 if (FAILED(hr)) 468 471 { 469 NonStandardLogFlow(("vboxNetCfgWinQueryInstaller failed , hr (0x%x)\n", hr));472 NonStandardLogFlow(("vboxNetCfgWinQueryInstaller failed: %Rhrc\n", hr)); 470 473 return hr; 471 474 } 472 475 473 476 OBO_TOKEN Token; 474 ZeroMemory(&Token, sizeof(Token));477 RT_ZERO(Token); 475 478 Token.Type = OBO_USER; 476 479 … … 480 483 hr = pNetCfg->Apply(); 481 484 if (FAILED(hr)) 482 NonStandardLogFlow(("Apply failed , hr (0x%x)\n", hr));485 NonStandardLogFlow(("Apply failed: %Rhrc\n", hr)); 483 486 } 484 487 else 485 NonStandardLogFlow(("DeInstall failed , hr (0x%x)\n", hr));488 NonStandardLogFlow(("DeInstall failed: %Rhrc\n", hr)); 486 489 487 490 if (pSetup) … … 490 493 } 491 494 492 typedef BOOL (*VBOXNETCFGWIN_NETCFGENUM_CALLBACK) (IN INetCfg *pNetCfg, IN INetCfgComponent *pNetCfgComponent, PVOID pContext); 495 typedef BOOL (*PFN_VBOXNETCFGWIN_NETCFGENUM_CALLBACK_T)(IN INetCfg *pNetCfg, IN INetCfgComponent *pNetCfgComponent, 496 PVOID pvContext); 493 497 494 498 static HRESULT vboxNetCfgWinEnumNetCfgComponents(IN INetCfg *pNetCfg, 495 499 IN const GUID *pguidClass, 496 VBOXNETCFGWIN_NETCFGENUM_CALLBACK callback,500 PFN_VBOXNETCFGWIN_NETCFGENUM_CALLBACK_T pfnCallback, 497 501 PVOID pContext) 498 502 { 499 IEnumNetCfgComponent *pEnumComponent ;503 IEnumNetCfgComponent *pEnumComponent = NULL; 500 504 HRESULT hr = pNetCfg->EnumComponents(pguidClass, &pEnumComponent); 501 505 if (SUCCEEDED(hr)) … … 503 507 INetCfgComponent *pNetCfgComponent; 504 508 hr = pEnumComponent->Reset(); 505 do509 for (;;) 506 510 { 507 511 hr = pEnumComponent->Next(1, &pNetCfgComponent, NULL); … … 514 518 if (pNetCfgComponent) 515 519 { 516 if (pContext) 517 fResult = callback(pNetCfg, pNetCfgComponent, pContext); 520 fResult = pfnCallback(pNetCfg, pNetCfgComponent, pContext); 518 521 pNetCfgComponent->Release(); 519 522 } … … 525 528 { 526 529 if (hr == S_FALSE) 527 { 528 hr = S_OK; 529 } 530 hr = S_OK; /* no more components */ 530 531 else 531 NonStandardLogFlow(("Next failed , hr (0x%x)\n", hr));532 NonStandardLogFlow(("Next failed: %Rhrc\n", hr)); 532 533 break; 533 534 } 534 } while (true);535 } 535 536 pEnumComponent->Release(); 536 537 } … … 538 539 } 539 540 540 /* 541 * Forward declarations of functions used in vboxNetCfgWinRemoveAllNetDevicesOfIdCallback. 542 */ 543 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinGenHostonlyConnectionName(PCWSTR DevName, WCHAR *pBuf, PULONG pcbBuf); 544 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinRenameConnection(LPWSTR pGuid, PCWSTR NewName); 545 541 /** PFNVBOXNETCFGWINNETENUMCALLBACK */ 546 542 static BOOL vboxNetCfgWinRemoveAllNetDevicesOfIdCallback(HDEVINFO hDevInfo, PSP_DEVINFO_DATA pDev, PVOID pvContext) 547 543 { 548 544 RT_NOREF1(pvContext); 545 549 546 SP_REMOVEDEVICE_PARAMS rmdParams; 550 memset(&rmdParams, 0, sizeof(SP_REMOVEDEVICE_PARAMS));547 RT_ZERO(rmdParams); 551 548 rmdParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER); 552 549 rmdParams.ClassInstallHeader.InstallFunction = DIF_REMOVE; … … 567 564 KEY_READ); 568 565 if (hKey == INVALID_HANDLE_VALUE) 569 { 570 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: SetupDiOpenDevRegKey failed with error %ld\n", 566 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: SetupDiOpenDevRegKey failed with error %u\n", 571 567 GetLastError())); 572 }573 568 else 574 569 { 575 570 WCHAR wszCfgGuidString[50] = { L'' }; 576 DWORD cbSize = sizeof(wszCfgGuidString); 577 DWORD dwValueType; 578 DWORD ret = RegQueryValueExW(hKey, L"NetCfgInstanceId", NULL, 579 &dwValueType, (LPBYTE)wszCfgGuidString, &cbSize); 580 if (ret == ERROR_SUCCESS) 581 { 582 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: Processing device ID \"%S\"\n", 583 wszCfgGuidString)); 584 585 /* Figure out device name. */ 586 WCHAR wszDevName[256], wszTempName[256]; 587 ULONG cbName = sizeof(wszTempName); 588 589 if (SetupDiGetDeviceRegistryPropertyW(hDevInfo, pDev, 590 SPDRP_FRIENDLYNAME, /* IN DWORD Property,*/ 591 NULL, /* OUT PDWORD PropertyRegDataType, OPTIONAL*/ 592 (PBYTE)wszDevName, /* OUT PBYTE PropertyBuffer,*/ 593 sizeof(wszDevName), /* IN DWORD PropertyBufferSize,*/ 594 NULL /* OUT PDWORD RequiredSize OPTIONAL*/)) 571 DWORD cbSize = sizeof(wszCfgGuidString) - sizeof(WCHAR); /* make sure we get a terminated string back */ 572 DWORD dwValueType = 0; 573 LSTATUS lrc = RegQueryValueExW(hKey, L"NetCfgInstanceId", NULL, &dwValueType, (LPBYTE)wszCfgGuidString, &cbSize); 574 if (lrc == ERROR_SUCCESS) 575 { 576 /** @todo r=bird: original didn't check the type here, just assumed it was a 577 * valid zero terminated string. (zero term handled by -sizeof(WHCAR) above now). */ 578 if (dwValueType == REG_SZ || dwValueType == REG_EXPAND_SZ || dwValueType == REG_EXPAND_SZ) 595 579 { 596 /* 597 * Rename the connection before removing the device. This will 598 * hopefully prevent an error when we will be attempting 599 * to rename a newly created connection (see @bugref{6740}). 600 */ 601 HRESULT hr = VBoxNetCfgWinGenHostonlyConnectionName(wszDevName, wszTempName, &cbName); 602 wcscat_s(wszTempName, sizeof(wszTempName), L" removed"); 603 if (SUCCEEDED(hr)) 604 hr = VBoxNetCfgWinRenameConnection(wszCfgGuidString, wszTempName); 605 //NonStandardLogFlow(("VBoxNetCfgWinRenameConnection(%S,%S) => 0x%x\n", pWCfgGuidString, TempName, hr_tmp)); 580 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: Processing device ID \"%ls\"\n", 581 wszCfgGuidString)); 582 583 /* Figure out device name. */ 584 WCHAR wszDevName[256 + 1] = {0}; 585 if (SetupDiGetDeviceRegistryPropertyW(hDevInfo, pDev, SPDRP_FRIENDLYNAME, NULL, (PBYTE)wszDevName, 586 sizeof(wszDevName) - sizeof(WCHAR) /* yes, in bytes */, NULL)) 587 { 588 /* 589 * Rename the connection before removing the device. This will 590 * hopefully prevent an error when we will be attempting 591 * to rename a newly created connection (see @bugref{6740}). 592 */ 593 WCHAR wszNewName[RT_ELEMENTS(wszDevName) + 128 /* ensure sufficient buffer */]; 594 HRESULT hr = VBoxNetCfgWinGenHostonlyConnectionName(wszDevName, wszNewName, 595 RT_ELEMENTS(wszNewName) - 10 /*removed++*/, 596 NULL); 597 RTUtf16CatAscii(wszNewName, sizeof(wszNewName), " removed"); 598 if (SUCCEEDED(hr)) 599 hr = VBoxNetCfgWinRenameConnection(wszCfgGuidString, wszNewName); 600 //NonStandardLogFlow(("VBoxNetCfgWinRenameConnection(%S,%S) => 0x%x\n", wszCfgGuidString, TempName, hr_tmp)); 601 } 602 else 603 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: Failed to get friendly name for device \"%ls\"\n", 604 wszCfgGuidString)); 606 605 } 607 606 else 608 { 609 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: Failed to get friendly name for device \"%S\"\n", 610 wszCfgGuidString)); 611 } 607 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: Friendly name for \"%S\" isn't a string: %d\n", 608 wszCfgGuidString, dwValueType 612 609 } 613 610 else 614 { 615 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: Querying instance ID failed with %d\n", 616 ret)); 617 } 611 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: Querying instance ID failed with %u (%#x)\n", 612 lrc, lrc)); 618 613 619 614 RegCloseKey(hKey); … … 623 618 if (SetupDiCallClassInstaller(DIF_REMOVE, hDevInfo, pDev)) 624 619 { 625 SP_DEVINSTALL_PARAMS devParams; 626 memset(&devParams, 0, sizeof(SP_DEVINSTALL_PARAMS)); 627 devParams.cbSize = sizeof(devParams); 628 629 if (SetupDiGetDeviceInstallParams(hDevInfo, pDev, &devParams)) 630 { 631 if ( (devParams.Flags & DI_NEEDRESTART) 632 || (devParams.Flags & DI_NEEDREBOOT)) 633 { 620 SP_DEVINSTALL_PARAMS_W DevParams = { sizeof(DevParams) }; 621 if (SetupDiGetDeviceInstallParams(hDevInfo, pDev, &DevParams)) 622 { 623 if ( (DevParams.Flags & DI_NEEDRESTART) 624 || (DevParams.Flags & DI_NEEDREBOOT)) 634 625 NonStandardLog(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: A reboot is required\n")); 635 }636 626 } 637 627 else 638 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: SetupDiGetDeviceInstallParams failed with % ld\n",628 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: SetupDiGetDeviceInstallParams failed with %u\n", 639 629 GetLastError())); 640 630 } 641 631 else 642 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: SetupDiCallClassInstaller failed with % ld\n",632 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: SetupDiCallClassInstaller failed with %u\n", 643 633 GetLastError())); 644 634 } 645 635 else 646 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: SetupDiSetSelectedDevice failed with % ld\n",636 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: SetupDiSetSelectedDevice failed with %u\n", 647 637 GetLastError())); 648 638 } 649 639 else 650 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: SetupDiSetClassInstallParams failed with % ld\n",640 NonStandardLogFlow(("vboxNetCfgWinRemoveAllNetDevicesOfIdCallback: SetupDiSetClassInstallParams failed with %u\n", 651 641 GetLastError())); 652 642 … … 657 647 typedef struct VBOXNECTFGWINPROPCHANGE 658 648 { 659 VBOXNECTFGWINPROPCHANGE_TYPE enmPcType;649 VBOXNECTFGWINPROPCHANGE_TYPE_T enmPcType; 660 650 HRESULT hr; 661 } VBOXNECTFGWINPROPCHANGE ,*PVBOXNECTFGWINPROPCHANGE;651 } VBOXNECTFGWINPROPCHANGE, *PVBOXNECTFGWINPROPCHANGE; 662 652 663 653 static BOOL vboxNetCfgWinPropChangeAllNetDevicesOfIdCallback(HDEVINFO hDevInfo, PSP_DEVINFO_DATA pDev, PVOID pContext) … … 666 656 667 657 SP_PROPCHANGE_PARAMS PcParams; 668 memset (&PcParams, 0, sizeof (PcParams));658 RT_ZERO(PcParams); 669 659 PcParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER); 670 660 PcParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; 671 661 PcParams.Scope = DICS_FLAG_GLOBAL; 672 662 673 switch (pPc->enmPcType)663 switch (pPc->enmPcType) 674 664 { 675 665 case VBOXNECTFGWINPROPCHANGE_TYPE_DISABLE: … … 687 677 } 688 678 689 if (SetupDiSetClassInstallParams (hDevInfo, pDev, &PcParams.ClassInstallHeader, sizeof(PcParams)))679 if (SetupDiSetClassInstallParamsW(hDevInfo, pDev, &PcParams.ClassInstallHeader, sizeof(PcParams))) 690 680 { 691 681 if (SetupDiSetSelectedDevice(hDevInfo, pDev)) … … 693 683 if (SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, pDev)) 694 684 { 695 SP_DEVINSTALL_PARAMS devParams; 696 devParams.cbSize = sizeof(devParams); 697 if (SetupDiGetDeviceInstallParams(hDevInfo,pDev,&devParams)) 698 { 699 if ( (devParams.Flags & DI_NEEDRESTART) 700 || (devParams.Flags & DI_NEEDREBOOT)) 701 { 685 SP_DEVINSTALL_PARAMS_W DevParams = { sizeof(DevParams) }; 686 if (SetupDiGetDeviceInstallParamsW(hDevInfo, pDev, &DevParams)) 687 { 688 if ( (DevParams.Flags & DI_NEEDRESTART) 689 || (DevParams.Flags & DI_NEEDREBOOT)) 702 690 NonStandardLog(("vboxNetCfgWinPropChangeAllNetDevicesOfIdCallback: A reboot is required\n")); 703 }704 691 } 705 692 else 706 NonStandardLogFlow(("vboxNetCfgWinPropChangeAllNetDevicesOfIdCallback: SetupDiGetDeviceInstallParams failed with % ld\n",693 NonStandardLogFlow(("vboxNetCfgWinPropChangeAllNetDevicesOfIdCallback: SetupDiGetDeviceInstallParams failed with %u\n", 707 694 GetLastError())); 708 695 } 709 696 else 710 NonStandardLogFlow(("vboxNetCfgWinPropChangeAllNetDevicesOfIdCallback: SetupDiCallClassInstaller failed with % ld\n",697 NonStandardLogFlow(("vboxNetCfgWinPropChangeAllNetDevicesOfIdCallback: SetupDiCallClassInstaller failed with %u\n", 711 698 GetLastError())); 712 699 } 713 700 else 714 NonStandardLogFlow(("SetupDiSetSelectedDevice failed with % ld\n", GetLastError()));701 NonStandardLogFlow(("SetupDiSetSelectedDevice failed with %u\n", GetLastError())); 715 702 } 716 703 else 717 NonStandardLogFlow(("SetupDiSetClassInstallParams failed with % ld\n", GetLastError()));704 NonStandardLogFlow(("SetupDiSetClassInstallParams failed with %u\n", GetLastError())); 718 705 719 706 /* Continue enumeration. */ … … 722 709 723 710 typedef BOOL (*PFNVBOXNETCFGWINNETENUMCALLBACK)(HDEVINFO hDevInfo, PSP_DEVINFO_DATA pDev, PVOID pContext); 724 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinEnumNetDevices(LPCWSTR pwszPnPId, 725 726 { 727 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: Searching for: % S\n", pwszPnPId));711 712 static HRESULT vboxNetCfgWinEnumNetDevices(LPCWSTR pwszPnPId, PFNVBOXNETCFGWINNETENUMCALLBACK pfnCallback, PVOID pvContext) 713 { 714 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: Searching for: %ls\n", pwszPnPId)); 728 715 729 716 HRESULT hr; … … 737 724 if (hDevInfo != INVALID_HANDLE_VALUE) 738 725 { 739 DWORD winEr = NO_ERROR; 740 741 DWORD dwDevId = 0; 742 size_t cPnPId = wcslen(pwszPnPId); 743 744 PBYTE pBuffer = NULL; 745 726 size_t const cwcPnPId = RTUtf16Len(pwszPnPId); 727 DWORD winEr = NO_ERROR; 728 DWORD dwDevId = 0; 729 DWORD cbBuffer = 0; 730 PBYTE pbBuffer = NULL; 746 731 for (;;) 747 732 { … … 754 739 winEr = GetLastError(); 755 740 if (winEr == ERROR_NO_MORE_ITEMS) 756 winEr = ERROR_SUCCESS;741 winEr = NO_ERROR; 757 742 break; 758 743 } 759 744 760 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: Enumerating device % ld... \n", dwDevId));745 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: Enumerating device %u ... \n", dwDevId)); 761 746 dwDevId++; 762 747 763 if (pBuffer)764 free(pBuffer);765 pBuffer = NULL;766 DWORD cbBuffer = 0;767 748 DWORD cbRequired = 0; 768 749 SetLastError(0); 769 750 if (!SetupDiGetDeviceRegistryPropertyW(hDevInfo, &Dev, 770 751 SPDRP_HARDWAREID, /* IN DWORD Property */ 771 752 NULL, /* OUT PDWORD PropertyRegDataType OPTIONAL */ 772 p Buffer,/* OUT PBYTE PropertyBuffer */753 pbBuffer, /* OUT PBYTE PropertyBuffer */ 773 754 cbBuffer, /* IN DWORD PropertyBufferSize */ 774 755 &cbRequired /* OUT PDWORD RequiredSize OPTIONAL */)) … … 777 758 if (winEr != ERROR_INSUFFICIENT_BUFFER) 778 759 { 779 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: SetupDiGetDeviceRegistryPropertyW (1) failed with %ld\n", winEr)); 760 if (winEr == ERROR_INVALID_DATA) 761 { 762 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: SetupDiGetDeviceRegistryPropertyW (1) failed with ERROR_INVALID_DATA - ignoring, skipping to next device\n")); 763 continue; 764 } 765 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: SetupDiGetDeviceRegistryPropertyW (1) failed with %u\n", winEr)); 780 766 break; 781 767 } 782 783 pBuffer = (PBYTE)malloc(cbRequired); 784 if (!pBuffer) 785 { 786 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: Out of memory allocating %ld bytes\n", 787 cbRequired)); 768 winEr = NO_ERROR; 769 770 cbBuffer = RT_ALIGN_32(cbRequired, 64); 771 void *pvNew = RTMemRealloc(pbBuffer, cbBuffer); 772 if (pvNew) 773 pbBuffer = (PBYTE)pvNew; 774 else 775 { 776 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: Out of memory allocating %u bytes\n", cbBuffer)); 788 777 winEr = ERROR_OUTOFMEMORY; 789 778 break; 790 779 } 791 780 792 cbBuffer = cbRequired; 793 794 if (!SetupDiGetDeviceRegistryPropertyW(hDevInfo,&Dev, 781 if (!SetupDiGetDeviceRegistryPropertyW(hDevInfo, &Dev, 795 782 SPDRP_HARDWAREID, /* IN DWORD Property */ 796 783 NULL, /* OUT PDWORD PropertyRegDataType, OPTIONAL */ 797 p Buffer,/* OUT PBYTE PropertyBuffer */784 pbBuffer, /* OUT PBYTE PropertyBuffer */ 798 785 cbBuffer, /* IN DWORD PropertyBufferSize */ 799 786 &cbRequired /* OUT PDWORD RequiredSize OPTIONAL */)) 800 787 { 801 788 winEr = GetLastError(); 802 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: SetupDiGetDeviceRegistryPropertyW (2) failed with % ld\n",789 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: SetupDiGetDeviceRegistryPropertyW (2) failed with %u\n", 803 790 winEr)); 804 791 break; … … 806 793 } 807 794 808 PWSTR pCurId = (PWSTR)pBuffer;809 size_t c CurId = wcslen(pCurId);810 811 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: Device % ld: %S\n", dwDevId, pCurId));812 813 if (c CurId >=cPnPId)814 { 815 NonStandardLogFlow(("! wcsnicmp(pCurId = (%S), pwszPnPId = (%S), cPnPId = (%d))\n", pCurId, pwszPnPId,cPnPId));816 817 p CurId += cCurId -cPnPId;818 if (! wcsnicmp(pCurId, pwszPnPId,cPnPId))795 PWSTR pwszCurId = (PWSTR)pbBuffer; 796 size_t cwcCurId = RTUtf16Len(pwszCurId); 797 798 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: Device %u: %ls\n", dwDevId, pwszCurId)); 799 800 if (cwcCurId >= cwcPnPId) 801 { 802 NonStandardLogFlow(("!RTUtf16NICmp(pwszCurId = (%ls), pwszPnPId = (%ls), cwcPnPId = (%d))\n", pwszCurId, pwszPnPId, cwcPnPId)); 803 804 pwszCurId += cwcCurId - cwcPnPId; 805 if (!RTUtf16NICmp(pwszCurId, pwszPnPId, cwcPnPId)) 819 806 { 820 807 if (!pfnCallback(hDevInfo, &Dev, pvContext)) … … 824 811 } 825 812 826 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: Found % lddevices total\n", dwDevId));827 828 if (p Buffer)829 free(pBuffer);813 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: Found %u devices total\n", dwDevId)); 814 815 if (pbBuffer) 816 RTMemFree(pbBuffer); 830 817 831 818 hr = HRESULT_FROM_WIN32(winEr); … … 836 823 { 837 824 DWORD winEr = GetLastError(); 838 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: SetupDiGetClassDevsExW failed with % ld\n", winEr));825 NonStandardLogFlow(("VBoxNetCfgWinEnumNetDevices: SetupDiGetClassDevsExW failed with %u\n", winEr)); 839 826 hr = HRESULT_FROM_WIN32(winEr); 840 827 } … … 844 831 } 845 832 846 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinRemoveAllNetDevicesOfId(IN LPCWSTR lpszPnPId)847 { 848 return VBoxNetCfgWinEnumNetDevices(lpszPnPId, vboxNetCfgWinRemoveAllNetDevicesOfIdCallback, NULL);849 } 850 851 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinPropChangeAllNetDevicesOfId(IN LPCWSTR lpszPnPId, VBOXNECTFGWINPROPCHANGE_TYPEenmPcType)833 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinRemoveAllNetDevicesOfId(IN LPCWSTR pwszPnPId) 834 { 835 return vboxNetCfgWinEnumNetDevices(pwszPnPId, vboxNetCfgWinRemoveAllNetDevicesOfIdCallback, NULL); 836 } 837 838 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinPropChangeAllNetDevicesOfId(IN LPCWSTR pwszPnPId, VBOXNECTFGWINPROPCHANGE_TYPE_T enmPcType) 852 839 { 853 840 VBOXNECTFGWINPROPCHANGE Pc; 854 841 Pc.enmPcType = enmPcType; 855 842 Pc.hr = S_OK; 856 NonStandardLogFlow(("Calling VBoxNetCfgWinEnumNetDevices with lpszPnPId =(%S) and vboxNetCfgWinPropChangeAllNetDevicesOfIdCallback\n", lpszPnPId));857 858 HRESULT hr = VBoxNetCfgWinEnumNetDevices(lpszPnPId, vboxNetCfgWinPropChangeAllNetDevicesOfIdCallback, &Pc);843 NonStandardLogFlow(("Calling VBoxNetCfgWinEnumNetDevices with pwszPnPId (= %ls) and vboxNetCfgWinPropChangeAllNetDevicesOfIdCallback\n", pwszPnPId)); 844 845 HRESULT hr = vboxNetCfgWinEnumNetDevices(pwszPnPId, vboxNetCfgWinPropChangeAllNetDevicesOfIdCallback, &Pc); 859 846 if (!SUCCEEDED(hr)) 860 847 { … … 872 859 } 873 860 874 /* 875 * logging 876 */ 861 862 863 /********************************************************************************************************************************* 864 * Logging * 865 *********************************************************************************************************************************/ 866 877 867 static void DoLogging(const char *pszString, ...) 878 868 { … … 880 870 if (pfnLogger) 881 871 { 882 char szBuffer[4096] = {0};872 char szBuffer[4096]; 883 873 va_list va; 884 874 va_start(va, pszString); 885 _vsnprintf(szBuffer, RT_ELEMENTS(szBuffer), pszString, va);875 RTStrPrintfV(szBuffer, RT_ELEMENTS(szBuffer), pszString, va); 886 876 va_end(va); 887 877 … … 895 885 } 896 886 897 /* 898 * IP configuration API 899 */ 887 888 889 /********************************************************************************************************************************* 890 * IP configuration API * 891 *********************************************************************************************************************************/ 900 892 /* network settings config */ 893 #if 1 /** @todo r=bird: Can't we replace this with VBox/com/ptr.h? */ 901 894 /** 902 895 * Strong referencing operators. Used as a second argument to ComPtr<>/ComObjPtr<>. 903 896 */ 904 template 897 template<class C> 905 898 class ComStrongRef 906 899 { 907 900 protected: 908 901 909 static void addref (C *p){ p->AddRef(); }910 static void release 902 static void addref(C *p) { p->AddRef(); } 903 static void release(C *p) { p->Release(); } 911 904 }; 912 905 … … 915 908 * Base template for smart COM pointers. Not intended to be used directly. 916 909 */ 917 template <class C, template<class> class RefOps = ComStrongRef>918 class ComPtrBase : protected RefOps 910 template<class C, template<class> class RefOps = ComStrongRef> 911 class ComPtrBase : protected RefOps<C> 919 912 { 920 913 public: 921 914 922 915 /* special template to disable AddRef()/Release() */ 923 template 916 template<class I> 924 917 class NoAddRefRelease : public I 925 918 { … … 938 931 protected: 939 932 940 ComPtrBase () : p(NULL) {}941 ComPtrBase (const ComPtrBase &that) : p(that.p) { addref(); }942 ComPtrBase (C *that_p) : p (that_p){ addref(); }933 ComPtrBase() : p(NULL) {} 934 ComPtrBase(const ComPtrBase &that) : p(that.p) { addref(); } 935 ComPtrBase(C *that_p) : p(that_p) { addref(); } 943 936 944 937 ~ComPtrBase() { release(); } 945 938 946 ComPtrBase &operator= 947 { 948 safe_assign 939 ComPtrBase &operator=(const ComPtrBase &that) 940 { 941 safe_assign(that.p); 949 942 return *this; 950 943 } 951 944 952 ComPtrBase &operator= 953 { 954 safe_assign 945 ComPtrBase &operator=(C *that_p) 946 { 947 safe_assign(that_p); 955 948 return *this; 956 949 } … … 969 962 } 970 963 971 bool operator! () const{ return isNull(); }972 973 bool operator< (C* that_p) const{ return p < that_p; }974 bool operator== (C* that_p) const{ return p == that_p; }975 976 template 977 bool equalsTo 978 { 979 return ComPtrEquals 980 } 981 982 template 983 bool equalsTo (const ComPtrBase<OC> &oc) const984 { 985 return equalsTo 964 bool operator!() const { return isNull(); } 965 966 bool operator<(C* that_p) const { return p < that_p; } 967 bool operator==(C* that_p) const { return p == that_p; } 968 969 template<class I> 970 bool equalsTo(I *aThat) const 971 { 972 return ComPtrEquals(p, aThat); 973 } 974 975 template<class OC> 976 bool equalsTo(const ComPtrBase<OC> &oc) const 977 { 978 return equalsTo((OC *) oc); 986 979 } 987 980 988 981 /** Intended to pass instances as in parameters to interface methods */ 989 operator C *() const { return p; }982 operator C *() const { return p; } 990 983 991 984 /** … … 993 986 * pointer). 994 987 */ 995 NoAddRefRelease <C> *operator->() const988 NoAddRefRelease<C> *operator->() const 996 989 { 997 990 AssertMsg (p, ("Managed pointer must not be null\n")); … … 999 992 } 1000 993 1001 template 1002 HRESULT queryInterfaceTo 994 template<class I> 995 HRESULT queryInterfaceTo(I **pp) const 1003 996 { 1004 997 if (pp) 1005 998 { 1006 999 if (p) 1007 { 1008 return p->QueryInterface (COM_IIDOF (I), (void **) pp); 1009 } 1010 else 1011 { 1012 *pp = NULL; 1013 return S_OK; 1014 } 1015 } 1016 1000 return p->QueryInterface(COM_IIDOF(I), (void **)pp); 1001 *pp = NULL; 1002 return S_OK; 1003 } 1017 1004 return E_INVALIDARG; 1018 1005 } … … 1030 1017 { 1031 1018 if (p) 1032 RefOps <C>::addref(p);1019 RefOps<C>::addref(p); 1033 1020 } 1034 1021 … … 1036 1023 { 1037 1024 if (p) 1038 RefOps <C>::release(p);1039 } 1040 1041 void safe_assign 1025 RefOps<C>::release(p); 1026 } 1027 1028 void safe_assign(C *that_p) 1042 1029 { 1043 1030 /* be aware of self-assignment */ 1044 1031 if (that_p) 1045 RefOps <C>::addref(that_p);1032 RefOps<C>::addref(that_p); 1046 1033 release(); 1047 1034 p = that_p; … … 1057 1044 * @param I COM interface class 1058 1045 */ 1059 template <class I, template<class> class RefOps = ComStrongRef>1060 class ComPtr : public ComPtrBase 1061 { 1062 typedef ComPtrBase 1046 template<class I, template<class> class RefOps = ComStrongRef> 1047 class ComPtr : public ComPtrBase<I, RefOps> 1048 { 1049 typedef ComPtrBase<I, RefOps> Base; 1063 1050 1064 1051 public: 1065 1052 1066 ComPtr 1067 ComPtr 1068 ComPtr 1069 { 1070 Base::operator= 1053 ComPtr() : Base() {} 1054 ComPtr(const ComPtr &that) : Base(that) {} 1055 ComPtr&operator= (const ComPtr &that) 1056 { 1057 Base::operator=(that); 1071 1058 return *this; 1072 1059 } 1073 1060 1074 template 1075 ComPtr (OI *that_p) : Base () { operator=(that_p); }1061 template<class OI> 1062 ComPtr(OI *that_p) : Base () { operator=(that_p); } 1076 1063 1077 1064 /* specialization for I */ 1078 ComPtr 1065 ComPtr(I *that_p) : Base (that_p) {} 1079 1066 1080 1067 template <class OC> 1081 ComPtr (const ComPtr <OC, RefOps> &oc) : Base () { operator=((OC *) oc); }1082 1083 template 1084 ComPtr &operator= 1068 ComPtr(const ComPtr<OC, RefOps> &oc) : Base() { operator=((OC *) oc); } 1069 1070 template<class OI> 1071 ComPtr &operator=(OI *that_p) 1085 1072 { 1086 1073 if (that_p) 1087 that_p->QueryInterface (COM_IIDOF (I), (void **)Base::asOutParam());1074 that_p->QueryInterface(COM_IIDOF(I), (void **)Base::asOutParam()); 1088 1075 else 1089 1076 Base::setNull(); … … 1094 1081 ComPtr &operator=(I *that_p) 1095 1082 { 1096 Base::operator= 1083 Base::operator=(that_p); 1097 1084 return *this; 1098 1085 } 1099 1086 1100 template 1101 ComPtr &operator= (const ComPtr<OC, RefOps> &oc)1102 { 1103 return operator= 1087 template<class OC> 1088 ComPtr &operator=(const ComPtr<OC, RefOps> &oc) 1089 { 1090 return operator=((OC *) oc); 1104 1091 } 1105 1092 }; 1106 1107 static HRESULT netIfWinFindAdapterClassById(IWbemServices * pSvc, const GUID * pGuid, IWbemClassObject **pAdapterConfig) 1093 #endif 1094 1095 static HRESULT netIfWinFindAdapterClassById(IWbemServices *pSvc, const GUID *pGuid, IWbemClassObject **pAdapterConfig) 1108 1096 { 1109 1097 HRESULT hr; 1110 WCHAR wszQuery[256]; 1098 1111 1099 WCHAR wszGuid[50]; 1112 1113 int length = StringFromGUID2(*pGuid, wszGuid, RT_ELEMENTS(wszGuid)); 1114 if (length) 1115 { 1116 swprintf(wszQuery, L"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE SettingID = \"%s\"", wszGuid); 1100 int cwcGuid = StringFromGUID2(*pGuid, wszGuid, RT_ELEMENTS(wszGuid)); 1101 if (cwcGuid) 1102 { 1103 com::BstrFmt bstrQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE SettingID = \"%ls\"", wszGuid); 1117 1104 IEnumWbemClassObject* pEnumerator = NULL; 1118 hr = pSvc->ExecQuery( bstr_t("WQL"), bstr_t(wszQuery), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,1105 hr = pSvc->ExecQuery(com::Bstr("WQL").raw(), bstrQuery.raw(), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 1119 1106 NULL, &pEnumerator); 1120 1107 if (SUCCEEDED(hr)) … … 1122 1109 if (pEnumerator) 1123 1110 { 1124 IWbemClassObject *pclsObj ;1111 IWbemClassObject *pclsObj = NULL; 1125 1112 ULONG uReturn = 0; 1126 1113 hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); … … 1136 1123 return S_OK; 1137 1124 } 1138 1139 1125 hr = E_FAIL; 1140 1126 } 1141 1142 1127 pEnumerator->Release(); 1143 1128 } … … 1154 1139 { 1155 1140 DWORD winEr = GetLastError(); 1156 hr = HRESULT_FROM_WIN32( winEr);1141 hr = HRESULT_FROM_WIN32(winEr); 1157 1142 if (SUCCEEDED(hr)) 1158 1143 hr = E_FAIL; … … 1164 1149 } 1165 1150 1166 static HRESULT netIfWinIsHostOnly(IWbemClassObject * pAdapterConfig, BOOL * pbIsHostOnly)1151 static HRESULT netIfWinIsHostOnly(IWbemClassObject *pAdapterConfig, BOOL *pfIsHostOnly) 1167 1152 { 1168 1153 VARIANT vtServiceName; … … 1172 1157 if (SUCCEEDED(hr)) 1173 1158 { 1174 *p bIsHostOnly = bstr_t(vtServiceName.bstrVal) == bstr_t("VBoxNetAdp");1159 *pfIsHostOnly = RTUtf16CmpAscii(vtServiceName.bstrVal, "VBoxNetAdp") == 0; 1175 1160 1176 1161 VariantClear(&vtServiceName); … … 1182 1167 static HRESULT netIfWinGetIpSettings(IWbemClassObject * pAdapterConfig, ULONG *pIpv4, ULONG *pMaskv4) 1183 1168 { 1169 *pIpv4 = 0; 1170 *pMaskv4 = 0; 1171 1184 1172 VARIANT vtIp; 1185 HRESULT hr;1186 1173 VariantInit(&vtIp); 1187 1188 *pIpv4 = 0; 1189 *pMaskv4 = 0; 1190 1191 hr = pAdapterConfig->Get(L"IPAddress", 0, &vtIp, 0, 0); 1174 HRESULT hr = pAdapterConfig->Get(L"IPAddress", 0, &vtIp, 0, 0); 1192 1175 if (SUCCEEDED(hr)) 1193 1176 { … … 1201 1184 if (vtMask.vt == (VT_ARRAY | VT_BSTR)) 1202 1185 { 1203 SAFEARRAY * pIpArray= vtIp.parray;1204 SAFEARRAY * 1186 SAFEARRAY *pIpArray = vtIp.parray; 1187 SAFEARRAY *pMaskArray = vtMask.parray; 1205 1188 if (pIpArray && pMaskArray) 1206 1189 { 1207 BSTR p CurIp;1208 BSTR p CurMask;1190 BSTR pBstrCurIp; 1191 BSTR pBstrCurMask; 1209 1192 for (LONG i = 0; 1210 SafeArrayGetElement(pIpArray, &i, (PVOID)&pCurIp)== S_OK1211 && SafeArrayGetElement(pMaskArray, &i, (PVOID)&pCurMask) == S_OK;1212 i++)1193 SafeArrayGetElement(pIpArray, &i, (PVOID)&pBstrCurIp) == S_OK 1194 && SafeArrayGetElement(pMaskArray, &i, (PVOID)&pBstrCurMask) == S_OK; 1195 i++) 1213 1196 { 1214 bstr_t ip(pCurIp); 1215 1216 ULONG Ipv4 = inet_addr((char*)(ip)); 1197 com::Utf8Str strIp(pBstrCurIp); 1198 ULONG Ipv4 = inet_addr(strIp.c_str()); 1217 1199 if (Ipv4 != INADDR_NONE) 1218 1200 { 1219 1201 *pIpv4 = Ipv4; 1220 bstr_t mask(pCurMask); 1221 *pMaskv4 = inet_addr((char*)(mask)); 1202 1203 com::Utf8Str strMask(pBstrCurMask); 1204 *pMaskv4 = inet_addr(strMask.c_str()); 1222 1205 break; 1223 1206 } … … 1225 1208 } 1226 1209 } 1227 else1228 {1229 *pIpv4 = 0;1230 *pMaskv4 = 0;1231 }1232 1233 1210 VariantClear(&vtMask); 1234 1211 } 1235 1212 } 1236 else1237 {1238 *pIpv4 = 0;1239 *pMaskv4 = 0;1240 }1241 1242 1213 VariantClear(&vtIp); 1243 1214 } … … 1308 1279 ULONG i; 1309 1280 *pFound = false; 1310 ComPtr 1281 ComPtr<IWbemClassObject> pAdapterConfig; 1311 1282 for (i = 0; 1312 1283 (hr = netIfWinFindAdapterClassById(pSvc, pGuid, pAdapterConfig.asOutParam())) == S_OK … … 1324 1295 #endif /* unused */ 1325 1296 1326 static HRESULT netIfWinCreateIWbemServices(IWbemServices ** 1297 static HRESULT netIfWinCreateIWbemServices(IWbemServices **ppSvc) 1327 1298 { 1328 1299 IWbemLocator *pLoc = NULL; 1329 HRESULT hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) 1300 HRESULT hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&pLoc); 1330 1301 if (SUCCEEDED(hr)) 1331 1302 { 1332 1303 IWbemServices *pSvc = NULL; 1333 hr = pLoc->ConnectServer( bstr_t(L"ROOT\\CIMV2"), /* [in] const BSTR strNetworkResource */1334 NULL, /* [in] const BSTR strUser */1335 NULL, /* [in] const BSTR strPassword */1336 0, /* [in] const BSTR strLocale */1337 NULL, /* [in] LONG lSecurityFlags */1338 0, /* [in] const BSTR strAuthority */1339 0, /* [in] IWbemContext* pCtx */1340 &pSvc /* [out] IWbemServices** ppNamespace */);1304 hr = pLoc->ConnectServer(com::Bstr(L"ROOT\\CIMV2").raw(), /* [in] const BSTR strNetworkResource */ 1305 NULL, /* [in] const BSTR strUser */ 1306 NULL, /* [in] const BSTR strPassword */ 1307 0, /* [in] const BSTR strLocale */ 1308 NULL, /* [in] LONG lSecurityFlags */ 1309 0, /* [in] const BSTR strAuthority */ 1310 0, /* [in] IWbemContext* pCtx */ 1311 &pSvc /* [out] IWbemServices** ppNamespace */); 1341 1312 if (SUCCEEDED(hr)) 1342 1313 { 1343 1314 hr = CoSetProxyBlanket(pSvc, /* IUnknown * pProxy */ 1344 RPC_C_AUTHN_WINNT, /* DWORD dwAuthnSvc */1345 RPC_C_AUTHZ_NONE, /* DWORD dwAuthzSvc */1346 NULL, /* WCHAR * pServerPrincName */1347 RPC_C_AUTHN_LEVEL_CALL, /* DWORD dwAuthnLevel */1348 RPC_C_IMP_LEVEL_IMPERSONATE, /* DWORD dwImpLevel */1349 NULL, /* RPC_AUTH_IDENTITY_HANDLE pAuthInfo */1350 EOAC_NONE /* DWORD dwCapabilities */1351 );1315 RPC_C_AUTHN_WINNT, /* DWORD dwAuthnSvc */ 1316 RPC_C_AUTHZ_NONE, /* DWORD dwAuthzSvc */ 1317 NULL, /* WCHAR * pServerPrincName */ 1318 RPC_C_AUTHN_LEVEL_CALL, /* DWORD dwAuthnLevel */ 1319 RPC_C_IMP_LEVEL_IMPERSONATE, /* DWORD dwImpLevel */ 1320 NULL, /* RPC_AUTH_IDENTITY_HANDLE pAuthInfo */ 1321 EOAC_NONE /* DWORD dwCapabilities */ 1322 ); 1352 1323 if (SUCCEEDED(hr)) 1353 1324 { … … 1357 1328 return hr; 1358 1329 } 1359 else 1360 NonStandardLogFlow(("CoSetProxyBlanket failed, hr (0x%x)\n", hr)); 1361 1330 1331 NonStandardLogFlow(("CoSetProxyBlanket failed: %Rhrc\n", hr)); 1362 1332 pSvc->Release(); 1363 1333 } 1364 1334 else 1365 NonStandardLogFlow(("ConnectServer failed , hr (0x%x)\n", hr));1335 NonStandardLogFlow(("ConnectServer failed: %Rhrc\n", hr)); 1366 1336 pLoc->Release(); 1367 1337 } 1368 1338 else 1369 NonStandardLogFlow(("CoCreateInstance failed , hr (0x%x)\n", hr));1370 return hr; 1371 } 1372 1373 static HRESULT netIfWinAdapterConfigPath(IWbemClassObject *pObj, BSTR * pStr)1339 NonStandardLogFlow(("CoCreateInstance failed: %Rhrc\n", hr)); 1340 return hr; 1341 } 1342 1343 static HRESULT netIfWinAdapterConfigPath(IWbemClassObject *pObj, com::Bstr *pRet) 1374 1344 { 1375 1345 VARIANT index; 1346 VariantInit(&index); 1376 1347 HRESULT hr = pObj->Get(L"Index", 0, &index, 0, 0); 1377 1348 if (SUCCEEDED(hr)) 1378 { 1379 WCHAR strIndex[8]; 1380 swprintf(strIndex, L"%u", index.uintVal); 1381 *pStr = (bstr_t(L"Win32_NetworkAdapterConfiguration.Index='") + strIndex + "'").copy(); 1382 } 1349 hr = pRet->printfNoThrow("Win32_NetworkAdapterConfiguration.Index='%u'", index.uintVal); 1383 1350 else 1384 NonStandardLogFlow(("Get failed, hr (0x%x)\n", hr)); 1385 return hr; 1386 } 1387 1388 static HRESULT netIfExecMethod(IWbemServices * pSvc, IWbemClassObject *pClass, BSTR ObjPath, 1389 BSTR MethodName, LPWSTR *pArgNames, LPVARIANT *pArgs, UINT cArgs, 1390 IWbemClassObject** ppOutParams 1391 ) 1392 { 1393 HRESULT hr = S_OK; 1394 ComPtr<IWbemClassObject> pInParamsDefinition; 1395 ComPtr<IWbemClassObject> pClassInstance; 1396 1397 if (cArgs) 1398 { 1399 hr = pClass->GetMethod(MethodName, 0, pInParamsDefinition.asOutParam(), NULL); 1351 { 1352 pRet->setNull(); 1353 NonStandardLogFlow(("Get failed: %Rhrc\n", hr)); 1354 } 1355 return hr; 1356 } 1357 1358 static HRESULT netIfExecMethod(IWbemServices * pSvc, IWbemClassObject *pClass, com::Bstr const &rObjPath, 1359 const char *pszMethodName, LPWSTR *papwszArgNames, LPVARIANT *pArgs, UINT cArgs, 1360 IWbemClassObject **ppOutParams) 1361 { 1362 *ppOutParams = NULL; 1363 com::Bstr bstrMethodName; 1364 HRESULT hr = bstrMethodName.assignEx(pszMethodName); 1365 if (SUCCEEDED(hr)) 1366 { 1367 ComPtr<IWbemClassObject> pInParamsDefinition; 1368 ComPtr<IWbemClassObject> pClassInstance; 1369 if (cArgs) 1370 { 1371 hr = pClass->GetMethod(bstrMethodName.raw(), 0, pInParamsDefinition.asOutParam(), NULL); 1372 if (SUCCEEDED(hr)) 1373 { 1374 hr = pInParamsDefinition->SpawnInstance(0, pClassInstance.asOutParam()); 1375 if (SUCCEEDED(hr)) 1376 { 1377 for (UINT i = 0; i < cArgs; i++) 1378 { 1379 hr = pClassInstance->Put(papwszArgNames[i], 0, pArgs[i], 0); 1380 if (FAILED(hr)) 1381 break; 1382 } 1383 } 1384 } 1385 } 1386 1400 1387 if (SUCCEEDED(hr)) 1401 1388 { 1402 hr = pInParamsDefinition->SpawnInstance(0, pClassInstance.asOutParam()); 1389 IWbemClassObject *pOutParams = NULL; 1390 hr = pSvc->ExecMethod(rObjPath.raw(), bstrMethodName.raw(), 0, NULL, pClassInstance, &pOutParams, NULL); 1403 1391 if (SUCCEEDED(hr)) 1404 { 1405 for (UINT i = 0; i < cArgs; i++) 1406 { 1407 hr = pClassInstance->Put(pArgNames[i], 0, 1408 pArgs[i], 0); 1409 if (FAILED(hr)) 1410 break; 1411 } 1412 } 1413 } 1414 } 1415 1416 if (SUCCEEDED(hr)) 1417 { 1418 IWbemClassObject* pOutParams = NULL; 1419 hr = pSvc->ExecMethod(ObjPath, MethodName, 0, NULL, pClassInstance, &pOutParams, NULL); 1392 *ppOutParams = pOutParams; 1393 } 1394 } 1395 1396 return hr; 1397 } 1398 1399 static HRESULT netIfWinCreateIpArray(SAFEARRAY **ppArray, in_addr const *paIps, UINT cIps) 1400 { 1401 HRESULT hr = S_OK; 1402 SAFEARRAY *pIpArray = SafeArrayCreateVector(VT_BSTR, 0, cIps); 1403 if (pIpArray) 1404 { 1405 for (UINT i = 0; i < cIps; i++) 1406 { 1407 com::Bstr bstrVal; 1408 hr = bstrVal.printfNoThrow("%RTnaipv4", paIps[i].s_addr); 1409 if (SUCCEEDED(hr)) 1410 { 1411 Assert(bstrVal.equals(inet_ntoa(paIps[i]))); 1412 1413 BSTR pRawVal; 1414 hr = bstrVal.detachToEx(&pRawVal); 1415 if (SUCCEEDED(hr)) 1416 { 1417 LONG aIndex[1] = { (LONG)i }; 1418 hr = SafeArrayPutElement(pIpArray, aIndex, pRawVal); 1419 if (SUCCEEDED(hr)) 1420 continue; 1421 SysFreeString(pRawVal); 1422 } 1423 } 1424 break; 1425 } 1426 1420 1427 if (SUCCEEDED(hr)) 1421 {1422 *ppOutParams = pOutParams;1423 }1424 }1425 1426 return hr;1427 }1428 1429 static HRESULT netIfWinCreateIpArray(SAFEARRAY **ppArray, in_addr* aIp, UINT cIp)1430 {1431 HRESULT hr = S_OK; /* MSC maybe used uninitialized */1432 SAFEARRAY * pIpArray = SafeArrayCreateVector(VT_BSTR, 0, cIp);1433 if (pIpArray)1434 {1435 for (UINT i = 0; i < cIp; i++)1436 {1437 char* addr = inet_ntoa(aIp[i]);1438 BSTR val = bstr_t(addr).copy();1439 long aIndex[1];1440 aIndex[0] = i;1441 hr = SafeArrayPutElement(pIpArray, aIndex, val);1442 if (FAILED(hr))1443 {1444 SysFreeString(val);1445 SafeArrayDestroy(pIpArray);1446 break;1447 }1448 }1449 1450 if (SUCCEEDED(hr))1451 {1452 1428 *ppArray = pIpArray; 1453 } 1429 else 1430 SafeArrayDestroy(pIpArray); 1454 1431 } 1455 1432 else 1456 1433 hr = HRESULT_FROM_WIN32(GetLastError()); 1457 1458 1434 return hr; 1459 1435 } … … 1466 1442 if (pIpArray) 1467 1443 { 1468 BSTR val = bstr_t(Ip, false).copy();1444 BSTR val = com::Bstr(Ip, false).copy(); 1469 1445 long aIndex[1]; 1470 1446 aIndex[0] = 0; … … 1489 1465 1490 1466 1491 static HRESULT netIfWinCreateIpArrayVariantV4(VARIANT * pIpAddresses, in_addr* aIp, UINT cIp) 1492 { 1493 HRESULT hr; 1467 static HRESULT netIfWinCreateIpArrayVariantV4(VARIANT *pIpAddresses, in_addr const *paIps, UINT cIps) 1468 { 1494 1469 VariantInit(pIpAddresses); 1495 1470 pIpAddresses->vt = VT_ARRAY | VT_BSTR; 1471 1496 1472 SAFEARRAY *pIpArray; 1497 hr = netIfWinCreateIpArray(&pIpArray, aIp, cIp);1473 HRESULT hr = netIfWinCreateIpArray(&pIpArray, paIps, cIps); 1498 1474 if (SUCCEEDED(hr)) 1499 {1500 1475 pIpAddresses->parray = pIpArray; 1501 }1502 1476 return hr; 1503 1477 } … … 1519 1493 #endif 1520 1494 1521 static HRESULT netIfWinEnableStatic(IWbemServices *pSvc, const GUID *pGuid, BSTR ObjPath, VARIANT *pIp, VARIANT *pMask) 1495 static HRESULT netIfWinEnableStatic(IWbemServices *pSvc, const GUID *pGuid, com::Bstr &rObjPath, VARIANT *pIp, VARIANT *pMask) 1496 { 1497 com::Bstr bstrClassName; 1498 HRESULT hr = bstrClassName.assignEx("Win32_NetworkAdapterConfiguration"); 1499 if (SUCCEEDED(hr)) 1500 { 1501 ComPtr<IWbemClassObject> pClass; 1502 hr = pSvc->GetObject(bstrClassName.raw(), 0, NULL, pClass.asOutParam(), NULL); 1503 if (SUCCEEDED(hr)) 1504 { 1505 LPWSTR argNames[] = {L"IPAddress", L"SubnetMask"}; 1506 LPVARIANT args[] = { pIp, pMask }; 1507 1508 ComPtr<IWbemClassObject> pOutParams; 1509 hr = netIfExecMethod(pSvc, pClass, rObjPath.raw(), "EnableStatic", argNames, args, 1510 2, pOutParams.asOutParam()); 1511 if (SUCCEEDED(hr)) 1512 { 1513 com::Bstr bstrReturnValue; 1514 hr = bstrReturnValue.assignEx("ReturnValue"); 1515 if (SUCCEEDED(hr)) 1516 { 1517 VARIANT varReturnValue; 1518 VariantInit(&varReturnValue); 1519 hr = pOutParams->Get(bstrReturnValue.raw(), 0, &varReturnValue, NULL, 0); 1520 Assert(SUCCEEDED(hr)); 1521 if (SUCCEEDED(hr)) 1522 { 1523 //Assert(varReturnValue.vt == VT_UINT); 1524 int winEr = varReturnValue.uintVal; 1525 switch (winEr) 1526 { 1527 case 0: 1528 { 1529 hr = S_OK; 1530 //bool bFound; 1531 //HRESULT tmpHr = netIfWinWaitIpSettings(pSvc, pGuid, pIp->parray, pMask->parray, 180, &bFound); 1532 NOREF(pGuid); 1533 break; 1534 } 1535 default: 1536 hr = HRESULT_FROM_WIN32( winEr ); 1537 break; 1538 } 1539 } 1540 } 1541 } 1542 } 1543 } 1544 return hr; 1545 } 1546 1547 1548 static HRESULT netIfWinEnableStaticV4(IWbemServices *pSvc, const GUID *pGuid, com::Bstr &rObjPath, 1549 in_addr const *paIps, in_addr const *paMasks, UINT cIpAndMasks) 1550 { 1551 VARIANT ipAddresses; 1552 HRESULT hr = netIfWinCreateIpArrayVariantV4(&ipAddresses, paIps, cIpAndMasks); 1553 if (SUCCEEDED(hr)) 1554 { 1555 VARIANT ipMasks; 1556 hr = netIfWinCreateIpArrayVariantV4(&ipMasks, paMasks, cIpAndMasks); 1557 if (SUCCEEDED(hr)) 1558 { 1559 hr = netIfWinEnableStatic(pSvc, pGuid, rObjPath, &ipAddresses, &ipMasks); 1560 VariantClear(&ipMasks); 1561 } 1562 VariantClear(&ipAddresses); 1563 } 1564 return hr; 1565 } 1566 1567 #if 0 /* unused */ 1568 1569 static HRESULT netIfWinEnableStaticV4V6(IWbemServices * pSvc, const GUID * pGuid, BSTR ObjPath, BSTR Ip, BSTR Mask) 1570 { 1571 VARIANT ipAddresses; 1572 HRESULT hr = netIfWinCreateIpArrayVariantV4V6(&ipAddresses, Ip); 1573 if (SUCCEEDED(hr)) 1574 { 1575 VARIANT ipMasks; 1576 hr = netIfWinCreateIpArrayVariantV4V6(&ipMasks, Mask); 1577 if (SUCCEEDED(hr)) 1578 { 1579 hr = netIfWinEnableStatic(pSvc, pGuid, ObjPath, &ipAddresses, &ipMasks); 1580 VariantClear(&ipMasks); 1581 } 1582 VariantClear(&ipAddresses); 1583 } 1584 return hr; 1585 } 1586 1587 /* win API allows to set gw metrics as well, we are not setting them */ 1588 static HRESULT netIfWinSetGateways(IWbemServices * pSvc, BSTR ObjPath, VARIANT * pGw) 1522 1589 { 1523 1590 ComPtr<IWbemClassObject> pClass; … … 1529 1596 if (SUCCEEDED(hr)) 1530 1597 { 1531 LPWSTR argNames[] = {L"IPAddress", L"SubnetMask"};1532 LPVARIANT args[] = {pIp, pMask};1533 ComPtr<IWbemClassObject> pOutParams;1534 1535 hr = netIfExecMethod(pSvc, pClass, ObjPath, bstr_t(L"EnableStatic"), argNames, args, 2, pOutParams.asOutParam());1536 if (SUCCEEDED(hr))1537 {1538 VARIANT varReturnValue;1539 hr = pOutParams->Get(bstr_t(L"ReturnValue"), 0,1540 &varReturnValue, NULL, 0);1541 Assert(SUCCEEDED(hr));1542 if (SUCCEEDED(hr))1543 {1544 // Assert(varReturnValue.vt == VT_UINT);1545 int winEr = varReturnValue.uintVal;1546 switch (winEr)1547 {1548 case 0:1549 {1550 hr = S_OK;1551 // bool bFound;1552 // HRESULT tmpHr = netIfWinWaitIpSettings(pSvc, pGuid, pIp->parray, pMask->parray, 180, &bFound);1553 NOREF(pGuid);1554 break;1555 }1556 default:1557 hr = HRESULT_FROM_WIN32( winEr );1558 break;1559 }1560 }1561 }1562 }1563 SysFreeString(ClassName);1564 }1565 else1566 hr = HRESULT_FROM_WIN32(GetLastError());1567 1568 return hr;1569 }1570 1571 1572 static HRESULT netIfWinEnableStaticV4(IWbemServices * pSvc, const GUID * pGuid, BSTR ObjPath, in_addr* aIp, in_addr * aMask, UINT cIp)1573 {1574 VARIANT ipAddresses;1575 HRESULT hr = netIfWinCreateIpArrayVariantV4(&ipAddresses, aIp, cIp);1576 if (SUCCEEDED(hr))1577 {1578 VARIANT ipMasks;1579 hr = netIfWinCreateIpArrayVariantV4(&ipMasks, aMask, cIp);1580 if (SUCCEEDED(hr))1581 {1582 hr = netIfWinEnableStatic(pSvc, pGuid, ObjPath, &ipAddresses, &ipMasks);1583 VariantClear(&ipMasks);1584 }1585 VariantClear(&ipAddresses);1586 }1587 return hr;1588 }1589 1590 #if 0 /* unused */1591 1592 static HRESULT netIfWinEnableStaticV4V6(IWbemServices * pSvc, const GUID * pGuid, BSTR ObjPath, BSTR Ip, BSTR Mask)1593 {1594 VARIANT ipAddresses;1595 HRESULT hr = netIfWinCreateIpArrayVariantV4V6(&ipAddresses, Ip);1596 if (SUCCEEDED(hr))1597 {1598 VARIANT ipMasks;1599 hr = netIfWinCreateIpArrayVariantV4V6(&ipMasks, Mask);1600 if (SUCCEEDED(hr))1601 {1602 hr = netIfWinEnableStatic(pSvc, pGuid, ObjPath, &ipAddresses, &ipMasks);1603 VariantClear(&ipMasks);1604 }1605 VariantClear(&ipAddresses);1606 }1607 return hr;1608 }1609 1610 /* win API allows to set gw metrics as well, we are not setting them */1611 static HRESULT netIfWinSetGateways(IWbemServices * pSvc, BSTR ObjPath, VARIANT * pGw)1612 {1613 ComPtr<IWbemClassObject> pClass;1614 BSTR ClassName = SysAllocString(L"Win32_NetworkAdapterConfiguration");1615 HRESULT hr;1616 if (ClassName)1617 {1618 hr = pSvc->GetObject(ClassName, 0, NULL, pClass.asOutParam(), NULL);1619 if (SUCCEEDED(hr))1620 {1621 1598 LPWSTR argNames[] = {L"DefaultIPGateway"}; 1622 1599 LPVARIANT args[] = {pGw}; 1623 1600 ComPtr<IWbemClassObject> pOutParams; 1624 1601 1625 hr = netIfExecMethod(pSvc, pClass, ObjPath, bstr_t(L"SetGateways"), argNames, args, 1, pOutParams.asOutParam());1602 hr = netIfExecMethod(pSvc, pClass, ObjPath, com::Bstr(L"SetGateways"), argNames, args, 1, pOutParams.asOutParam()); 1626 1603 if (SUCCEEDED(hr)) 1627 1604 { 1628 1605 VARIANT varReturnValue; 1629 hr = pOutParams->Get( bstr_t(L"ReturnValue"), 0, &varReturnValue, NULL, 0);1606 hr = pOutParams->Get(com::Bstr(L"ReturnValue"), 0, &varReturnValue, NULL, 0); 1630 1607 Assert(SUCCEEDED(hr)); 1631 1608 if (SUCCEEDED(hr)) … … 1681 1658 #endif /* unused */ 1682 1659 1683 static HRESULT netIfWinEnableDHCP(IWbemServices * pSvc, BSTRObjPath)1684 { 1685 ComPtr<IWbemClassObject> pClass;1686 BSTR ClassName = SysAllocString(L"Win32_NetworkAdapterConfiguration");1687 HRESULT hr;1688 if (ClassName)1689 {1690 hr = pSvc->GetObject( ClassName, 0, NULL, pClass.asOutParam(), NULL);1660 static HRESULT netIfWinEnableDHCP(IWbemServices * pSvc, const com::Bstr &rObjPath) 1661 { 1662 com::Bstr bstrClassName; 1663 HRESULT hr = bstrClassName.assignEx("Win32_NetworkAdapterConfiguration"); 1664 if (SUCCEEDED(hr)) 1665 { 1666 ComPtr<IWbemClassObject> pClass; 1667 hr = pSvc->GetObject(bstrClassName.raw(), 0, NULL, pClass.asOutParam(), NULL); 1691 1668 if (SUCCEEDED(hr)) 1692 1669 { 1693 1670 ComPtr<IWbemClassObject> pOutParams; 1694 1695 hr = netIfExecMethod(pSvc, pClass, ObjPath, bstr_t(L"EnableDHCP"), NULL, NULL, 0, pOutParams.asOutParam()); 1671 hr = netIfExecMethod(pSvc, pClass, rObjPath, "EnableDHCP", NULL, NULL, 0, pOutParams.asOutParam()); 1696 1672 if (SUCCEEDED(hr)) 1697 1673 { 1698 VARIANT varReturnValue; 1699 hr = pOutParams->Get(bstr_t(L"ReturnValue"), 0, 1700 &varReturnValue, NULL, 0); 1701 Assert(SUCCEEDED(hr)); 1674 com::Bstr bstrReturnValue; 1675 hr = bstrReturnValue.assignEx("ReturnValue"); 1702 1676 if (SUCCEEDED(hr)) 1703 1677 { 1704 // Assert(varReturnValue.vt == VT_UINT); 1705 int winEr = varReturnValue.uintVal; 1706 switch (winEr) 1678 VARIANT varReturnValue; 1679 VariantInit(&varReturnValue); 1680 hr = pOutParams->Get(bstrReturnValue.raw(), 0, &varReturnValue, NULL, 0); 1681 Assert(SUCCEEDED(hr)); 1682 if (SUCCEEDED(hr)) 1707 1683 { 1708 case 0: 1709 hr = S_OK; 1710 break; 1711 default: 1712 hr = HRESULT_FROM_WIN32( winEr ); 1713 break; 1684 //Assert(varReturnValue.vt == VT_UINT); 1685 int winEr = varReturnValue.uintVal; 1686 switch (winEr) 1687 { 1688 case 0: 1689 hr = S_OK; 1690 break; 1691 default: 1692 hr = HRESULT_FROM_WIN32( winEr ); 1693 break; 1694 } 1714 1695 } 1715 1696 } 1716 1697 } 1717 1698 } 1718 SysFreeString(ClassName); 1719 } 1720 else 1721 hr = HRESULT_FROM_WIN32(GetLastError()); 1722 1723 return hr; 1724 } 1725 1726 static HRESULT netIfWinDhcpRediscover(IWbemServices * pSvc, BSTR ObjPath) 1727 { 1728 ComPtr<IWbemClassObject> pClass; 1729 BSTR ClassName = SysAllocString(L"Win32_NetworkAdapterConfiguration"); 1730 HRESULT hr; 1731 if (ClassName) 1732 { 1733 hr = pSvc->GetObject(ClassName, 0, NULL, pClass.asOutParam(), NULL); 1699 } 1700 return hr; 1701 } 1702 1703 static HRESULT netIfWinDhcpRediscover(IWbemServices * pSvc, const com::Bstr &rObjPath) 1704 { 1705 com::Bstr bstrClassName; 1706 HRESULT hr = bstrClassName.assignEx("Win32_NetworkAdapterConfiguration"); 1707 if (SUCCEEDED(hr)) 1708 { 1709 ComPtr<IWbemClassObject> pClass; 1710 hr = pSvc->GetObject(bstrClassName.raw(), 0, NULL, pClass.asOutParam(), NULL); 1734 1711 if (SUCCEEDED(hr)) 1735 1712 { 1736 1713 ComPtr<IWbemClassObject> pOutParams; 1737 1738 hr = netIfExecMethod(pSvc, pClass, ObjPath, bstr_t(L"ReleaseDHCPLease"), NULL, NULL, 0, pOutParams.asOutParam()); 1714 hr = netIfExecMethod(pSvc, pClass, rObjPath, "ReleaseDHCPLease", NULL, NULL, 0, pOutParams.asOutParam()); 1739 1715 if (SUCCEEDED(hr)) 1740 1716 { 1741 VARIANT varReturnValue; 1742 hr = pOutParams->Get(bstr_t(L"ReturnValue"), 0, &varReturnValue, NULL, 0); 1743 Assert(SUCCEEDED(hr)); 1717 com::Bstr bstrReturnValue; 1718 hr = bstrReturnValue.assignEx("ReturnValue"); 1744 1719 if (SUCCEEDED(hr)) 1745 1720 { 1746 // Assert(varReturnValue.vt == VT_UINT); 1747 int winEr = varReturnValue.uintVal; 1748 if (winEr == 0) 1721 VARIANT varReturnValue; 1722 VariantInit(&varReturnValue); 1723 hr = pOutParams->Get(bstrReturnValue.raw(), 0, &varReturnValue, NULL, 0); 1724 Assert(SUCCEEDED(hr)); 1725 if (SUCCEEDED(hr)) 1749 1726 { 1750 hr = netIfExecMethod(pSvc, pClass, ObjPath, bstr_t(L"RenewDHCPLease"), NULL, NULL, 0, pOutParams.asOutParam()); 1751 if (SUCCEEDED(hr)) 1727 //Assert(varReturnValue.vt == VT_UINT); 1728 int winEr = varReturnValue.uintVal; 1729 if (winEr == 0) 1752 1730 { 1753 hr = pOutParams->Get(bstr_t(L"ReturnValue"), 0, &varReturnValue, NULL, 0); 1754 Assert(SUCCEEDED(hr)); 1731 hr = netIfExecMethod(pSvc, pClass, rObjPath, "RenewDHCPLease", NULL, NULL, 0, pOutParams.asOutParam()); 1755 1732 if (SUCCEEDED(hr)) 1756 1733 { 1757 // Assert(varReturnValue.vt == VT_UINT); 1758 winEr = varReturnValue.uintVal; 1759 if (winEr == 0) 1760 hr = S_OK; 1761 else 1762 hr = HRESULT_FROM_WIN32( winEr ); 1734 hr = pOutParams->Get(bstrReturnValue.raw(), 0, &varReturnValue, NULL, 0); 1735 Assert(SUCCEEDED(hr)); 1736 if (SUCCEEDED(hr)) 1737 { 1738 //Assert(varReturnValue.vt == VT_UINT); 1739 winEr = varReturnValue.uintVal; 1740 if (winEr == 0) 1741 hr = S_OK; 1742 else 1743 hr = HRESULT_FROM_WIN32( winEr ); 1744 } 1763 1745 } 1764 1746 } 1747 else 1748 hr = HRESULT_FROM_WIN32( winEr ); 1765 1749 } 1766 else 1767 hr = HRESULT_FROM_WIN32( winEr ); 1768 } 1769 } 1770 } 1771 SysFreeString(ClassName); 1772 } 1773 else 1774 hr = HRESULT_FROM_WIN32(GetLastError()); 1775 1776 return hr; 1777 } 1778 1779 static HRESULT vboxNetCfgWinIsDhcpEnabled(IWbemClassObject * pAdapterConfig, BOOL *pEnabled) 1750 } 1751 } 1752 } 1753 } 1754 1755 return hr; 1756 } 1757 1758 static HRESULT vboxNetCfgWinIsDhcpEnabled(IWbemClassObject *pAdapterConfig, BOOL *pfEnabled) 1780 1759 { 1781 1760 VARIANT vtEnabled; 1761 VariantInit(&vtEnabled); 1782 1762 HRESULT hr = pAdapterConfig->Get(L"DHCPEnabled", 0, &vtEnabled, 0, 0); 1783 1763 if (SUCCEEDED(hr)) 1784 *pEnabled = vtEnabled.boolVal; 1785 return hr; 1786 } 1787 1788 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinGetAdapterSettings(IN const GUID * pGuid, OUT PADAPTER_SETTINGS pSettings) 1789 { 1790 HRESULT hr; 1791 ComPtr <IWbemServices> pSvc; 1792 hr = netIfWinCreateIWbemServices(pSvc.asOutParam()); 1764 *pfEnabled = vtEnabled.boolVal; 1765 else 1766 *pfEnabled = FALSE; 1767 return hr; 1768 } 1769 1770 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinGetAdapterSettings(IN const GUID *pGuid, OUT PADAPTER_SETTINGS pSettings) 1771 { 1772 ComPtr<IWbemServices> pSvc; 1773 HRESULT hr = netIfWinCreateIWbemServices(pSvc.asOutParam()); 1793 1774 if (SUCCEEDED(hr)) 1794 1775 { … … 1808 1789 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinIsDhcpEnabled(const GUID * pGuid, BOOL *pEnabled) 1809 1790 { 1810 HRESULT hr; 1811 ComPtr <IWbemServices> pSvc; 1812 hr = netIfWinCreateIWbemServices(pSvc.asOutParam()); 1791 ComPtr<IWbemServices> pSvc; 1792 HRESULT hr = netIfWinCreateIWbemServices(pSvc.asOutParam()); 1813 1793 if (SUCCEEDED(hr)) 1814 1794 { … … 1838 1818 if (SUCCEEDED(hr)) 1839 1819 { 1840 BOOL bIsHostOnly;1841 hr = netIfWinIsHostOnly(pAdapterConfig, & bIsHostOnly);1820 BOOL fIsHostOnly; 1821 hr = netIfWinIsHostOnly(pAdapterConfig, &fIsHostOnly); 1842 1822 if (SUCCEEDED(hr)) 1843 1823 { 1844 if ( bIsHostOnly)1824 if (fIsHostOnly) 1845 1825 { 1846 1826 in_addr aIp[1]; 1847 1827 in_addr aMask[1]; 1848 aIp[0].S_un.S_addr = ip;1828 aIp[0].S_un.S_addr = ip; 1849 1829 aMask[0].S_un.S_addr = mask; 1850 1830 1851 BSTRObjPath;1852 hr = netIfWinAdapterConfigPath(pAdapterConfig, & ObjPath);1831 com::Bstr bstrObjPath; 1832 hr = netIfWinAdapterConfigPath(pAdapterConfig, &bstrObjPath); 1853 1833 if (SUCCEEDED(hr)) 1854 1834 { 1855 hr = netIfWinEnableStaticV4(pSvc, pGuid, ObjPath, aIp, aMask, ip != 0 ? 1 : 0);1835 hr = netIfWinEnableStaticV4(pSvc, pGuid, bstrObjPath, aIp, aMask, ip != 0 ? 1 : 0); 1856 1836 if (SUCCEEDED(hr)) 1857 1837 { … … 1859 1839 in_addr aGw[1]; 1860 1840 aGw[0].S_un.S_addr = gw; 1861 hr = netIfWinSetGatewaysV4(pSvc, ObjPath, aGw, 1);1841 hr = netIfWinSetGatewaysV4(pSvc, bstrObjPath, aGw, 1); 1862 1842 if (SUCCEEDED(hr)) 1863 1843 #endif … … 1865 1845 } 1866 1846 } 1867 SysFreeString(ObjPath);1868 1847 } 1869 1848 } … … 1876 1855 } 1877 1856 1878 NonStandardLogFlow(("VBoxNetCfgWinEnableStaticIpConfig: returns 0x%x\n", hr));1857 NonStandardLogFlow(("VBoxNetCfgWinEnableStaticIpConfig: returns %Rhrc\n", hr)); 1879 1858 return hr; 1880 1859 } … … 1884 1863 { 1885 1864 HRESULT hr; 1886 ComPtr 1865 ComPtr<IWbemServices> pSvc; 1887 1866 hr = netIfWinCreateIWbemServices(pSvc.asOutParam()); 1888 1867 if (SUCCEEDED(hr)) … … 1931 1910 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinEnableDynamicIpConfig(IN const GUID *pGuid) 1932 1911 { 1933 HRESULT hr; 1934 ComPtr <IWbemServices> pSvc; 1935 hr = netIfWinCreateIWbemServices(pSvc.asOutParam()); 1936 if (SUCCEEDED(hr)) 1937 { 1938 ComPtr<IWbemClassObject> pAdapterConfig; 1939 hr = netIfWinFindAdapterClassById(pSvc, pGuid, pAdapterConfig.asOutParam()); 1940 if (SUCCEEDED(hr)) 1941 { 1942 BOOL bIsHostOnly; 1943 hr = netIfWinIsHostOnly(pAdapterConfig, &bIsHostOnly); 1944 if (SUCCEEDED(hr)) 1945 { 1946 if (bIsHostOnly) 1947 { 1948 BSTR ObjPath; 1949 hr = netIfWinAdapterConfigPath(pAdapterConfig, &ObjPath); 1950 if (SUCCEEDED(hr)) 1951 { 1952 hr = netIfWinEnableDHCP(pSvc, ObjPath); 1953 if (SUCCEEDED(hr)) 1954 { 1955 // hr = netIfWinUpdateConfig(pIf); 1956 } 1957 SysFreeString(ObjPath); 1958 } 1959 } 1960 else 1961 { 1962 hr = E_FAIL; 1963 } 1964 } 1965 } 1966 } 1967 1968 1969 return hr; 1970 } 1971 1972 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinDhcpRediscover(IN const GUID *pGuid) 1973 { 1974 HRESULT hr; 1975 ComPtr <IWbemServices> pSvc; 1976 hr = netIfWinCreateIWbemServices(pSvc.asOutParam()); 1912 ComPtr<IWbemServices> pSvc; 1913 HRESULT hr = netIfWinCreateIWbemServices(pSvc.asOutParam()); 1977 1914 if (SUCCEEDED(hr)) 1978 1915 { … … 1981 1918 if (SUCCEEDED(hr)) 1982 1919 { 1983 BOOL bIsHostOnly;1984 hr = netIfWinIsHostOnly(pAdapterConfig, & bIsHostOnly);1920 BOOL fIsHostOnly; 1921 hr = netIfWinIsHostOnly(pAdapterConfig, &fIsHostOnly); 1985 1922 if (SUCCEEDED(hr)) 1986 1923 { 1987 if ( bIsHostOnly)1988 { 1989 BSTRObjPath;1990 hr = netIfWinAdapterConfigPath(pAdapterConfig, & ObjPath);1924 if (fIsHostOnly) 1925 { 1926 com::Bstr bstrObjPath; 1927 hr = netIfWinAdapterConfigPath(pAdapterConfig, &bstrObjPath); 1991 1928 if (SUCCEEDED(hr)) 1992 1929 { 1993 hr = netIfWin DhcpRediscover(pSvc,ObjPath);1930 hr = netIfWinEnableDHCP(pSvc, bstrObjPath); 1994 1931 if (SUCCEEDED(hr)) 1995 1932 { 1996 1933 //hr = netIfWinUpdateConfig(pIf); 1997 1934 } 1998 SysFreeString(ObjPath);1999 1935 } 2000 1936 } 2001 1937 else 2002 {2003 1938 hr = E_FAIL; 2004 } 2005 } 2006 } 2007 } 2008 2009 2010 return hr; 2011 } 2012 2013 static const char *vboxNetCfgWinAddrToStr(char *pszBuf, LPSOCKADDR pAddr) 1939 } 1940 } 1941 } 1942 return hr; 1943 } 1944 1945 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinDhcpRediscover(IN const GUID *pGuid) 1946 { 1947 ComPtr<IWbemServices> pSvc; 1948 HRESULT hr = netIfWinCreateIWbemServices(pSvc.asOutParam()); 1949 if (SUCCEEDED(hr)) 1950 { 1951 ComPtr<IWbemClassObject> pAdapterConfig; 1952 hr = netIfWinFindAdapterClassById(pSvc, pGuid, pAdapterConfig.asOutParam()); 1953 if (SUCCEEDED(hr)) 1954 { 1955 BOOL fIsHostOnly; 1956 hr = netIfWinIsHostOnly(pAdapterConfig, &fIsHostOnly); 1957 if (SUCCEEDED(hr)) 1958 { 1959 if (fIsHostOnly) 1960 { 1961 com::Bstr bstrObjPath; 1962 hr = netIfWinAdapterConfigPath(pAdapterConfig, &bstrObjPath); 1963 if (SUCCEEDED(hr)) 1964 { 1965 hr = netIfWinDhcpRediscover(pSvc, bstrObjPath); 1966 if (SUCCEEDED(hr)) 1967 { 1968 //hr = netIfWinUpdateConfig(pIf); 1969 } 1970 } 1971 } 1972 else 1973 hr = E_FAIL; 1974 } 1975 } 1976 } 1977 1978 1979 return hr; 1980 } 1981 1982 static const char *vboxNetCfgWinAddrToStr(char *pszBuf, size_t cbBuf, LPSOCKADDR pAddr) 2014 1983 { 2015 1984 switch (pAddr->sa_family) 2016 1985 { 2017 1986 case AF_INET: 2018 sprintf(pszBuf, "%d.%d.%d.%d",2019 ((PSOCKADDR_IN)pAddr)->sin_addr.S_un.S_un_b.s_b1,2020 ((PSOCKADDR_IN)pAddr)->sin_addr.S_un.S_un_b.s_b2,2021 ((PSOCKADDR_IN)pAddr)->sin_addr.S_un.S_un_b.s_b3,2022 ((PSOCKADDR_IN)pAddr)->sin_addr.S_un.S_un_b.s_b4);1987 RTStrPrintf(pszBuf, cbBuf, "%d.%d.%d.%d", 1988 ((PSOCKADDR_IN)pAddr)->sin_addr.S_un.S_un_b.s_b1, 1989 ((PSOCKADDR_IN)pAddr)->sin_addr.S_un.S_un_b.s_b2, 1990 ((PSOCKADDR_IN)pAddr)->sin_addr.S_un.S_un_b.s_b3, 1991 ((PSOCKADDR_IN)pAddr)->sin_addr.S_un.S_un_b.s_b4); 2023 1992 break; 2024 1993 case AF_INET6: 2025 sprintf(pszBuf, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",2026 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[0], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[1],2027 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[2], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[3],2028 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[4], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[5],2029 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[6], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[7],2030 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[8], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[9],2031 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[10], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[11],2032 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[12], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[13],2033 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[14], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[15]);1994 RTStrPrintf(pszBuf, cbBuf, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", 1995 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[0], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[1], 1996 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[2], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[3], 1997 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[4], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[5], 1998 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[6], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[7], 1999 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[8], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[9], 2000 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[10], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[11], 2001 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[12], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[13], 2002 ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[14], ((PSOCKADDR_IN6)pAddr)->sin6_addr.s6_addr[15]); 2034 2003 break; 2035 2004 default: 2036 strcpy(pszBuf, "unknown");2005 RTStrCopy(pszBuf, cbBuf, "unknown"); 2037 2006 break; 2038 2007 } … … 2047 2016 for (pAdapter = pAddresses; pAdapter; pAdapter = pAdapter->Next) 2048 2017 { 2049 char szBuf[80];2050 2051 2018 NonStandardLogFlow(("+- Enumerating adapter '%ls' %s\n", pAdapter->FriendlyName, pAdapter->AdapterName)); 2052 2019 for (PIP_ADAPTER_PREFIX pPrefix = pAdapter->FirstPrefix; pPrefix; pPrefix = pPrefix->Next) 2053 2020 { 2054 const char *pcszAddress = vboxNetCfgWinAddrToStr(szBuf, pPrefix->Address.lpSockaddr); 2021 char szBuf[80]; 2022 const char *pcszAddress = vboxNetCfgWinAddrToStr(szBuf, sizeof(szBuf), pPrefix->Address.lpSockaddr); 2023 2055 2024 /* We are concerned with IPv4 only, ignore the rest. */ 2056 2025 if (pPrefix->Address.lpSockaddr->sa_family != AF_INET) … … 2059 2028 continue; 2060 2029 } 2030 2061 2031 /* Ignore invalid prefixes as well as host addresses. */ 2062 2032 if (pPrefix->PrefixLength < 1 || pPrefix->PrefixLength > 31) … … 2065 2035 continue; 2066 2036 } 2037 2067 2038 /* Ignore multicast and beyond. */ 2068 2039 ULONG ip = ((struct sockaddr_in *)pPrefix->Address.lpSockaddr)->sin_addr.s_addr; … … 2072 2043 continue; 2073 2044 } 2045 2074 2046 ULONG mask = htonl((~(((ULONG)~0) >> pPrefix->PrefixLength))); 2075 2047 bool fContinue = pfnCallback(ip, mask, pContext); … … 2079 2051 return; 2080 2052 } 2081 else 2082 2053 2054 NonStandardLogFlow(("| +- %s %d: no conflict, moving on\n", pcszAddress, pPrefix->PrefixLength)); 2083 2055 } 2084 2056 } … … 2088 2060 { 2089 2061 ULONG Prefix; 2090 bool bConflict;2062 bool fConflict; 2091 2063 }IPPROBE_CONTEXT, *PIPPROBE_CONTEXT; 2092 2064 2093 #define IPPROBE_INIT(_pContext, _addr) \ 2094 ((_pContext)->bConflict = false, \ 2095 (_pContext)->Prefix = _addr) 2096 2097 #define IPPROBE_INIT_STR(_pContext, _straddr) \ 2098 IPROBE_INIT(_pContext, inet_addr(_straddr)) 2065 #define IPPROBE_INIT(a_pContext, a_addr) \ 2066 do { (a_pContext)->fConflict = false; (a_pContext)->Prefix = (a_addr); } while (0) 2067 2068 #define IPPROBE_INIT_STR(a_pContext, a_straddr) \ 2069 IPROBE_INIT(a_pContext, inet_addr(_straddr)) 2099 2070 2100 2071 static bool vboxNetCfgWinIpProbeCallback (ULONG ip, ULONG mask, PVOID pContext) … … 2104 2075 if ((ip & mask) == (pProbe->Prefix & mask)) 2105 2076 { 2106 pProbe-> bConflict = true;2077 pProbe->fConflict = true; 2107 2078 return false; 2108 2079 } … … 2113 2084 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinGenHostOnlyNetworkNetworkIp(OUT PULONG pNetIp, OUT PULONG pNetMask) 2114 2085 { 2115 DWORD dwRc;2116 2086 HRESULT hr = S_OK; 2087 2088 *pNetIp = 0; 2089 *pNetMask = 0; 2090 2117 2091 /* 2118 2092 * MSDN recommends to pre-allocate a 15KB buffer. 2119 2093 */ 2120 ULONG uBufLen = 15 * 1024;2121 PIP_ADAPTER_ADDRESSES p Addresses = (PIP_ADAPTER_ADDRESSES)malloc(uBufLen);2122 if (!p Addresses)2123 return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);2124 dwRc = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &uBufLen);2094 ULONG cbBuf = 15 * _1K; 2095 PIP_ADAPTER_ADDRESSES paAddresses = (PIP_ADAPTER_ADDRESSES)RTMemAllocZ(cbBuf); 2096 if (!paAddresses) 2097 return E_OUTOFMEMORY; 2098 DWORD dwRc = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, paAddresses, &cbBuf); 2125 2099 if (dwRc == ERROR_BUFFER_OVERFLOW) 2126 2100 { 2127 2101 /* Impressive! More than 10 adapters! Get more memory and try again. */ 2128 free(pAddresses);2129 p Addresses = (PIP_ADAPTER_ADDRESSES)malloc(uBufLen);2130 if (!p Addresses)2131 return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);2132 dwRc = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, p Addresses, &uBufLen);2102 RTMemFree(paAddresses); 2103 paAddresses = (PIP_ADAPTER_ADDRESSES)RTMemAllocZ(cbBuf); 2104 if (!paAddresses) 2105 return E_OUTOFMEMORY; 2106 dwRc = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, paAddresses, &cbBuf); 2133 2107 } 2134 2108 if (dwRc == NO_ERROR) 2135 2109 { 2136 IPPROBE_CONTEXT Context;2137 2110 const ULONG ip192168 = inet_addr("192.168.0.0"); 2138 srand(GetTickCount()); 2139 2140 *pNetIp = 0; 2141 *pNetMask = 0; 2142 2143 for (int i = 0; i < 255; i++) 2144 { 2145 ULONG ipProbe = rand()*255/RAND_MAX; 2111 for (int i = 0; i < 384; i++) 2112 { 2113 #if 0 2114 ULONG ipProbe = rand()*255 / RAND_MAX; 2115 #else 2116 uint32_t ipProbe = RTRandU32Ex(0, 255); 2117 #endif 2146 2118 ipProbe = ip192168 | (ipProbe << 16); 2147 unsigned char *a = (unsigned char *)&ipProbe; 2148 NonStandardLogFlow(("probing %d.%d.%d.%d\n", a[0], a[1], a[2], a[3])); 2119 NonStandardLogFlow(("probing %RTnaipv4\n", ipProbe)); 2120 2121 IPPROBE_CONTEXT Context; 2149 2122 IPPROBE_INIT(&Context, ipProbe); 2150 vboxNetCfgWinEnumIpConfig(p Addresses, vboxNetCfgWinIpProbeCallback, &Context);2151 if (!Context. bConflict)2152 { 2153 NonStandardLogFlow(("found unused net % d.%d.%d.%d\n", a[0], a[1], a[2], a[3]));2123 vboxNetCfgWinEnumIpConfig(paAddresses, vboxNetCfgWinIpProbeCallback, &Context); 2124 if (!Context.fConflict) 2125 { 2126 NonStandardLogFlow(("found unused net %RTnaipv4\n", ipProbe)); 2154 2127 *pNetIp = ipProbe; 2155 2128 *pNetMask = inet_addr("255.255.255.0"); … … 2161 2134 } 2162 2135 else 2163 NonStandardLogFlow(("GetAdaptersAddresses err (%d)\n", dwRc)); 2164 2165 if (pAddresses) 2166 free(pAddresses); 2136 NonStandardLogFlow(("GetAdaptersAddresses err (%u)\n", dwRc)); 2137 2138 RTMemFree(paAddresses); 2167 2139 2168 2140 if (dwRc != NO_ERROR) 2169 {2170 2141 hr = HRESULT_FROM_WIN32(dwRc); 2171 }2172 2173 2142 return hr; 2174 2143 } … … 2189 2158 2190 2159 hr = VBoxNetCfgWinUninstallComponent(pNc, pNcc); 2191 NonStandardLogFlow(("NetFlt component uninstallation ended with hr ( 0x%x)\n", hr));2160 NonStandardLogFlow(("NetFlt component uninstallation ended with hr (%Rhrc)\n", hr)); 2192 2161 2193 2162 pNcc->Release(); 2194 2163 } 2195 2164 else if (hr == S_FALSE) 2196 {2197 2165 NonStandardLog("NetFlt is not installed currently\n"); 2198 }2199 2166 else 2200 { 2201 NonStandardLogFlow(("FindComponent failed, hr (0x%x)\n", hr)); 2202 } 2167 NonStandardLogFlow(("FindComponent failed: %Rhrc\n", hr)); 2203 2168 2204 2169 VBoxDrvCfgInfUninstallAllF(L"NetService", VBOXNETCFGWIN_NETFLT_ID, InfRmFlags); … … 2213 2178 } 2214 2179 2215 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinNetFltInstall(IN INetCfg *pNc, 2216 IN LPCWSTR const *apInfFullPaths, IN UINT cInfFullPaths) 2180 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinNetFltInstall(IN INetCfg *pNc, IN LPCWSTR const *pwszInfFullPath, IN UINT cInfFullPaths) 2217 2181 { 2218 2182 HRESULT hr = vboxNetCfgWinNetFltUninstall(pNc, SUOI_FORCEDELETE); … … 2222 2186 hr = vboxNetCfgWinInstallInfAndComponent(pNc, VBOXNETCFGWIN_NETFLT_ID, 2223 2187 &GUID_DEVCLASS_NETSERVICE, 2224 apInfFullPaths,2188 pwszInfFullPath, 2225 2189 cInfFullPaths, 2226 2190 NULL); … … 2245 2209 2246 2210 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinNetAdpInstall(IN INetCfg *pNc, 2247 IN LPCWSTR const p InfFullPath)2211 IN LPCWSTR const pwszInfFullPath) 2248 2212 { 2249 2213 NonStandardLog("NetAdp will be installed ...\n"); 2250 HRESULT hr = vboxNetCfgWinInstallInfAndComponent(pNc, VBOXNETCFGWIN_NETADP_ID, 2251 &GUID_DEVCLASS_NET, 2252 &pInfFullPath, 2253 1, 2254 NULL); 2255 return hr; 2256 } 2257 2258 #define VBOXNETCFGWIN_NETLWF_ID L"oracle_VBoxNetLwf" 2214 HRESULT hr = vboxNetCfgWinInstallInfAndComponent(pNc, VBOXNETCFGWIN_NETADP_ID_WSZ, 2215 &GUID_DEVCLASS_NET, 2216 &pwszInfFullPath, 2217 1, 2218 NULL); 2219 return hr; 2220 } 2221 2259 2222 2260 2223 static HRESULT vboxNetCfgWinNetLwfUninstall(IN INetCfg *pNc, DWORD InfRmFlags) … … 2277 2240 else 2278 2241 { 2279 NonStandardLogFlow(("FindComponent failed , hr (0x%x)\n", hr));2242 NonStandardLogFlow(("FindComponent failed: %Rhrc\n", hr)); 2280 2243 hr = S_OK; 2281 2244 } … … 2299 2262 * See @bugref{7899}. 2300 2263 */ 2301 HKEY hNetKey; 2302 DWORD dwMaxNumFilters = 0; 2303 DWORD cbMaxNumFilters = sizeof(dwMaxNumFilters); 2304 LONG hr = RegOpenKeyEx(HKEY_LOCAL_MACHINE, 2305 _T("SYSTEM\\CurrentControlSet\\Control\\Network"), 2306 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &hNetKey); 2307 if (SUCCEEDED(hr)) 2308 { 2309 hr = RegQueryValueEx(hNetKey, _T("MaxNumFilters"), NULL, NULL, 2310 (LPBYTE)&dwMaxNumFilters, &cbMaxNumFilters); 2311 if (SUCCEEDED(hr) && cbMaxNumFilters == sizeof(dwMaxNumFilters) && dwMaxNumFilters == 8) 2264 /** @todo r=bird: This code was mixing HRESULT and LSTATUS, checking the return 2265 * codes using SUCCEEDED(lrc) instead of lrc == ERROR_SUCCESS. So, it might 2266 * have misbehaved on bogus registry content, but worked fine on sane values. */ 2267 HKEY hKeyNet = NULL; 2268 LSTATUS lrc = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Network", 0, 2269 KEY_QUERY_VALUE | KEY_SET_VALUE, &hKeyNet); 2270 if (lrc == ERROR_SUCCESS) 2271 { 2272 DWORD dwMaxNumFilters = 0; 2273 DWORD cbMaxNumFilters = sizeof(dwMaxNumFilters); 2274 lrc = RegQueryValueExW(hKeyNet, L"MaxNumFilters", NULL, NULL, (LPBYTE)&dwMaxNumFilters, &cbMaxNumFilters); 2275 if (lrc == ERROR_SUCCESS && cbMaxNumFilters == sizeof(dwMaxNumFilters) && dwMaxNumFilters == 8) 2312 2276 { 2313 2277 dwMaxNumFilters = 14; 2314 hr = RegSetValueEx(hNetKey, _T("MaxNumFilters"), 0, REG_DWORD, 2315 (LPBYTE)&dwMaxNumFilters, sizeof(dwMaxNumFilters)); 2316 if (SUCCEEDED(hr)) 2278 lrc = RegSetValueExW(hKeyNet, L"MaxNumFilters", 0, REG_DWORD, (LPBYTE)&dwMaxNumFilters, sizeof(dwMaxNumFilters)); 2279 if (lrc == ERROR_SUCCESS) 2317 2280 NonStandardLog("Adjusted the installed filter limit to 14...\n"); 2318 2281 else 2319 NonStandardLog("Failed to set MaxNumFilters, error code 0x%x\n", hr);2320 } 2321 RegCloseKey(h NetKey);2282 NonStandardLog("Failed to set MaxNumFilters, error code %d\n", lrc); 2283 } 2284 RegCloseKey(hKeyNet); 2322 2285 } 2323 2286 else 2324 { 2325 NonStandardLog("Failed to open network key, error code 0x%x\n", hr); 2326 } 2327 2328 } 2329 2330 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinNetLwfInstall(IN INetCfg *pNc, 2331 IN LPCWSTR const pInfFullPath) 2287 NonStandardLog("Failed to open network key, error code %d\n", lrc); 2288 2289 } 2290 2291 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinNetLwfInstall(IN INetCfg *pNc, IN LPCWSTR const pwszInfFullPath) 2332 2292 { 2333 2293 HRESULT hr = vboxNetCfgWinNetLwfUninstall(pNc, SUOI_FORCEDELETE); … … 2338 2298 hr = vboxNetCfgWinInstallInfAndComponent(pNc, VBOXNETCFGWIN_NETLWF_ID, 2339 2299 &GUID_DEVCLASS_NETSERVICE, 2340 &p InfFullPath,2300 &pwszInfFullPath, 2341 2301 1, 2342 2302 NULL); … … 2345 2305 } 2346 2306 2347 #define VBOX_CONNECTION_NAME L"VirtualBox Host-Only Network" 2348 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinGenHostonlyConnectionName(PCWSTR DevName, WCHAR *pBuf, PULONG pcbBuf) 2349 { 2350 const WCHAR * pSuffix = wcsrchr( DevName, L'#' ); 2351 ULONG cbSize = sizeof(VBOX_CONNECTION_NAME); 2352 2353 if (pSuffix) 2354 { 2355 cbSize += (ULONG)wcslen(pSuffix) * 2; 2356 cbSize += 2; /* for space */ 2357 } 2358 2359 if (*pcbBuf < cbSize) 2360 { 2361 *pcbBuf = cbSize; 2362 return E_FAIL; 2363 } 2364 2365 wcscpy(pBuf, VBOX_CONNECTION_NAME); 2366 if (pSuffix) 2367 { 2368 wcscat(pBuf, L" "); 2369 wcscat(pBuf, pSuffix); 2370 } 2371 2372 return S_OK; 2307 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinGenHostonlyConnectionName(IN PCWSTR pwszDevName, OUT WCHAR *pwszBuf, 2308 IN ULONG cwcBuf, OUT PULONG pcwcNeeded) 2309 { 2310 /* Look for a suffix that we need to preserve. */ 2311 size_t const cwcDevName = RTUtf16Len(pwszDevName); 2312 size_t offSuffix = cwcDevName; 2313 while (offSuffix > 0 && pwszDevName[offSuffix - 1] != '#') 2314 offSuffix--; 2315 size_t const cwcSuffix = pwszDevName[offSuffix] != '#' ? 0 : cwcDevName - offSuffix; 2316 2317 /* Calculate required buffer size: */ 2318 size_t cwcNeeded = sizeof(VBOX_CONNECTION_NAME_WSZ) / sizeof(wchar_t) /* includes terminator */ 2319 + !!cwcSuffix /*space*/ + cwcSuffix; 2320 if (pcwcNeeded) 2321 *pcwcNeeded = (ULONG)cwcNeeded; 2322 2323 if (cwcNeeded <= cwcBuf) 2324 { 2325 memcpy(pwszBuf, VBOX_CONNECTION_NAME_WSZ, sizeof(VBOX_CONNECTION_NAME_WSZ)); 2326 if (cwcSuffix > 0) 2327 { 2328 size_t offDst = sizeof(VBOX_CONNECTION_NAME_WSZ) / sizeof(wchar_t) - 1; 2329 pwszBuf[offDst++] = ' '; 2330 memcpy(&pwszBuf[offDst], &pwszDevName[offSuffix], cwcSuffix * sizeof(wchar_t)); 2331 pwszBuf[offDst + cwcSuffix] = '\0'; 2332 } 2333 return S_OK; 2334 } 2335 return E_FAIL; 2373 2336 } 2374 2337 2375 2338 static BOOL vboxNetCfgWinAdjustHostOnlyNetworkInterfacePriority(IN INetCfg *pNc, IN INetCfgComponent *pNcc, PVOID pContext) 2376 2339 { 2340 GUID * const pGuid = (GUID*)pContext; 2377 2341 RT_NOREF1(pNc); 2378 INetCfgComponentBindings *pNetCfgBindings;2379 GUID *pGuid = (GUID*)pContext;2380 2342 2381 2343 /* Get component's binding. */ 2382 HRESULT hr = pNcc->QueryInterface(IID_INetCfgComponentBindings, (PVOID*)&pNetCfgBindings); 2344 INetCfgComponentBindings *pNetCfgBindings = NULL; 2345 HRESULT hr = pNcc->QueryInterface(IID_INetCfgComponentBindings, (PVOID *)&pNetCfgBindings); 2383 2346 if (SUCCEEDED(hr)) 2384 2347 { 2385 2348 /* Get binding path enumerator reference. */ 2386 IEnumNetCfgBindingPath *pEnumNetCfgBindPath ;2349 IEnumNetCfgBindingPath *pEnumNetCfgBindPath = NULL; 2387 2350 hr = pNetCfgBindings->EnumBindingPaths(EBP_BELOW, &pEnumNetCfgBindPath); 2388 2351 if (SUCCEEDED(hr)) 2389 2352 { 2390 bool bFoundIface = false;2353 bool fFoundIface = false; 2391 2354 hr = pEnumNetCfgBindPath->Reset(); 2392 2355 do 2393 2356 { 2394 INetCfgBindingPath *pNetCfgBindPath ;2357 INetCfgBindingPath *pNetCfgBindPath = NULL; 2395 2358 hr = pEnumNetCfgBindPath->Next(1, &pNetCfgBindPath, NULL); 2396 2359 if (hr == S_OK) … … 2422 2385 hr = pNetCfgBindings->MoveAfter(pNetCfgBindPath, NULL); 2423 2386 if (FAILED(hr)) 2424 NonStandardLogFlow(("Unable to move interface, hr (0x%x)\n", hr)); 2425 bFoundIface = true; 2387 NonStandardLogFlow(("Unable to move interface: %Rhrc\n", hr)); 2388 fFoundIface = true; 2389 2426 2390 /* 2427 2391 * Enable binding paths for host-only adapters bound to bridged filter … … 2429 2393 */ 2430 2394 HRESULT hr2; 2431 LPWSTR pwszHwId = NULL;2395 LPWSTR pwszHwId = NULL; 2432 2396 if ((hr2 = pNcc->GetId(&pwszHwId)) != S_OK) 2433 NonStandardLogFlow(("Failed to get HW ID, hr (0x%x)\n", hr2)); 2434 else if (_wcsnicmp(pwszHwId, VBOXNETCFGWIN_NETLWF_ID, 2435 sizeof(VBOXNETCFGWIN_NETLWF_ID)/2)) 2436 NonStandardLogFlow(("Ignoring component %ls\n", pwszHwId)); 2437 else if ((hr2 = pNetCfgBindPath->IsEnabled()) != S_FALSE) 2438 NonStandardLogFlow(("Already enabled binding path, hr (0x%x)\n", hr2)); 2439 else if ((hr2 = pNetCfgBindPath->Enable(TRUE)) != S_OK) 2440 NonStandardLogFlow(("Failed to enable binding path, hr (0x%x)\n", hr2)); 2397 NonStandardLogFlow(("Failed to get HW ID: %Rhrc\n", hr2)); 2441 2398 else 2442 NonStandardLogFlow(("Enabled binding path\n")); 2443 if (pwszHwId) 2399 { 2400 /** @todo r=bird: Original was: 2401 * _wcsnicmp(pwszHwId, VBOXNETCFGWIN_NETLWF_ID, sizeof(VBOXNETCFGWIN_NETLWF_ID)/2) 2402 * which is the same as _wcsicmp. Not sure if this was accidental, but it's not the 2403 * only one in this code area (VBoxNetFltNobj.cpp had some too IIRC). */ 2404 if (RTUtf16ICmp(pwszHwId, VBOXNETCFGWIN_NETLWF_ID) != 0) 2405 NonStandardLogFlow(("Ignoring component %ls\n", pwszHwId)); 2406 else if ((hr2 = pNetCfgBindPath->IsEnabled()) != S_FALSE) 2407 NonStandardLogFlow(("Already enabled binding path: %Rhrc\n", hr2)); 2408 else if ((hr2 = pNetCfgBindPath->Enable(TRUE)) != S_OK) 2409 NonStandardLogFlow(("Failed to enable binding path: %Rhrc\n", hr2)); 2410 else 2411 NonStandardLogFlow(("Enabled binding path\n")); 2444 2412 CoTaskMemFree(pwszHwId); 2413 } 2445 2414 } 2446 2415 } … … 2448 2417 } 2449 2418 else 2450 NonStandardLogFlow(("GetLowerComponent failed , hr (0x%x)\n", hr));2419 NonStandardLogFlow(("GetLowerComponent failed: %Rhrc\n", hr)); 2451 2420 pNetCfgBindIfce->Release(); 2452 2421 } … … 2456 2425 hr = S_OK; 2457 2426 else 2458 NonStandardLogFlow(("Next binding interface failed , hr (0x%x)\n", hr));2427 NonStandardLogFlow(("Next binding interface failed: %Rhrc\n", hr)); 2459 2428 break; 2460 2429 } 2461 } while (! bFoundIface);2430 } while (!fFoundIface); 2462 2431 pEnumNetCfgBindIface->Release(); 2463 2432 } 2464 2433 else 2465 NonStandardLogFlow(("EnumBindingInterfaces failed , hr (0x%x)\n", hr));2434 NonStandardLogFlow(("EnumBindingInterfaces failed: %Rhrc\n", hr)); 2466 2435 pNetCfgBindPath->Release(); 2467 2436 } … … 2471 2440 hr = S_OK; 2472 2441 else 2473 NonStandardLogFlow(("Next bind path failed , hr (0x%x)\n", hr));2442 NonStandardLogFlow(("Next bind path failed: %Rhrc\n", hr)); 2474 2443 break; 2475 2444 } 2476 } while (! bFoundIface);2445 } while (!fFoundIface); 2477 2446 pEnumNetCfgBindPath->Release(); 2478 2447 } 2479 2448 else 2480 NonStandardLogFlow(("EnumBindingPaths failed , hr (0x%x)\n", hr));2449 NonStandardLogFlow(("EnumBindingPaths failed: %Rhrc\n", hr)); 2481 2450 pNetCfgBindings->Release(); 2482 2451 } 2483 2452 else 2484 NonStandardLogFlow(("QueryInterface for IID_INetCfgComponentBindings failed , hr (0x%x)\n", hr));2453 NonStandardLogFlow(("QueryInterface for IID_INetCfgComponentBindings failed: %Rhrc\n", hr)); 2485 2454 return TRUE; 2486 2455 } 2487 2456 2488 static UINT WINAPI vboxNetCfgWinPspFileCallback( 2489 PVOID Context, 2490 UINT Notification, 2491 UINT_PTR Param1, 2492 UINT_PTR Param2 2493 ) 2457 /** Callback for SetupDiSetDeviceInstallParams used by 2458 * vboxNetCfgWinCreateHostOnlyNetworkInterface */ 2459 static UINT WINAPI vboxNetCfgWinPspFileCallback(PVOID Context, UINT Notification, UINT_PTR Param1, UINT_PTR Param2) 2494 2460 { 2495 2461 switch (Notification) … … 2499 2465 return TRUE; 2500 2466 } 2501 return SetupDefaultQueueCallback(Context, Notification, Param1, Param2); 2502 } 2503 2504 /* The original source of the VBoxNetAdp adapter creation/destruction code has the following copyright */ 2467 return SetupDefaultQueueCallbackW(Context, Notification, Param1, Param2); 2468 } 2469 2470 2471 2472 2473 /* The original source of the VBoxNetAdp adapter creation/destruction code has the following copyright: */ 2505 2474 /* 2506 2475 Copyright 2004 by the Massachusetts Institute of Technology … … 2530 2499 * Use the IShellFolder API to rename the connection. 2531 2500 */ 2532 static HRESULT rename_shellfolder (PCWSTR wGuid, PCWSTR wNewName) 2533 { 2501 static HRESULT rename_shellfolder(PCWSTR pwszGuid, PCWSTR pwszNewName) 2502 { 2503 /* Build the display name in the form "::{GUID}". Do this first in case it overflows. */ 2504 WCHAR wszAdapterGuid[MAX_PATH + 2] = {0}; 2505 ssize_t cwc = RTUtf16Printf(wszAdapterGuid, RT_ELEMENTS(wszAdapterGuid), "::%ls", pwszGuid); 2506 if (cwc < 0) 2507 return E_INVALIDARG; 2508 2534 2509 /* This is the GUID for the network connections folder. It is constant. 2535 2510 * {7007ACC7-3202-11D1-AAD2-00805FC1270E} */ … … 2540 2515 }; 2541 2516 2542 LPITEMIDLIST pidl = NULL;2517 /* Create an instance of the network connections folder. */ 2543 2518 IShellFolder *pShellFolder = NULL; 2544 HRESULT hr; 2545 2546 /* Build the display name in the form "::{GUID}". */ 2547 if (wcslen(wGuid) >= MAX_PATH) 2548 return E_INVALIDARG; 2549 WCHAR szAdapterGuid[MAX_PATH + 2] = {0}; 2550 swprintf(szAdapterGuid, L"::%ls", wGuid); 2551 2552 /* Create an instance of the network connections folder. */ 2553 hr = CoCreateInstance(MY_CLSID_NetworkConnections, NULL, 2554 CLSCTX_INPROC_SERVER, IID_IShellFolder, 2555 reinterpret_cast<LPVOID *>(&pShellFolder)); 2556 /* Parse the display name. */ 2557 if (SUCCEEDED (hr)) 2558 { 2559 hr = pShellFolder->ParseDisplayName (NULL, NULL, szAdapterGuid, NULL, 2560 &pidl, NULL); 2561 } 2562 if (SUCCEEDED (hr)) 2563 { 2564 hr = pShellFolder->SetNameOf (NULL, pidl, wNewName, SHGDN_NORMAL, 2565 &pidl); 2566 } 2567 2568 CoTaskMemFree (pidl); 2569 2570 if (pShellFolder) 2519 HRESULT hr = CoCreateInstance(MY_CLSID_NetworkConnections, NULL, CLSCTX_INPROC_SERVER, IID_IShellFolder, 2520 reinterpret_cast<LPVOID *>(&pShellFolder)); 2521 if (SUCCEEDED(hr)) 2522 { 2523 /* Parse the display name. */ 2524 LPITEMIDLIST pidl = NULL; 2525 hr = pShellFolder->ParseDisplayName(NULL, NULL, wszAdapterGuid, NULL, &pidl, NULL); 2526 if (SUCCEEDED(hr)) 2527 hr = pShellFolder->SetNameOf(NULL, pidl, pwszNewName, SHGDN_NORMAL, &pidl); 2528 CoTaskMemFree(pidl); 2571 2529 pShellFolder->Release(); 2572 2530 } 2573 2531 return hr; 2574 2532 } … … 2578 2536 * 2579 2537 * @returns Module handle or NULL 2580 * @param p szNameThe DLL name.2538 * @param pwszName The DLL name. 2581 2539 */ 2582 static HMODULE loadSystemDll(const char *pszName)2583 { 2584 charszPath[MAX_PATH];2585 UINT c chPath = GetSystemDirectoryA(szPath, sizeof(szPath));2586 size_t c bName = strlen(pszName) + 1;2587 if (c chPath + 1 + cbName > sizeof(szPath))2540 static HMODULE loadSystemDll(const wchar_t *pwszName) 2541 { 2542 WCHAR wszPath[MAX_PATH]; 2543 UINT cwcPath = GetSystemDirectoryW(wszPath, RT_ELEMENTS(wszPath)); 2544 size_t cwcName = RTUtf16Len(pwszName) + 1; 2545 if (cwcPath + 1 + cwcName > RT_ELEMENTS(wszPath)) 2588 2546 return NULL; 2589 szPath[cchPath] = '\\'; 2590 memcpy(&szPath[cchPath + 1], pszName, cbName); 2591 return LoadLibraryA(szPath); 2592 } 2593 2594 static bool vboxNetCfgWinDetectStaleConnection(PCWSTR pName) 2595 { 2596 HKEY hkeyConnection, hkeyAdapter, hkeyAdapters; 2597 WCHAR wszAdaptersKeyName[] = L"SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"; 2598 WCHAR wszAdapterSubKeyName[MAX_PATH]; 2599 LSTATUS status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, wszAdaptersKeyName, 0, KEY_ALL_ACCESS, &hkeyAdapters); 2600 if (status != ERROR_SUCCESS) 2547 2548 wszPath[cwcPath++] = '\\'; 2549 memcpy(&wszPath[cwcPath], pwszName, cwcName * sizeof(wszPath[0])); 2550 return LoadLibraryW(wszPath); 2551 } 2552 2553 static bool vboxNetCfgWinDetectStaleConnection(PCWSTR pwszName) 2554 { 2555 HKEY hKeyAdapters = NULL; 2556 LSTATUS lrc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, 2557 L"SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 2558 0 /*ulOptions*/, KEY_ALL_ACCESS, &hKeyAdapters); 2559 if (lrc != ERROR_SUCCESS) 2601 2560 return false; 2602 2561 … … 2604 2563 for (DWORD i = 0; !fFailureImminent; ++i) 2605 2564 { 2606 DWORD cbName = MAX_PATH; 2607 status = RegEnumKeyEx(hkeyAdapters, i, wszAdapterSubKeyName, &cbName, NULL, NULL, NULL, NULL); 2608 // if (status == ERROR_NO_MORE_ITEMS) 2609 // break; 2610 if (status != ERROR_SUCCESS) 2565 WCHAR wszAdapterSubKeyName[MAX_PATH]; 2566 DWORD cwcAdapterSubKeyName = MAX_PATH; 2567 lrc = RegEnumKeyEx(hKeyAdapters, i, wszAdapterSubKeyName, &cwcAdapterSubKeyName, NULL, NULL, NULL, NULL); 2568 if (lrc != ERROR_SUCCESS) 2611 2569 break; 2612 2570 2613 status = RegOpenKeyEx(hkeyAdapters, wszAdapterSubKeyName, 0, KEY_ALL_ACCESS, &hkeyAdapter); 2614 if (status == ERROR_SUCCESS) 2615 { 2616 status = RegOpenKeyEx(hkeyAdapter, L"Connection", 0, KEY_ALL_ACCESS, &hkeyConnection); 2617 if (status == ERROR_SUCCESS) 2618 { 2619 WCHAR wszName[MAX_PATH]; 2620 cbName = MAX_PATH; 2621 status = RegQueryValueEx(hkeyConnection, L"Name", NULL, NULL, (LPBYTE)wszName, &cbName); 2622 if (status == ERROR_SUCCESS) 2623 if (wcsicmp(wszName, pName) == 0) 2571 HKEY hKeyAdapter = NULL; 2572 lrc = RegOpenKeyEx(hKeyAdapters, wszAdapterSubKeyName, 0, KEY_ALL_ACCESS, &hKeyAdapter); 2573 if (lrc == ERROR_SUCCESS) 2574 { 2575 HKEY hKeyConnection = NULL; 2576 lrc = RegOpenKeyEx(hKeyAdapter, L"Connection", 0, KEY_ALL_ACCESS, &hKeyConnection); 2577 if (lrc == ERROR_SUCCESS) 2578 { 2579 WCHAR wszCurName[MAX_PATH + 1]; 2580 DWORD cbCurName = sizeof(wszCurName) - sizeof(WCHAR); 2581 DWORD dwType = REG_SZ; 2582 lrc = RegQueryValueEx(hKeyConnection, L"Name", NULL, NULL, (LPBYTE)wszCurName, &cbCurName); 2583 if ( lrc == ERROR_SUCCESS 2584 /** @todo r=bird: The original code didn't do any value type checks, thus allowing all SZ types. */ 2585 && (dwType == REG_SZ || dwType == REG_EXPAND_SZ || dwType == REG_MULTI_SZ)) 2586 { 2587 wszCurName[MAX_PATH] = '\0'; /* returned values doesn't necessarily need to be terminated */ 2588 2589 if (RTUtf16ICmp(pwszName, pwszName) == 0) 2624 2590 fFailureImminent = true; 2625 RegCloseKey(hkeyConnection); 2626 } 2627 RegCloseKey(hkeyAdapter); 2628 } 2629 } 2630 RegCloseKey(hkeyAdapters); 2591 } 2592 RegCloseKey(hKeyConnection); 2593 } 2594 RegCloseKey(hKeyAdapter); 2595 } 2596 } 2597 RegCloseKey(hKeyAdapters); 2631 2598 2632 2599 return fFailureImminent; 2633 2600 } 2634 2601 2635 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinRenameConnection (LPWSTR pGuid, PCWSTR NewName) 2636 { 2637 typedef HRESULT (WINAPI *lpHrRenameConnection) (const GUID *, PCWSTR); 2638 lpHrRenameConnection RenameConnectionFunc = NULL; 2639 HRESULT status; 2640 2602 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinRenameConnection(LPWSTR pwszGuid, PCWSTR NewName) 2603 { 2641 2604 /* 2642 2605 * Before attempting to rename the connection, check if there is a stale … … 2646 2609 if (vboxNetCfgWinDetectStaleConnection(NewName)) 2647 2610 return S_OK; 2611 2648 2612 /* First try the IShellFolder interface, which was unimplemented 2649 2613 * for the network connections folder before XP. */ 2650 status = rename_shellfolder (pGuid, NewName);2651 if ( status== E_NOTIMPL)2614 HRESULT hrc = rename_shellfolder(pwszGuid, NewName); 2615 if (hrc == E_NOTIMPL) 2652 2616 { 2653 2617 /** @todo that code doesn't seem to work! */ … … 2655 2619 * Try the (undocumented) HrRenameConnection API in the netshell 2656 2620 * library. */ 2657 CLSID clsid; 2658 HINSTANCE hNetShell; 2659 status = CLSIDFromString ((LPOLESTR) pGuid, &clsid); 2660 if (FAILED(status)) 2621 CLSID clsid; 2622 hrc = CLSIDFromString((LPOLESTR)pwszGuid, &clsid); 2623 if (FAILED(hrc)) 2661 2624 return E_FAIL; 2662 hNetShell = loadSystemDll("netshell.dll"); 2625 2626 HINSTANCE hNetShell = loadSystemDll(L"netshell.dll"); 2663 2627 if (hNetShell == NULL) 2664 2628 return E_FAIL; 2665 RenameConnectionFunc = 2666 (lpHrRenameConnection) GetProcAddress (hNetShell, 2667 "HrRenameConnection"); 2668 if (RenameConnectionFunc == NULL) 2669 { 2670 FreeLibrary (hNetShell); 2671 return E_FAIL; 2672 } 2673 status = RenameConnectionFunc (&clsid, NewName); 2674 FreeLibrary (hNetShell); 2675 } 2676 if (FAILED (status)) 2677 return status; 2678 2629 2630 typedef HRESULT (WINAPI *PFNHRRENAMECONNECTION)(const GUID *, PCWSTR); 2631 PFNHRRENAMECONNECTION pfnRenameConnection = (PFNHRRENAMECONNECTION)GetProcAddress(hNetShell, "HrRenameConnection"); 2632 if (pfnRenameConnection != NULL) 2633 hrc = pfnRenameConnection(&clsid, NewName); 2634 else 2635 hrc = E_FAIL; 2636 2637 FreeLibrary(hNetShell); 2638 } 2639 if (FAILED(hrc)) 2640 return hrc; 2679 2641 return S_OK; 2680 2642 } 2681 2643 2682 #define DRIVERHWID _T("sun_VBoxNetAdp") 2683 2684 #define SetErrBreak(strAndArgs) \ 2685 if (1) { \ 2686 hrc = E_FAIL; \ 2687 NonStandardLog strAndArgs; \ 2688 bstrError = bstr_printf strAndArgs; \ 2689 break; \ 2690 } else do {} while (0) 2691 2692 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinRemoveHostOnlyNetworkInterface(IN const GUID *pGUID, OUT BSTR *pErrMsg) 2644 2645 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinRemoveHostOnlyNetworkInterface(IN const GUID *pGUID, OUT BSTR *pBstrErrMsg) 2693 2646 { 2694 2647 HRESULT hrc = S_OK; 2695 bstr_t bstrError; 2696 2697 do 2698 { 2699 TCHAR lszPnPInstanceId [512] = {0}; 2700 2701 /* We have to find the device instance ID through a registry search */ 2702 2703 HKEY hkeyNetwork = 0; 2704 HKEY hkeyConnection = 0; 2705 2706 do 2707 { 2708 WCHAR strRegLocation [256]; 2648 com::Bstr bstrError; 2649 2650 do /* break non-loop */ 2651 { 2652 WCHAR wszPnPInstanceId[512] = {0}; 2653 2654 /* 2655 * We have to find the device instance ID through a registry search 2656 */ 2657 HKEY hkeyNetwork = NULL; 2658 HKEY hkeyConnection = NULL; 2659 do /* another non-loop for breaking out of */ 2660 { 2709 2661 WCHAR wszGuid[50]; 2710 2711 int length = StringFromGUID2(*pGUID, wszGuid, RT_ELEMENTS(wszGuid)); 2712 if (!length) 2662 int cwcGuid = StringFromGUID2(*pGUID, wszGuid, RT_ELEMENTS(wszGuid)); 2663 if (!cwcGuid) 2713 2664 SetErrBreak(("Failed to create a Guid string")); 2714 2665 2715 swprintf (strRegLocation, 2716 L"SYSTEM\\CurrentControlSet\\Control\\Network\\" 2717 L"{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s", 2718 wszGuid); 2719 2720 LONG status; 2721 status = RegOpenKeyExW (HKEY_LOCAL_MACHINE, strRegLocation, 0, 2722 KEY_READ, &hkeyNetwork); 2723 if ((status != ERROR_SUCCESS) || !hkeyNetwork) 2724 SetErrBreak (("Host interface network is not found in registry (%S) [1]", 2725 strRegLocation)); 2726 2727 status = RegOpenKeyExW (hkeyNetwork, L"Connection", 0, 2728 KEY_READ, &hkeyConnection); 2729 if ((status != ERROR_SUCCESS) || !hkeyConnection) 2730 SetErrBreak (("Host interface network is not found in registry (%S) [2]", 2731 strRegLocation)); 2732 2733 DWORD len = sizeof (lszPnPInstanceId); 2734 DWORD dwKeyType; 2735 status = RegQueryValueExW (hkeyConnection, L"PnPInstanceID", NULL, 2736 &dwKeyType, (LPBYTE) lszPnPInstanceId, &len); 2737 if ((status != ERROR_SUCCESS) || (dwKeyType != REG_SZ)) 2738 SetErrBreak (("Host interface network is not found in registry (%S) [3]", 2739 strRegLocation)); 2740 } 2741 while (0); 2666 WCHAR wszRegLocation[128 + RT_ELEMENTS(wszGuid)]; 2667 RTUtf16Printf(wszRegLocation, RT_ELEMENTS(wszRegLocation), 2668 "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\%ls", wszGuid); 2669 2670 LSTATUS lrc = RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRegLocation, 0, KEY_READ, &hkeyNetwork); 2671 if (lrc != ERROR_SUCCESS || !hkeyNetwork) 2672 SetErrBreak(("Host interface network is not found in registry (%S): lrc=%u [1]", wszRegLocation, lrc)); 2673 2674 lrc = RegOpenKeyExW(hkeyNetwork, L"Connection", 0, KEY_READ, &hkeyConnection); 2675 if (lrc != ERROR_SUCCESS || !hkeyConnection) 2676 SetErrBreak(("Host interface network is not found in registry (%S): lrc=%u [2]", wszRegLocation, lrc)); 2677 2678 DWORD cbValue = sizeof(wszPnPInstanceId) - sizeof(WCHAR); 2679 DWORD dwType = ~0U; 2680 lrc = RegQueryValueExW(hkeyConnection, L"PnPInstanceID", NULL, &dwType, (LPBYTE)wszPnPInstanceId, &cbValue); 2681 if (lrc != ERROR_SUCCESS || dwType != REG_SZ) 2682 SetErrBreak(("Host interface network is not found in registry (%S): lrc=%u, dwType=%u [3]", 2683 wszRegLocation, lrc, dwType)); 2684 } while (0); 2742 2685 2743 2686 if (hkeyConnection) 2744 RegCloseKey 2687 RegCloseKey(hkeyConnection); 2745 2688 if (hkeyNetwork) 2746 RegCloseKey (hkeyNetwork); 2747 2748 if (FAILED (hrc)) 2689 RegCloseKey(hkeyNetwork); 2690 if (FAILED(hrc)) 2749 2691 break; 2750 2692 … … 2753 2695 * wait until we encounter the right device instance ID 2754 2696 */ 2755 2756 2697 HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE; 2757 2758 do 2759 { 2760 BOOL ok; 2698 do /* break-only, not-a-loop */ 2699 { 2700 BOOL ok; 2701 2702 /* initialize the structure size */ 2703 SP_DEVINFO_DATA DeviceInfoData = { sizeof(DeviceInfoData) }; 2704 2705 /* copy the net class GUID */ 2761 2706 GUID netGuid; 2762 SP_DEVINFO_DATA DeviceInfoData; 2763 DWORD index = 0; 2764 BOOL found = FALSE; 2765 DWORD size = 0; 2766 2767 /* initialize the structure size */ 2768 DeviceInfoData.cbSize = sizeof (SP_DEVINFO_DATA); 2769 2770 /* copy the net class GUID */ 2771 memcpy(&netGuid, &GUID_DEVCLASS_NET, sizeof (GUID_DEVCLASS_NET)); 2707 memcpy(&netGuid, &GUID_DEVCLASS_NET, sizeof(GUID_DEVCLASS_NET)); 2772 2708 2773 2709 /* return a device info set contains all installed devices of the Net class */ 2774 2710 hDeviceInfo = SetupDiGetClassDevs(&netGuid, NULL, NULL, DIGCF_PRESENT); 2775 2776 2711 if (hDeviceInfo == INVALID_HANDLE_VALUE) 2777 2712 SetErrBreak(("SetupDiGetClassDevs failed (0x%08X)", GetLastError())); 2778 2713 2779 /* enumerate the driver info list */ 2780 while (TRUE) 2781 { 2782 TCHAR *deviceHwid; 2783 2784 ok = SetupDiEnumDeviceInfo(hDeviceInfo, index, &DeviceInfoData); 2785 2786 if (!ok) 2714 /* Enumerate the driver info list. */ 2715 bool fFound = false; 2716 for (DWORD index = 0; !fFound; index++) 2717 { 2718 if (!SetupDiEnumDeviceInfo(hDeviceInfo, index, &DeviceInfoData)) 2787 2719 { 2788 2720 if (GetLastError() == ERROR_NO_MORE_ITEMS) 2789 2721 break; 2790 else 2791 { 2792 index++; 2793 continue; 2794 } 2722 continue; 2795 2723 } 2796 2724 2797 2725 /* try to get the hardware ID registry property */ 2798 ok = SetupDiGetDeviceRegistryProperty(hDeviceInfo, 2726 DWORD cbValue = 0; 2727 if (SetupDiGetDeviceRegistryPropertyW(hDeviceInfo, 2799 2728 &DeviceInfoData, 2800 2729 SPDRP_HARDWAREID, … … 2802 2731 NULL, 2803 2732 0, 2804 &size); 2805 if (!ok) 2806 { 2807 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) 2808 { 2809 index++; 2810 continue; 2811 } 2812 2813 deviceHwid = (TCHAR *) malloc(size); 2814 ok = SetupDiGetDeviceRegistryProperty(hDeviceInfo, 2815 &DeviceInfoData, 2816 SPDRP_HARDWAREID, 2817 NULL, 2818 (PBYTE)deviceHwid, 2819 size, 2820 NULL); 2821 if (!ok) 2822 { 2823 free(deviceHwid); 2824 deviceHwid = NULL; 2825 index++; 2826 continue; 2827 } 2828 } 2829 else 2830 { 2831 /* something is wrong. This shouldn't have worked with a NULL buffer */ 2832 index++; 2733 &cbValue)) 2734 continue; /* Something is wrong. This shouldn't have worked with a NULL buffer! */ 2735 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) 2833 2736 continue; 2834 } 2835 2836 for (TCHAR *t = deviceHwid; 2837 t && *t && t < &deviceHwid[size / sizeof(TCHAR)]; 2838 t += _tcslen(t) + 1) 2839 { 2840 if (!_tcsicmp(DRIVERHWID, t)) 2841 { 2842 /* get the device instance ID */ 2843 TCHAR devId[MAX_DEVICE_ID_LEN]; 2844 if (CM_Get_Device_ID(DeviceInfoData.DevInst, 2845 devId, MAX_DEVICE_ID_LEN, 0) == CR_SUCCESS) 2846 { 2847 /* compare to what we determined before */ 2848 if (wcscmp(devId, lszPnPInstanceId) == 0) 2849 { 2850 found = TRUE; 2851 break; 2852 } 2853 } 2854 } 2855 } 2856 2857 if (deviceHwid) 2858 { 2859 free (deviceHwid); 2860 deviceHwid = NULL; 2861 } 2862 2863 if (found) 2737 2738 WCHAR *pwszzDeviceHwId = (WCHAR *)RTMemAllocZ(cbValue + sizeof(WCHAR) * 2); 2739 if (!pwszzDeviceHwId) 2864 2740 break; 2865 2866 index++; 2867 } 2868 2869 if (found == FALSE) 2870 SetErrBreak (("Host Interface Network driver not found (0x%08X)", 2871 GetLastError())); 2872 2873 ok = SetupDiSetSelectedDevice (hDeviceInfo, &DeviceInfoData); 2741 if (SetupDiGetDeviceRegistryPropertyW(hDeviceInfo, 2742 &DeviceInfoData, 2743 SPDRP_HARDWAREID, 2744 NULL, 2745 (PBYTE)pwszzDeviceHwId, 2746 cbValue, 2747 &cbValue)) 2748 { 2749 /* search the string list. */ 2750 for (WCHAR *pwszCurHwId = pwszzDeviceHwId; 2751 (uintptr_t)pwszCurHwId - (uintptr_t)pwszzDeviceHwId < cbValue && *pwszCurHwId != L'\0'; 2752 pwszCurHwId += RTUtf16Len(pwszCurHwId) + 1) 2753 if (RTUtf16ICmp(DRIVERHWID, pwszCurHwId) == 0) 2754 { 2755 /* get the device instance ID */ 2756 WCHAR wszDevId[MAX_DEVICE_ID_LEN]; 2757 if (CM_Get_Device_IDW(DeviceInfoData.DevInst, wszDevId, MAX_DEVICE_ID_LEN, 0) == CR_SUCCESS) 2758 { 2759 /* compare to what we determined before */ 2760 if (RTUtf16Cmp(wszDevId, wszPnPInstanceId) == 0) 2761 { 2762 fFound = true; 2763 break; 2764 } 2765 } 2766 } 2767 } 2768 RTMemFree(pwszzDeviceHwId); 2769 } 2770 2771 if (!fFound) 2772 SetErrBreak(("Host Interface Network driver not found (0x%08X)", GetLastError())); 2773 2774 ok = SetupDiSetSelectedDevice(hDeviceInfo, &DeviceInfoData); 2874 2775 if (!ok) 2875 SetErrBreak (("SetupDiSetSelectedDevice failed (0x%08X)", 2876 GetLastError())); 2877 2878 ok = SetupDiCallClassInstaller (DIF_REMOVE, hDeviceInfo, &DeviceInfoData); 2776 SetErrBreak(("SetupDiSetSelectedDevice failed (0x%08X)", GetLastError())); 2777 2778 ok = SetupDiCallClassInstaller(DIF_REMOVE, hDeviceInfo, &DeviceInfoData); 2879 2779 if (!ok) 2880 SetErrBreak (("SetupDiCallClassInstaller (DIF_REMOVE) failed (0x%08X)", 2881 GetLastError())); 2882 } 2883 while (0); 2780 SetErrBreak(("SetupDiCallClassInstaller (DIF_REMOVE) failed (0x%08X)", GetLastError())); 2781 } while (0); 2884 2782 2885 2783 /* clean up the device info set */ 2886 2784 if (hDeviceInfo != INVALID_HANDLE_VALUE) 2887 2785 SetupDiDestroyDeviceInfoList (hDeviceInfo); 2888 2889 if (FAILED (hrc)) 2890 break; 2891 } 2892 while (0); 2893 2894 if (pErrMsg && bstrError.length()) 2895 *pErrMsg = bstrError.Detach(); 2896 2786 } while (0); 2787 2788 if (pBstrErrMsg) 2789 { 2790 *pBstrErrMsg = NULL; 2791 if (bstrError.isNotEmpty()) 2792 bstrError.detachToEx(pBstrErrMsg); 2793 } 2897 2794 return hrc; 2898 2795 } 2899 2796 2900 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinUpdateHostOnlyNetworkInterface(LPCWSTR pcsxwInf, BOOL *p bRebootRequired, LPCWSTR pcsxwId)2901 { 2902 return VBoxDrvCfgDrvUpdate(pcsxwId, pcsxwInf, p bRebootRequired);2797 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinUpdateHostOnlyNetworkInterface(LPCWSTR pcsxwInf, BOOL *pfRebootRequired, LPCWSTR pcsxwId) 2798 { 2799 return VBoxDrvCfgDrvUpdate(pcsxwId, pcsxwInf, pfRebootRequired); 2903 2800 } 2904 2801 … … 2907 2804 switch (dwState) 2908 2805 { 2909 case SERVICE_STOPPED: return "is not running";2910 case SERVICE_STOP_PENDING: return "is stopping";2911 case SERVICE_CONTINUE_PENDING: return "continue is pending";2912 case SERVICE_PAUSE_PENDING: return "pause is pending";2913 case SERVICE_PAUSED: return "is paused";2914 case SERVICE_RUNNING: return "is running";2915 case SERVICE_START_PENDING: return "is starting";2806 case SERVICE_STOPPED: return "is not running"; 2807 case SERVICE_STOP_PENDING: return "is stopping"; 2808 case SERVICE_CONTINUE_PENDING: return "continue is pending"; 2809 case SERVICE_PAUSE_PENDING: return "pause is pending"; 2810 case SERVICE_PAUSED: return "is paused"; 2811 case SERVICE_RUNNING: return "is running"; 2812 case SERVICE_START_PENDING: return "is starting"; 2916 2813 } 2917 2814 return "state is invalid"; … … 2941 2838 } 2942 2839 2943 typedef struct { 2840 typedef struct 2841 { 2944 2842 BSTR bstrName; 2945 2843 GUID *pGuid; … … 2963 2861 } 2964 2862 2965 /* We assume the following name matches the device description in vboxnetadp6.inf */ 2966 #define HOSTONLY_ADAPTER_NAME "VirtualBox Host-Only Ethernet Adapter" 2967 2968 /* 2863 /** 2969 2864 * Enumerate all host-only adapters collecting their names into a set, then 2970 2865 * come up with the next available name by taking the first unoccupied index. 2971 2866 */ 2972 static HRESULT vboxNetCfgWinNextAvailableDevName(bstr_t& bstrName) 2973 { 2974 SP_DEVINFO_DATA DeviceInfoData; 2975 /* initialize the structure size */ 2976 DeviceInfoData.cbSize = sizeof (SP_DEVINFO_DATA); 2977 2978 HDEVINFO DeviceInfoSet = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT); 2979 if (DeviceInfoSet == INVALID_HANDLE_VALUE) 2980 return GetLastError(); 2981 2982 DWORD i; 2983 std::set<bstr_t> aExistingNames; 2984 for (i = 0; SetupDiEnumDeviceInfo(DeviceInfoSet, i, &DeviceInfoData); ++i) 2867 static HRESULT vboxNetCfgWinNextAvailableDevName(com::Bstr *pbstrName) 2868 { 2869 SP_DEVINFO_DATA DeviceInfoData = { sizeof(SP_DEVINFO_DATA) }; 2870 HDEVINFO hDeviceInfoSet = SetupDiGetClassDevsW(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT); 2871 if (hDeviceInfoSet == INVALID_HANDLE_VALUE) 2872 return HRESULT_FROM_WIN32(GetLastError()); 2873 2874 typedef struct VBOXDEVNAMEENTRY 2875 { 2876 RTLISTNODE ListEntry; 2877 WCHAR wszDevName[64]; 2878 WCHAR wcZeroParanoia; 2879 } VBOXDEVNAMEENTRY; 2880 2881 #if 0 2882 /* 2883 * Build a list of names starting with HOSTONLY_ADAPTER_NAME_WSZ belonging to our device. 2884 */ 2885 RTLISTANCHOR Head; /* VBOXDEVNAMEENTRY */ 2886 RTListInit(&Head); 2887 HRESULT hrc = S_OK; 2888 #else 2889 /* 2890 * Build a bitmap of in-use index values of devices starting with HOSTONLY_ADAPTER_NAME_WSZ. 2891 * Reserving 0 for one w/o a suffix, and marking 1 as unusable. 2892 */ 2893 uint64_t bmIndexes[_32K / 64]; /* 4KB - 32767 device should be sufficient. */ 2894 RT_ZERO(bmIndexes); 2895 ASMBitSet(&bmIndexes, 1); 2896 #endif 2897 for (DWORD i = 0; SetupDiEnumDeviceInfo(hDeviceInfoSet, i, &DeviceInfoData); ++i) 2985 2898 { 2986 2899 /* Should be more than enough for both our device id and our device name, we do not care about the rest */ 2987 WCHAR wszDevName[64];2988 if (!SetupDiGetDeviceRegistryProperty (DeviceInfoSet, &DeviceInfoData, SPDRP_HARDWAREID,2989 NULL, (PBYTE)wszDevName, sizeof(wszDevName), NULL))2900 VBOXDEVNAMEENTRY Entry = { { 0, 0 }, L"", 0 }; /* (initialize it to avoid the wrath of asan) */ 2901 if (!SetupDiGetDeviceRegistryPropertyW(hDeviceInfoSet, &DeviceInfoData, SPDRP_HARDWAREID, 2902 NULL, (PBYTE)Entry.wszDevName, sizeof(Entry.wszDevName), NULL)) 2990 2903 continue; 2904 2991 2905 /* Ignore everything except our host-only adapters */ 2992 if (_wcsicmp(wszDevName, DRIVERHWID)) 2993 continue; 2994 if ( SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_FRIENDLYNAME, 2995 NULL, (PBYTE)wszDevName, sizeof(wszDevName), NULL) 2996 || SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_DEVICEDESC, 2997 NULL, (PBYTE)wszDevName, sizeof(wszDevName), NULL)) 2998 aExistingNames.insert(bstr_t(wszDevName)); 2999 } 3000 /* Try the name without index first */ 3001 bstrName = HOSTONLY_ADAPTER_NAME; 3002 if (aExistingNames.find(bstrName) != aExistingNames.end()) 3003 { 3004 WCHAR wszSuffix[16]; 3005 /* Try indexed names until we find unused one */ 3006 for (i = 2;; ++i) 3007 { 3008 wsprintf(wszSuffix, L" #%u", i); 3009 if (aExistingNames.find(bstrName + wszSuffix) == aExistingNames.end()) 3010 { 3011 bstrName += wszSuffix; 2906 if (RTUtf16ICmp(Entry.wszDevName, DRIVERHWID) == 0) 2907 { 2908 if ( SetupDiGetDeviceRegistryPropertyW(hDeviceInfoSet, &DeviceInfoData, SPDRP_FRIENDLYNAME, 2909 NULL, (PBYTE)Entry.wszDevName, sizeof(Entry.wszDevName), NULL) 2910 || SetupDiGetDeviceRegistryPropertyW(hDeviceInfoSet, &DeviceInfoData, SPDRP_DEVICEDESC, 2911 NULL, (PBYTE)Entry.wszDevName, sizeof(Entry.wszDevName), NULL)) 2912 { 2913 /* We can ignore any host-only adapter with a non-standard name. */ 2914 if (RTUtf16NICmp(Entry.wszDevName, HOSTONLY_ADAPTER_NAME_WSZ, 2915 RT_ELEMENTS(HOSTONLY_ADAPTER_NAME_WSZ) - 1) == 0) 2916 { 2917 #if 0 2918 VBOXDEVNAMEENTRY *pEntry = (VBOXDEVNAMEENTRY *)RTMemDup(&Entry, sizeof(Entry)); 2919 if (pEntry) 2920 RTListAppend(&Head, &pEntry->ListEntry); 2921 else 2922 { 2923 hrc = E_OUTOFMEMORY; 2924 break; 2925 } 2926 #else 2927 WCHAR const *pwc = &Entry.wszDevName[RT_ELEMENTS(HOSTONLY_ADAPTER_NAME_WSZ) - 1]; 2928 2929 /* skip leading space */ 2930 WCHAR wc = *pwc; 2931 while (wc == L' ' || wc == L'\t' || wc == L'\n' || wc == L'\r') 2932 wc = *++pwc; 2933 2934 /* If end of string, use index 0. */ 2935 if (wc == L'\0') 2936 ASMBitSet(bmIndexes, 0); 2937 2938 /* Hash and digit? */ 2939 else if (wc == L'#') 2940 { 2941 wc = *++pwc; 2942 while (wc == L' ' || wc == L'\t' || wc == L'\n' || wc == L'\r') /* just in case */ 2943 wc = *++pwc; 2944 if (wc >= L'0' && wc <= L'9') 2945 { 2946 /* Convert what we can to a number and mark it as allocated in the bitmap. */ 2947 uint64_t uIndex = wc - L'0'; 2948 while ((wc = *++pwc) >= L'0' && wc <= L'9') 2949 uIndex = uIndex * 10 + wc - L'0'; 2950 if (uIndex < sizeof(bmIndexes) * 8 && uIndex > 0) 2951 ASMBitSet(bmIndexes, (int32_t)uIndex); 2952 } 2953 } 2954 #endif 2955 } 2956 } 2957 } 2958 } 2959 #if 0 2960 if (SUCCEEDED(hrc)) 2961 { 2962 /* 2963 * First try a name w/o an index, then try index #2 and up. 2964 * 2965 * Note! We have to use ASCII/UTF-8 strings here as Bstr will confuse WCHAR 2966 * with BSTR/OLECHAR strings and use SysAllocString to duplicate it . 2967 */ 2968 char szName[sizeof(HOSTONLY_ADAPTER_NAME_SZ " #4294967296") + 32] = HOSTONLY_ADAPTER_NAME_SZ; 2969 size_t const cchBase = sizeof(HOSTONLY_ADAPTER_NAME_SZ) - 1; 2970 for (DWORD idx = 2;; idx++) 2971 { 2972 bool fFound = false; 2973 VBOXDEVNAMEENTRY *pCur; 2974 RTListForEach(&Head, pCur, VBOXDEVNAMEENTRY, ListEntry) 2975 { 2976 fFound = RTUtf16ICmpAscii(pCur->wszDevName, szName) == 0; 2977 if (fFound) 2978 { 2979 hrc = pbstrName->assignEx(szName); 2980 break; 2981 } 2982 } 2983 if (fFound) 3012 2984 break; 3013 } 3014 } 3015 } 3016 3017 if (DeviceInfoSet) 3018 SetupDiDestroyDeviceInfoList(DeviceInfoSet); 3019 return S_OK; 3020 } 3021 3022 static HRESULT vboxNetCfgWinCreateHostOnlyNetworkInterface(IN LPCWSTR pInfPath, IN bool bIsInfPathFile, IN BSTR bstrDesiredName, 3023 OUT GUID *pGuid, OUT BSTR *lppszName, OUT BSTR *pErrMsg) 3024 { 3025 HRESULT hrc = S_OK; 3026 3027 HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE; 3028 SP_DEVINFO_DATA DeviceInfoData; 3029 PVOID pQueueCallbackContext = NULL; 3030 DWORD ret = 0; 3031 BOOL registered = FALSE; 3032 BOOL destroyList = FALSE; 3033 WCHAR pWCfgGuidString [50]; 3034 WCHAR DevName[256]; 3035 HKEY hkey = (HKEY)INVALID_HANDLE_VALUE; 3036 bstr_t bstrError; 3037 bstr_t bstrNewInterfaceName; 3038 3039 if (SysStringLen(bstrDesiredName) != 0) 3040 bstrNewInterfaceName = bstrDesiredName; 2985 RTStrPrintf(&szName[cchBase], sizeof(szName) - cchBase, " #%u", idx); 2986 } 2987 } 2988 2989 VBOXDEVNAMEENTRY *pFirst; 2990 while ((pFirst = RTListRemoveFirst(&Head, VBOXDEVNAMEENTRY, ListEntry)) != NULL) 2991 RTMemFree(pFirst); 2992 2993 #else 2994 /* 2995 * Find an unused index value and format the corresponding name. 2996 */ 2997 HRESULT hrc; 2998 int32_t iBit = ASMBitFirstClear(bmIndexes, sizeof(bmIndexes) * 8); 2999 if (iBit >= 0) 3000 { 3001 if (iBit == 0) 3002 hrc = pbstrName->assignEx(HOSTONLY_ADAPTER_NAME_SZ); /* Not _WSZ! */ 3003 else 3004 hrc = pbstrName->printfNoThrow(HOSTONLY_ADAPTER_NAME_SZ " #%u", iBit); 3005 } 3041 3006 else 3042 3007 { 3043 hrc = vboxNetCfgWinNextAvailableDevName(bstrNewInterfaceName); 3008 NonStandardLogFlow(("vboxNetCfgWinNextAvailableDevName: no unused index in the first 32K!\n")); 3009 hrc = E_FAIL; 3010 } 3011 #endif 3012 3013 if (hDeviceInfoSet) 3014 SetupDiDestroyDeviceInfoList(hDeviceInfoSet); 3015 return hrc; 3016 } 3017 3018 static HRESULT vboxNetCfgWinCreateHostOnlyNetworkInterface(IN LPCWSTR pwszInfPath, IN bool fIsInfPathFile, 3019 IN BSTR pBstrDesiredName, 3020 OUT GUID *pGuid, OUT BSTR *pBstrName, OUT BSTR *pBstrErrMsg) 3021 { 3022 com::Bstr bstrError; 3023 3024 /* Determine the interface name. We make a copy of the input here for 3025 renaming reasons, see futher down. */ 3026 com::Bstr bstrNewInterfaceName; 3027 HRESULT hrc; 3028 if (SysStringLen(pBstrDesiredName) != 0) 3029 hrc = bstrNewInterfaceName.assignEx(pBstrDesiredName); 3030 else 3031 { 3032 hrc = vboxNetCfgWinNextAvailableDevName(&bstrNewInterfaceName); 3044 3033 if (FAILED(hrc)) 3045 3034 NonStandardLogFlow(("vboxNetCfgWinNextAvailableDevName failed with 0x%x\n", hrc)); 3046 3035 } 3047 3048 do 3049 { 3050 BOOL found = FALSE; 3036 if (FAILED(hrc)) 3037 return hrc; 3038 3039 WCHAR wszCfgGuidString[50] = {0}; 3040 WCHAR wszDevName[256 + 1] = {0}; 3041 SP_DEVINFO_DATA DeviceInfoData = { sizeof(DeviceInfoData) }; 3042 HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE; 3043 PVOID pQueueCallbackContext = NULL; 3044 BOOL fRegistered = FALSE; 3045 BOOL destroyList = FALSE; 3046 HKEY hkey = (HKEY)INVALID_HANDLE_VALUE; 3047 LSTATUS lrcRet = ERROR_SUCCESS; /* the */ 3048 3049 do /* non-loop, for breaking. */ 3050 { 3051 /* copy the net class GUID */ 3051 3052 GUID netGuid; 3052 SP_DRVINFO_DATA DriverInfoData;3053 SP_DEVINSTALL_PARAMS DeviceInstallParams;3054 TCHAR className [MAX_PATH];3055 DWORD index = 0;3056 PSP_DRVINFO_DETAIL_DATA pDriverInfoDetail;3057 /* for our purposes, 2k buffer is more3058 * than enough to obtain the hardware ID3059 * of the VBoxNetAdp driver. */3060 DWORD detailBuf [2048];3061 3062 DWORD cbSize;3063 DWORD dwValueType;3064 3065 /* initialize the structure size */3066 DeviceInfoData.cbSize = sizeof (SP_DEVINFO_DATA);3067 DriverInfoData.cbSize = sizeof (SP_DRVINFO_DATA);3068 3069 /* copy the net class GUID */3070 3053 memcpy(&netGuid, &GUID_DEVCLASS_NET, sizeof(GUID_DEVCLASS_NET)); 3071 3054 3072 /* create an empty device info set associated with the net class GUID*/3055 /* Create an empty device info set associated with the net class GUID: */ 3073 3056 hDeviceInfo = SetupDiCreateDeviceInfoList(&netGuid, NULL); 3074 3057 if (hDeviceInfo == INVALID_HANDLE_VALUE) 3075 SetErrBreak (("SetupDiCreateDeviceInfoList failed (0x%08X)", 3076 GetLastError())); 3077 3078 /* get the class name from GUID */ 3079 BOOL fResult = SetupDiClassNameFromGuid (&netGuid, className, MAX_PATH, NULL); 3080 if (!fResult) 3081 SetErrBreak (("SetupDiClassNameFromGuid failed (0x%08X)", 3082 GetLastError())); 3083 3084 /* create a device info element and add the new device instance 3085 * key to registry */ 3086 fResult = SetupDiCreateDeviceInfo (hDeviceInfo, className, &netGuid, NULL, NULL, 3087 DICD_GENERATE_ID, &DeviceInfoData); 3088 if (!fResult) 3089 SetErrBreak (("SetupDiCreateDeviceInfo failed (0x%08X)", 3090 GetLastError())); 3091 3092 /* select the newly created device info to be the currently 3093 selected member */ 3094 fResult = SetupDiSetSelectedDevice (hDeviceInfo, &DeviceInfoData); 3095 if (!fResult) 3096 SetErrBreak (("SetupDiSetSelectedDevice failed (0x%08X)", 3097 GetLastError())); 3098 3099 if (pInfPath) 3058 SetErrBreak(("SetupDiCreateDeviceInfoList failed (%Rwc)", GetLastError())); 3059 3060 /* Translate the GUID to a class name: */ 3061 WCHAR wszClassName[MAX_PATH]; 3062 if (!SetupDiClassNameFromGuid(&netGuid, wszClassName, MAX_PATH, NULL)) 3063 SetErrBreak(("SetupDiClassNameFromGuid failed (%Rwc)", GetLastError())); 3064 3065 /* Create a device info element and add the new device instance key to registry: */ 3066 if (!SetupDiCreateDeviceInfo(hDeviceInfo, wszClassName, &netGuid, NULL, NULL, DICD_GENERATE_ID, &DeviceInfoData)) 3067 SetErrBreak(("SetupDiCreateDeviceInfo failed (%Rwc)", GetLastError())); 3068 3069 /* Select the newly created device info to be the currently selected member: */ 3070 if (!SetupDiSetSelectedDevice(hDeviceInfo, &DeviceInfoData)) 3071 SetErrBreak(("SetupDiSetSelectedDevice failed (%Rwc)", GetLastError())); 3072 3073 SP_DEVINSTALL_PARAMS DeviceInstallParams; 3074 if (pwszInfPath) 3100 3075 { 3101 3076 /* get the device install parameters and disable filecopy */ 3102 3077 DeviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS); 3103 fResult = SetupDiGetDeviceInstallParams (hDeviceInfo, &DeviceInfoData, 3104 &DeviceInstallParams); 3105 if (fResult) 3078 if (SetupDiGetDeviceInstallParams(hDeviceInfo, &DeviceInfoData, &DeviceInstallParams)) 3106 3079 { 3107 3080 memset(DeviceInstallParams.DriverPath, 0, sizeof(DeviceInstallParams.DriverPath)); 3108 size_t pathLenght = wcslen(p InfPath) + 1/* null terminator */;3081 size_t pathLenght = wcslen(pwszInfPath) + 1/* null terminator */; 3109 3082 if (pathLenght < sizeof(DeviceInstallParams.DriverPath)/sizeof(DeviceInstallParams.DriverPath[0])) 3110 3083 { 3111 memcpy(DeviceInstallParams.DriverPath, pInfPath, pathLenght*sizeof(DeviceInstallParams.DriverPath[0])); 3112 3113 if (bIsInfPathFile) 3084 memcpy(DeviceInstallParams.DriverPath, pwszInfPath, pathLenght*sizeof(DeviceInstallParams.DriverPath[0])); 3085 3086 if (fIsInfPathFile) 3087 DeviceInstallParams.Flags |= DI_ENUMSINGLEINF; 3088 3089 if (!SetupDiSetDeviceInstallParams(hDeviceInfo, &DeviceInfoData, &DeviceInstallParams)) 3114 3090 { 3115 DeviceInstallParams.Flags |= DI_ENUMSINGLEINF; 3116 } 3117 3118 fResult = SetupDiSetDeviceInstallParams(hDeviceInfo, &DeviceInfoData, 3119 &DeviceInstallParams); 3120 if (!fResult) 3121 { 3122 DWORD winEr = GetLastError(); 3123 NonStandardLogFlow(("SetupDiSetDeviceInstallParams failed, winEr (%d)\n", winEr)); 3091 NonStandardLogFlow(("SetupDiSetDeviceInstallParams failed (%Rwc)\n", GetLastError())); 3124 3092 break; 3125 3093 } … … 3132 3100 } 3133 3101 else 3134 { 3135 DWORD winEr = GetLastError(); 3136 NonStandardLogFlow(("SetupDiGetDeviceInstallParams failed, winEr (%d)\n", winEr)); 3137 } 3102 NonStandardLogFlow(("SetupDiGetDeviceInstallParams failed (%Rwc)\n", GetLastError())); 3138 3103 } 3139 3104 3140 3105 /* build a list of class drivers */ 3141 fResult = SetupDiBuildDriverInfoList (hDeviceInfo, &DeviceInfoData, 3142 SPDIT_CLASSDRIVER); 3143 if (!fResult) 3144 SetErrBreak (("SetupDiBuildDriverInfoList failed (0x%08X)", 3145 GetLastError())); 3106 if (!SetupDiBuildDriverInfoList(hDeviceInfo, &DeviceInfoData, SPDIT_CLASSDRIVER)) 3107 SetErrBreak(("SetupDiBuildDriverInfoList failed (%Rwc)", GetLastError())); 3146 3108 3147 3109 destroyList = TRUE; 3148 3110 3149 /* enumerate the driver info list */ 3150 while (TRUE) 3151 { 3152 BOOL fRet = SetupDiEnumDriverInfo(hDeviceInfo, &DeviceInfoData, SPDIT_CLASSDRIVER, index, &DriverInfoData); 3153 3154 /* if the function failed and GetLastError() returns 3155 * ERROR_NO_MORE_ITEMS, then we have reached the end of the 3156 * list. Otherwise there was something wrong with this 3157 * particular driver. */ 3158 if (!fRet) 3111 /* 3112 * Enumerate the driver info list. 3113 */ 3114 /* For our purposes, 2k buffer is more than enough to obtain the 3115 hardware ID of the VBoxNetAdp driver. */ /** @todo r=bird: The buffer isn't 2KB, it's 8KB, but whatever. */ 3116 DWORD detailBuf[2048]; 3117 SP_DRVINFO_DATA DriverInfoData = { sizeof(DriverInfoData) }; 3118 bool fFound = false; 3119 for (DWORD index = 0; !fFound; index++) 3120 { 3121 /* If the function fails with last error set to ERROR_NO_MORE_ITEMS, 3122 then we have reached the end of the list. Otherwise there was 3123 something wrong with this particular driver. */ 3124 if (!SetupDiEnumDriverInfo(hDeviceInfo, &DeviceInfoData, SPDIT_CLASSDRIVER, index, &DriverInfoData)) 3159 3125 { 3160 3126 if (GetLastError() == ERROR_NO_MORE_ITEMS) 3161 3127 break; 3162 index++;3163 3128 continue; 3164 3129 } 3165 3166 pDriverInfoDetail = (PSP_DRVINFO_DETAIL_DATA) detailBuf;3167 pDriverInfoDetail->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);3168 3130 3169 3131 /* if we successfully find the hardware ID and it turns out to 3170 3132 * be the one for the loopback driver, then we are done. */ 3171 if (SetupDiGetDriverInfoDetail (hDeviceInfo, 3133 PSP_DRVINFO_DETAIL_DATA_W pDriverInfoDetail = (PSP_DRVINFO_DETAIL_DATA_W)detailBuf; 3134 pDriverInfoDetail->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA); 3135 DWORD cbValue = 0; 3136 if (SetupDiGetDriverInfoDetailW(hDeviceInfo, 3172 3137 &DeviceInfoData, 3173 3138 &DriverInfoData, 3174 3139 pDriverInfoDetail, 3175 sizeof (detailBuf), 3176 NULL)) 3177 { 3178 TCHAR * t; 3179 3180 /* pDriverInfoDetail->HardwareID is a MULTISZ string. Go through the 3181 * whole list and see if there is a match somewhere. */ 3182 t = pDriverInfoDetail->HardwareID; 3183 while (t && *t && t < (TCHAR *) &detailBuf [RT_ELEMENTS(detailBuf)]) 3184 { 3185 if (!_tcsicmp(t, DRIVERHWID)) 3140 sizeof(detailBuf) - sizeof(detailBuf[0]), 3141 &cbValue)) 3142 { 3143 /* Sure that the HardwareID string list is properly zero terminated (paranoia). */ 3144 detailBuf[RT_ELEMENTS(detailBuf) - 1] = 0; AssertCompile(sizeof(detailBuf[0]) == sizeof(WCHAR) * 2); 3145 3146 /* pDriverInfoDetail->HardwareID is a MULTISZ string. Go through the whole 3147 list and see if there is a match somewhere: */ 3148 for (WCHAR *pwszCurHwId = pDriverInfoDetail->HardwareID; 3149 (uintptr_t)pwszCurHwId - (uintptr_t)pDriverInfoDetail < cbValue && *pwszCurHwId != L'\0'; 3150 pwszCurHwId += RTUtf16Len(pwszCurHwId) + 1) 3151 if (RTUtf16ICmp(DRIVERHWID, pwszCurHwId) == 0) 3152 { 3153 fFound = true; 3186 3154 break; 3187 3188 t += _tcslen(t) + 1; 3189 } 3190 3191 if (t && *t && t < (TCHAR *) &detailBuf [RT_ELEMENTS(detailBuf)]) 3192 { 3193 found = TRUE; 3194 break; 3195 } 3196 } 3197 3198 index ++; 3199 } 3200 3201 if (!found) 3155 } 3156 } 3157 } 3158 3159 if (!fFound) 3202 3160 SetErrBreak(("Could not find Host Interface Networking driver! Please reinstall")); 3203 3161 3204 3162 /* set the loopback driver to be the currently selected */ 3205 fResult = SetupDiSetSelectedDriver (hDeviceInfo, &DeviceInfoData, 3206 &DriverInfoData); 3207 if (!fResult) 3208 SetErrBreak(("SetupDiSetSelectedDriver failed (0x%08X)", 3209 GetLastError())); 3163 if (!SetupDiSetSelectedDriver(hDeviceInfo, &DeviceInfoData, &DriverInfoData)) 3164 SetErrBreak(("SetupDiSetSelectedDriver failed (%u)", GetLastError())); 3210 3165 3211 3166 /* register the phantom device to prepare for install */ 3212 fResult = SetupDiCallClassInstaller (DIF_REGISTERDEVICE, hDeviceInfo, 3213 &DeviceInfoData); 3214 if (!fResult) 3215 { 3216 DWORD err = GetLastError(); 3217 SetErrBreak (("SetupDiCallClassInstaller failed (0x%08X)", 3218 err)); 3219 } 3167 if (!SetupDiCallClassInstaller(DIF_REGISTERDEVICE, hDeviceInfo, &DeviceInfoData)) 3168 SetErrBreak(("SetupDiCallClassInstaller failed (%u)", GetLastError())); 3220 3169 3221 3170 /* registered, but remove if errors occur in the following code */ 3222 registered = TRUE;3171 fRegistered = TRUE; 3223 3172 3224 3173 /* ask the installer if we can install the device */ 3225 fResult = SetupDiCallClassInstaller (DIF_ALLOW_INSTALL, hDeviceInfo, 3226 &DeviceInfoData); 3227 if (!fResult) 3174 if (!SetupDiCallClassInstaller(DIF_ALLOW_INSTALL, hDeviceInfo, &DeviceInfoData)) 3228 3175 { 3229 3176 if (GetLastError() != ERROR_DI_DO_DEFAULT) 3230 SetErrBreak (("SetupDiCallClassInstaller (DIF_ALLOW_INSTALL) failed (0x%08X)", 3231 GetLastError())); 3177 SetErrBreak(("SetupDiCallClassInstaller (DIF_ALLOW_INSTALL) failed (%u)", GetLastError())); 3232 3178 /* that's fine */ 3233 3179 } … … 3235 3181 /* get the device install parameters and disable filecopy */ 3236 3182 DeviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS); 3237 fResult = SetupDiGetDeviceInstallParams (hDeviceInfo, &DeviceInfoData, 3238 &DeviceInstallParams); 3239 if (fResult) 3183 if (SetupDiGetDeviceInstallParams(hDeviceInfo, &DeviceInfoData, &DeviceInstallParams)) 3240 3184 { 3241 3185 pQueueCallbackContext = SetupInitDefaultQueueCallback(NULL); … … 3244 3188 DeviceInstallParams.InstallMsgHandlerContext = pQueueCallbackContext; 3245 3189 DeviceInstallParams.InstallMsgHandler = (PSP_FILE_CALLBACK)vboxNetCfgWinPspFileCallback; 3246 fResult = SetupDiSetDeviceInstallParams (hDeviceInfo, &DeviceInfoData, 3247 &DeviceInstallParams); 3248 if (!fResult) 3190 if (!SetupDiSetDeviceInstallParamsW(hDeviceInfo, &DeviceInfoData, &DeviceInstallParams)) 3249 3191 { 3250 3192 DWORD winEr = GetLastError(); 3251 NonStandardLogFlow(("SetupDiSetDeviceInstallParams failed, winEr (%d)\n", winEr));3252 }3253 Assert(fResult);3193 NonStandardLogFlow(("SetupDiSetDeviceInstallParamsW failed, winEr (%d)\n", winEr)); 3194 Assert(0); 3195 } 3254 3196 } 3255 3197 else … … 3266 3208 3267 3209 /* install the files first */ 3268 fResult = SetupDiCallClassInstaller (DIF_INSTALLDEVICEFILES, hDeviceInfo, 3269 &DeviceInfoData); 3270 if (!fResult) 3271 SetErrBreak (("SetupDiCallClassInstaller (DIF_INSTALLDEVICEFILES) failed (0x%08X)", 3272 GetLastError())); 3210 if (!SetupDiCallClassInstaller(DIF_INSTALLDEVICEFILES, hDeviceInfo, &DeviceInfoData)) 3211 SetErrBreak(("SetupDiCallClassInstaller (DIF_INSTALLDEVICEFILES) failed (%Rwc)", GetLastError())); 3212 3273 3213 /* get the device install parameters and disable filecopy */ 3274 3214 DeviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS); 3275 fResult = SetupDiGetDeviceInstallParams (hDeviceInfo, &DeviceInfoData, 3276 &DeviceInstallParams); 3277 if (fResult) 3215 if (SetupDiGetDeviceInstallParams(hDeviceInfo, &DeviceInfoData, &DeviceInstallParams)) 3278 3216 { 3279 3217 DeviceInstallParams.Flags |= DI_NOFILECOPY; 3280 fResult = SetupDiSetDeviceInstallParams(hDeviceInfo, &DeviceInfoData, 3281 &DeviceInstallParams); 3282 if (!fResult) 3283 SetErrBreak (("SetupDiSetDeviceInstallParams failed (0x%08X)", 3284 GetLastError())); 3285 } 3218 if (!SetupDiSetDeviceInstallParamsW(hDeviceInfo, &DeviceInfoData, &DeviceInstallParams)) 3219 SetErrBreak(("SetupDiSetDeviceInstallParamsW failed (%Rwc)", GetLastError())); 3220 } 3221 /** @todo r=bird: Why isn't SetupDiGetDeviceInstallParams failure fatal here? */ 3286 3222 3287 3223 /* 3288 3224 * Register any device-specific co-installers for this device, 3289 3225 */ 3290 fResult = SetupDiCallClassInstaller(DIF_REGISTER_COINSTALLERS, 3291 hDeviceInfo, 3292 &DeviceInfoData); 3293 if (!fResult) 3294 SetErrBreak (("SetupDiCallClassInstaller (DIF_REGISTER_COINSTALLERS) failed (0x%08X)", 3295 GetLastError())); 3226 if (!SetupDiCallClassInstaller(DIF_REGISTER_COINSTALLERS, hDeviceInfo, &DeviceInfoData)) 3227 SetErrBreak(("SetupDiCallClassInstaller (DIF_REGISTER_COINSTALLERS) failed (%Rwc)", GetLastError())); 3296 3228 3297 3229 /* … … 3299 3231 * and then do the real install 3300 3232 */ 3301 fResult = SetupDiCallClassInstaller(DIF_INSTALLINTERFACES, 3302 hDeviceInfo, 3303 &DeviceInfoData); 3304 if (!fResult) 3305 SetErrBreak (("SetupDiCallClassInstaller (DIF_INSTALLINTERFACES) failed (0x%08X)", 3306 GetLastError())); 3307 3308 fResult = SetupDiCallClassInstaller(DIF_INSTALLDEVICE, 3309 hDeviceInfo, 3310 &DeviceInfoData); 3311 if (!fResult) 3312 SetErrBreak (("SetupDiCallClassInstaller (DIF_INSTALLDEVICE) failed (0x%08X)", 3313 GetLastError())); 3314 3315 /* Query the instance ID; on Windows 10, the registry key may take a short 3233 if (!SetupDiCallClassInstaller(DIF_INSTALLINTERFACES, hDeviceInfo, &DeviceInfoData)) 3234 SetErrBreak(("SetupDiCallClassInstaller (DIF_INSTALLINTERFACES) failed (%Rwc)", GetLastError())); 3235 3236 if (!SetupDiCallClassInstaller(DIF_INSTALLDEVICE, hDeviceInfo, &DeviceInfoData)) 3237 SetErrBreak(("SetupDiCallClassInstaller (DIF_INSTALLDEVICE) failed (%Rwc)", GetLastError())); 3238 3239 /* 3240 * Query the instance ID; on Windows 10, the registry key may take a short 3316 3241 * while to appear. Microsoft recommends waiting for up to 5 seconds, but 3317 3242 * we want to be on the safe side, so let's wait for 20 seconds. Waiting … … 3324 3249 3325 3250 /* Figure out NetCfgInstanceId */ 3326 hkey = SetupDiOpenDevRegKey(hDeviceInfo, 3327 &DeviceInfoData, 3328 DICS_FLAG_GLOBAL, 3329 0, 3330 DIREG_DRV, 3331 KEY_READ); 3251 hkey = SetupDiOpenDevRegKey(hDeviceInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_READ); 3332 3252 if (hkey == INVALID_HANDLE_VALUE) 3333 3253 break; 3334 3254 3335 cbSize = sizeof(pWCfgGuidString);3336 ret = RegQueryValueExW (hkey, L"NetCfgInstanceId", NULL,3337 &dwValueType, (LPBYTE) pWCfgGuidString, &cbSize);3255 DWORD cbSize = sizeof(wszCfgGuidString); 3256 DWORD dwValueType = 0; 3257 lrcRet = RegQueryValueExW(hkey, L"NetCfgInstanceId", NULL, &dwValueType, (LPBYTE)wszCfgGuidString, &cbSize); 3338 3258 /* As long as the return code is FILE_NOT_FOUND, sleep and retry. */ 3339 if ( ret != ERROR_FILE_NOT_FOUND)3259 if (lrcRet != ERROR_FILE_NOT_FOUND) 3340 3260 break; 3341 3261 3342 RegCloseKey 3262 RegCloseKey(hkey); 3343 3263 hkey = (HKEY)INVALID_HANDLE_VALUE; 3344 3264 } 3345 3265 3346 if ( ret == ERROR_FILE_NOT_FOUND)3266 if (lrcRet == ERROR_FILE_NOT_FOUND) 3347 3267 { 3348 3268 hrc = E_ABORT; … … 3351 3271 3352 3272 /* 3353 * We need to check 'hkey' after we check ' ret' to distinguish the case3273 * We need to check 'hkey' after we check 'lrcRet' to distinguish the case 3354 3274 * of failed SetupDiOpenDevRegKey from the case when we timed out. 3355 3275 */ 3356 3276 if (hkey == INVALID_HANDLE_VALUE) 3357 SetErrBreak(("SetupDiOpenDevRegKey failed ( 0x%08X)", GetLastError()));3358 3359 if ( ret != ERROR_SUCCESS)3360 SetErrBreak(("Querying NetCfgInstanceId failed ( 0x%08X)", ret));3277 SetErrBreak(("SetupDiOpenDevRegKey failed (%Rwc)", GetLastError())); 3278 3279 if (lrcRet != ERROR_SUCCESS) 3280 SetErrBreak(("Querying NetCfgInstanceId failed (%Rwc)", lrcRet)); 3361 3281 3362 3282 NET_LUID luid; 3363 HRESULT h SMRes = vboxNetCfgWinGetInterfaceLUID(hkey, &luid);3283 HRESULT hrcSMRes = vboxNetCfgWinGetInterfaceLUID(hkey, &luid); 3364 3284 3365 3285 /* Close the key as soon as possible. See @bugref{7973}. */ 3366 RegCloseKey 3286 RegCloseKey(hkey); 3367 3287 hkey = (HKEY)INVALID_HANDLE_VALUE; 3368 3288 3369 if (FAILED(h SMRes))3289 if (FAILED(hrcSMRes)) 3370 3290 { 3371 3291 /* 3372 *The setting of Metric is not very important functionality,3373 *So we will not break installation process due to this error.3374 */3375 NonStandardLogFlow(("vboxNetCfgWinCreateHostOnlyNetworkInterface Warning! "3376 "vboxNetCfgWinGetInterfaceLUID failed, default metric "3377 "for new interface will not be set, hr (0x%x)\n", hSMRes));3292 * The setting of Metric is not very important functionality, 3293 * So we will not break installation process due to this error. 3294 */ 3295 NonStandardLogFlow(("vboxNetCfgWinCreateHostOnlyNetworkInterface: Warning! " 3296 "vboxNetCfgWinGetInterfaceLUID failed, default metric for new interface will not be set: %Rhrc\n", 3297 hrcSMRes)); 3378 3298 } 3379 3299 else 3380 3300 { 3381 3301 /* 3382 * 3383 * 3302 * Set default metric value of interface to fix multicast issue 3303 * See @bugref{6379} for details. 3384 3304 */ 3385 h SMRes = vboxNetCfgWinSetupMetric(&luid);3386 if (FAILED(h SMRes))3305 hrcSMRes = vboxNetCfgWinSetupMetric(&luid); 3306 if (FAILED(hrcSMRes)) 3387 3307 { 3388 3308 /* 3389 * 3390 * 3309 * The setting of Metric is not very important functionality, 3310 * So we will not break installation process due to this error. 3391 3311 */ 3392 NonStandardLogFlow(("vboxNetCfgWinCreateHostOnlyNetworkInterface Warning! " 3393 "vboxNetCfgWinSetupMetric failed, default metric " 3394 "for new interface will not be set, hr (0x%x)\n", hSMRes)); 3395 } 3396 } 3397 3312 NonStandardLogFlow(("vboxNetCfgWinCreateHostOnlyNetworkInterface: Warning! " 3313 "vboxNetCfgWinSetupMetric failed, default metric for new interface will not be set: %Rhrc\n", 3314 hrcSMRes)); 3315 } 3316 } 3398 3317 3399 3318 /* … … 3404 3323 SPDRP_FRIENDLYNAME , /* IN DWORD Property,*/ 3405 3324 NULL, /*OUT PDWORD PropertyRegDataType, OPTIONAL*/ 3406 (PBYTE)DevName, /*OUT PBYTE PropertyBuffer,*/ 3407 sizeof(DevName), /* IN DWORD PropertyBufferSize,*/ 3408 NULL /*OUT PDWORD RequiredSize OPTIONAL*/)) 3409 { 3410 int err = GetLastError(); 3411 if (err != ERROR_INVALID_DATA) 3412 { 3413 SetErrBreak (("SetupDiGetDeviceRegistryProperty failed (0x%08X)", 3414 err)); 3415 } 3416 3325 (PBYTE)wszDevName, /*OUT PBYTE PropertyBuffer,*/ 3326 sizeof(wszDevName) - sizeof(WCHAR), /* IN DWORD PropertyBufferSize,*/ 3327 NULL /*OUT PDWORD RequiredSize OPTIONAL*/ )) 3328 { 3329 DWORD dwErr = GetLastError(); 3330 if (dwErr != ERROR_INVALID_DATA) 3331 SetErrBreak(("SetupDiGetDeviceRegistryProperty failed (%Rwc)", dwErr)); 3332 3333 RT_ZERO(wszDevName); 3417 3334 if (!SetupDiGetDeviceRegistryPropertyW(hDeviceInfo, &DeviceInfoData, 3418 SPDRP_DEVICEDESC, /* IN DWORD Property,*/ 3419 NULL, /*OUT PDWORD PropertyRegDataType, OPTIONAL*/ 3420 (PBYTE)DevName, /*OUT PBYTE PropertyBuffer,*/ 3421 sizeof(DevName), /* IN DWORD PropertyBufferSize,*/ 3422 NULL /*OUT PDWORD RequiredSize OPTIONAL*/ 3423 )) 3424 { 3425 err = GetLastError(); 3426 SetErrBreak (("SetupDiGetDeviceRegistryProperty failed (0x%08X)", 3427 err)); 3428 } 3429 } 3335 SPDRP_DEVICEDESC, /* IN DWORD Property,*/ 3336 NULL, /*OUT PDWORD PropertyRegDataType, OPTIONAL*/ 3337 (PBYTE)wszDevName, /*OUT PBYTE PropertyBuffer,*/ 3338 sizeof(wszDevName) - sizeof(WCHAR), /* IN DWORD PropertyBufferSize,*/ 3339 NULL /*OUT PDWORD RequiredSize OPTIONAL*/ )) 3340 SetErrBreak(("SetupDiGetDeviceRegistryProperty failed (%Rwc)", GetLastError())); 3341 } 3342 3430 3343 /* No need to rename the device if the names match. */ 3431 if (!wcscmp(bstrNewInterfaceName.GetBSTR(), DevName)) 3432 bstrNewInterfaceName.Assign(NULL); 3344 if (RTUtf16Cmp(bstrNewInterfaceName.raw(), wszDevName) == 0) 3345 bstrNewInterfaceName.setNull(); 3346 3433 3347 #ifdef VBOXNETCFG_DELAYEDRENAME 3434 /* Re-use DevName for device instance id retrieval. */ 3435 if (!SetupDiGetDeviceInstanceId(hDeviceInfo, &DeviceInfoData, DevName, RT_ELEMENTS(DevName), &cbSize)) 3436 SetErrBreak (("SetupDiGetDeviceInstanceId failed (0x%08X)", 3437 GetLastError())); 3348 /* Re-use wszDevName for device instance id retrieval. */ 3349 DWORD cwcReturned = 0; 3350 RT_ZERO(wszDevName); 3351 if (!SetupDiGetDeviceInstanceIdW(hDeviceInfo, &DeviceInfoData, wszDevName, RT_ELEMENTS(wszDevName) - 1, &cwcReturned)) 3352 SetErrBreak(("SetupDiGetDeviceInstanceId failed (%Rwc)", GetLastError())); 3438 3353 #endif /* VBOXNETCFG_DELAYEDRENAME */ 3439 } 3440 while (0); 3354 } while (0); 3441 3355 3442 3356 /* 3443 * cleanup3357 * Cleanup. 3444 3358 */ 3445 3359 if (hkey != INVALID_HANDLE_VALUE) 3446 RegCloseKey 3360 RegCloseKey(hkey); 3447 3361 3448 3362 if (pQueueCallbackContext) … … 3452 3366 { 3453 3367 /* an error has occurred, but the device is registered, we must remove it */ 3454 if ( ret != 0 && registered)3368 if (lrcRet != ERROR_SUCCESS && fRegistered) 3455 3369 SetupDiCallClassInstaller(DIF_REMOVE, hDeviceInfo, &DeviceInfoData); 3456 3370 … … 3458 3372 3459 3373 /* destroy the driver info list */ 3374 #if 0 3375 /* I've remove this, as I was consistently getting crashes in 3376 * SetupDiDestroyDeviceInfoList otherwise during MSI installation 3377 * (W10 build 19044, VBox r153431 + nocrt changes). 3378 * 3379 * Some details from windbg: 3380 * 3381 * (175e8.1596c): Access violation - code c0000005 (first chance) 3382 * First chance exceptions are reported before any exception handling. 3383 * This exception may be expected and handled. 3384 * SETUPAPI!DereferenceClassDriverList+0x4e: 3385 * 00007ffa`83e2a42a 834008ff add dword ptr [rax+8],0FFFFFFFFh ds:00000000`00000008=???????? 3386 * 0:006> k 3387 * # Child-SP RetAddr Call Site 3388 * 00 0000007e`ccd7c070 00007ffa`83e2a287 SETUPAPI!DereferenceClassDriverList+0x4e 3389 * 01 0000007e`ccd7c0a0 00007ffa`56b96bd3 SETUPAPI!SetupDiDestroyDriverInfoList+0x117 3390 * 02 0000007e`ccd7c0f0 00007ffa`56b982a3 MSIF170!vboxNetCfgWinCreateHostOnlyNetworkInterface+0xb23 [E:\vbox\svn\trunk\src\VBox\HostDrivers\VBoxNetFlt\win\cfg\VBoxNetCfg.cpp @ 3378] 3391 * 03 0000007e`ccd7ef10 00007ffa`56b92cb8 MSIF170!VBoxNetCfgWinCreateHostOnlyNetworkInterface+0x53 [E:\vbox\svn\trunk\src\VBox\HostDrivers\VBoxNetFlt\win\cfg\VBoxNetCfg.cpp @ 3479] 3392 * 04 0000007e`ccd7efc0 00007ffa`610f59d3 MSIF170!_createHostOnlyInterface+0x218 [E:\vbox\svn\trunk\src\VBox\Installer\win\InstallHelper\VBoxInstallHelper.cpp @ 1453] 3393 * 05 0000007e`ccd7f260 00007ffa`610d80ac msi!CallCustomDllEntrypoint+0x2b 3394 * 06 0000007e`ccd7f2d0 00007ffa`84567034 msi!CMsiCustomAction::CustomActionThread+0x34c 3395 * 07 0000007e`ccd7f8c0 00007ffa`849a2651 KERNEL32!BaseThreadInitThunk+0x14 3396 * 08 0000007e`ccd7f8f0 00000000`00000000 ntdll!RtlUserThreadStart+0x21 3397 * 0:006> r 3398 * rax=0000000000000000 rbx=0000025f4f03af90 rcx=00007ffa83f23b40 3399 * rdx=0000025f4f03af90 rsi=0000025f5108be50 rdi=0000025f4f066f10 3400 * rip=00007ffa83e2a42a rsp=0000007eccd7c070 rbp=00007ffa83f23000 3401 * r8=0000007eccd7c0a8 r9=0000007eccd7c1f0 r10=0000000000000000 3402 * r11=0000007eccd7c020 r12=0000007eccd7c3d0 r13=00007ffa83f23000 3403 * r14=0000000000000000 r15=0000025f4f03af90 3404 * iopl=0 nv up ei pl nz na po nc 3405 * cs=0033 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00010206 3406 * SETUPAPI!DereferenceClassDriverList+0x4e: 3407 * 00007ffa`83e2a42a 834008ff add dword ptr [rax+8],0FFFFFFFFh ds:00000000`00000008=???????? 3408 * 3409 */ 3460 3410 if (destroyList) 3461 SetupDiDestroyDriverInfoList(hDeviceInfo, &DeviceInfoData, 3462 SPDIT_CLASSDRIVER); 3411 SetupDiDestroyDriverInfoList(hDeviceInfo, &DeviceInfoData, SPDIT_CLASSDRIVER); 3412 #else 3413 RT_NOREF(destroyList); 3414 #endif 3415 3463 3416 /* clean up the device info set */ 3464 SetupDiDestroyDeviceInfoList (hDeviceInfo); 3465 } 3466 3467 /* return the network connection GUID on success */ 3417 SetupDiDestroyDeviceInfoList(hDeviceInfo); 3418 } 3419 3420 /* 3421 * Return the network connection GUID on success. 3422 */ 3468 3423 if (SUCCEEDED(hrc)) 3469 3424 { 3470 HRESULT hr; 3471 INetCfg *pNetCfg = NULL; 3472 LPWSTR lpszApp = NULL; 3473 3425 /** @todo r=bird: Some please explain this mess. It's not just returning a 3426 * GUID here, it's a bit more than that. */ 3474 3427 RENAMING_CONTEXT context; 3475 3428 context.hr = E_FAIL; … … 3477 3430 if (pGuid) 3478 3431 { 3479 hrc = CLSIDFromString( pWCfgGuidString, (LPCLSID)pGuid);3432 hrc = CLSIDFromString(wszCfgGuidString, (LPCLSID)pGuid); 3480 3433 if (FAILED(hrc)) 3481 3434 NonStandardLogFlow(("CLSIDFromString failed, hrc (0x%x)\n", hrc)); 3482 3435 } 3483 3436 3484 hr = VBoxNetCfgWinQueryINetCfg(&pNetCfg, TRUE, L"VirtualBox Host-Only Creation", 3485 30 * 1000, /* on Vista we often get 6to4svc.dll holding the lock, wait for 30 sec. */ 3486 /** @todo special handling for 6to4svc.dll ???, i.e. several retrieves */ 3487 &lpszApp); 3488 if (hr == S_OK) 3489 { 3490 if (!!bstrNewInterfaceName) 3437 INetCfg *pNetCfg = NULL; 3438 LPWSTR pwszApp = NULL; 3439 HRESULT hrc2 = VBoxNetCfgWinQueryINetCfg(&pNetCfg, TRUE, L"VirtualBox Host-Only Creation", 3440 30 * 1000, /* on Vista we often get 6to4svc.dll holding the lock, wait for 30 sec. */ 3441 /** @todo special handling for 6to4svc.dll ???, i.e. several retrieves */ 3442 &pwszApp); 3443 if (hrc2 == S_OK) 3444 { 3445 if (bstrNewInterfaceName.isNotEmpty()) 3491 3446 { 3492 3447 /* The assigned name does not match the desired one, rename the device */ 3493 context.bstrName = bstrNewInterfaceName.GetBSTR(); 3494 context.pGuid = pGuid; 3495 hr = vboxNetCfgWinEnumNetCfgComponents(pNetCfg, 3496 &GUID_DEVCLASS_NET, 3497 vboxNetCfgWinRenameHostOnlyNetworkInterface, 3498 &context); 3499 } 3500 if (SUCCEEDED(hr)) 3501 hr = vboxNetCfgWinEnumNetCfgComponents(pNetCfg, 3502 &GUID_DEVCLASS_NETSERVICE, 3503 vboxNetCfgWinAdjustHostOnlyNetworkInterfacePriority, 3504 pGuid); 3505 if (SUCCEEDED(hr)) 3506 hr = vboxNetCfgWinEnumNetCfgComponents(pNetCfg, 3507 &GUID_DEVCLASS_NETTRANS, 3508 vboxNetCfgWinAdjustHostOnlyNetworkInterfacePriority, 3509 pGuid); 3510 if (SUCCEEDED(hr)) 3511 hr = vboxNetCfgWinEnumNetCfgComponents(pNetCfg, 3512 &GUID_DEVCLASS_NETCLIENT, 3513 vboxNetCfgWinAdjustHostOnlyNetworkInterfacePriority, 3514 pGuid); 3515 if (SUCCEEDED(hr)) 3516 { 3517 hr = pNetCfg->Apply(); 3518 } 3448 context.bstrName = bstrNewInterfaceName.raw(); 3449 context.pGuid = pGuid; 3450 hrc2 = vboxNetCfgWinEnumNetCfgComponents(pNetCfg, &GUID_DEVCLASS_NET, 3451 vboxNetCfgWinRenameHostOnlyNetworkInterface, &context); 3452 } 3453 if (SUCCEEDED(hrc2)) 3454 hrc2 = vboxNetCfgWinEnumNetCfgComponents(pNetCfg, &GUID_DEVCLASS_NETSERVICE, 3455 vboxNetCfgWinAdjustHostOnlyNetworkInterfacePriority, pGuid); 3456 if (SUCCEEDED(hrc2)) 3457 hrc2 = vboxNetCfgWinEnumNetCfgComponents(pNetCfg, &GUID_DEVCLASS_NETTRANS, 3458 vboxNetCfgWinAdjustHostOnlyNetworkInterfacePriority, pGuid); 3459 if (SUCCEEDED(hrc2)) 3460 hrc2 = vboxNetCfgWinEnumNetCfgComponents(pNetCfg, &GUID_DEVCLASS_NETCLIENT, 3461 vboxNetCfgWinAdjustHostOnlyNetworkInterfacePriority, pGuid); 3462 if (SUCCEEDED(hrc2)) 3463 hrc2 = pNetCfg->Apply(); 3519 3464 else 3520 NonStandardLogFlow(("Enumeration failed, hr 0x%x\n", hr)); 3465 NonStandardLogFlow(("Enumeration failed, hrc2=%Rhrc\n", hrc2)); 3466 3521 3467 VBoxNetCfgWinReleaseINetCfg(pNetCfg, TRUE); 3522 3468 } 3523 else if (hr == NETCFG_E_NO_WRITE_LOCK && lpszApp) 3524 { 3525 NonStandardLogFlow(("Application %ws is holding the lock, failed\n", lpszApp)); 3526 CoTaskMemFree(lpszApp); 3469 else if (hrc2 == NETCFG_E_NO_WRITE_LOCK && pwszApp) 3470 { 3471 NonStandardLogFlow(("Application '%ls' is holding the lock, failed\n", pwszApp)); 3472 CoTaskMemFree(pwszApp); 3473 pwszApp = NULL; 3527 3474 } 3528 3475 else 3529 NonStandardLogFlow(("VBoxNetCfgWinQueryINetCfg failed, hr 0x%x\n", hr));3476 NonStandardLogFlow(("VBoxNetCfgWinQueryINetCfg failed, hrc2=%Rhrc\n", hrc2)); 3530 3477 3531 3478 #ifndef VBOXNETCFG_DELAYEDRENAME 3532 if (SUCCEEDED(hr) && SUCCEEDED(context.hr)) 3533 { 3534 /* The device has been successfully renamed, replace the name now. */ 3535 wcscpy_s(DevName, RT_ELEMENTS(DevName), bstrDesiredName); 3536 } 3537 3538 WCHAR ConnectionName[128]; 3539 ULONG cbName = sizeof(ConnectionName); 3540 3541 hr = VBoxNetCfgWinGenHostonlyConnectionName(DevName, ConnectionName, &cbName); 3542 if (SUCCEEDED(hr)) 3543 hr = VBoxNetCfgWinRenameConnection(pWCfgGuidString, ConnectionName); 3479 /* If the device has been successfully renamed, replace the name now. */ 3480 if (SUCCEEDED(hrc2) && SUCCEEDED(context.hr)) 3481 RTUtf16Copy(wszDevName, RT_ELEMENTS(wszDevName), pBstrDesiredName); 3482 3483 WCHAR wszConnectionName[128]; 3484 hrc2 = VBoxNetCfgWinGenHostonlyConnectionName(wszDevName, wszConnectionName, RT_ELEMENTS(wszConnectionName), NULL); 3485 if (SUCCEEDED(hrc2)) 3486 hrc2 = VBoxNetCfgWinRenameConnection(wszCfgGuidString, wszConnectionName); 3544 3487 #endif 3545 if (lppszName) 3546 { 3547 *lppszName = SysAllocString((const OLECHAR *) DevName); 3548 if (!*lppszName) 3488 3489 /* 3490 * Now, return the network connection GUID/name. 3491 */ 3492 if (pBstrName) 3493 { 3494 *pBstrName = SysAllocString((const OLECHAR *)wszDevName); 3495 if (!*pBstrName) 3549 3496 { 3550 3497 NonStandardLogFlow(("SysAllocString failed\n")); 3551 hrc = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY); 3552 } 3553 } 3554 } 3555 3556 if (pErrMsg && bstrError.length()) 3557 *pErrMsg = bstrError.Detach(); 3558 3498 hrc = E_OUTOFMEMORY; 3499 } 3500 } 3501 } 3502 3503 if (pBstrErrMsg) 3504 { 3505 *pBstrErrMsg = NULL; 3506 if (bstrError.isNotEmpty()) 3507 bstrError.detachToEx(pBstrErrMsg); 3508 } 3559 3509 return hrc; 3560 3510 } 3561 3511 3562 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinCreateHostOnlyNetworkInterface(IN LPCWSTR pInfPath, IN bool bIsInfPathFile, IN BSTR pwsDesiredName, 3563 OUT GUID *pGuid, OUT BSTR *lppszName, OUT BSTR *pErrMsg) 3564 { 3565 HRESULT hrc = vboxNetCfgWinCreateHostOnlyNetworkInterface(pInfPath, bIsInfPathFile, pwsDesiredName, pGuid, lppszName, pErrMsg); 3512 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinCreateHostOnlyNetworkInterface(IN LPCWSTR pwszInfPath, IN bool fIsInfPathFile, 3513 IN BSTR pwszDesiredName, OUT GUID *pGuid, 3514 OUT BSTR *pBstrName, OUT BSTR *pBstrErrMsg) 3515 { 3516 HRESULT hrc = vboxNetCfgWinCreateHostOnlyNetworkInterface(pwszInfPath, fIsInfPathFile, pwszDesiredName, 3517 pGuid, pBstrName, pBstrErrMsg); 3566 3518 if (hrc == E_ABORT) 3567 3519 { 3568 3520 NonStandardLogFlow(("Timed out while waiting for NetCfgInstanceId, try again immediately...\n")); 3521 3569 3522 /* 3570 3523 * This is the first time we fail to obtain NetCfgInstanceId, let us … … 3575 3528 * See @bugref{7973} for details. 3576 3529 */ 3577 hrc = vboxNetCfgWinCreateHostOnlyNetworkInterface(pInfPath, bIsInfPathFile, pwsDesiredName, pGuid, lppszName, pErrMsg); 3530 hrc = vboxNetCfgWinCreateHostOnlyNetworkInterface(pwszInfPath, fIsInfPathFile, pwszDesiredName, 3531 pGuid, pBstrName, pBstrErrMsg); 3578 3532 if (hrc == E_ABORT) 3579 3533 { 3580 3534 NonStandardLogFlow(("Timed out again while waiting for NetCfgInstanceId, try again after a while...\n")); 3535 3581 3536 /* 3582 3537 * This is the second time we fail to obtain NetCfgInstanceId, let us … … 3585 3540 * conditions. See @bugref{7973} for details. 3586 3541 */ 3587 3588 SC_HANDLE hSCM = NULL; 3589 SC_HANDLE hService = NULL; 3590 3591 hSCM = OpenSCManager(NULL, NULL, GENERIC_READ); 3542 SC_HANDLE hSCM = OpenSCManagerW(NULL, NULL, GENERIC_READ); 3592 3543 if (hSCM) 3593 3544 { 3594 hService = OpenService(hSCM, _T("NetSetupSvc"), GENERIC_READ);3545 SC_HANDLE hService = OpenServiceW(hSCM, L"NetSetupSvc", GENERIC_READ); 3595 3546 if (hService) 3596 3547 { … … 3598 3549 Sleep(1000); 3599 3550 CloseServiceHandle(hService); 3600 hrc = vboxNetCfgWinCreateHostOnlyNetworkInterface(pInfPath, bIsInfPathFile, pwsDesiredName, pGuid, lppszName, pErrMsg); 3551 hrc = vboxNetCfgWinCreateHostOnlyNetworkInterface(pwszInfPath, fIsInfPathFile, pwszDesiredName, 3552 pGuid, pBstrName, pBstrErrMsg); 3601 3553 } 3602 3554 else 3603 NonStandardLogFlow(("OpenService failed ( 0x%x)\n", GetLastError()));3555 NonStandardLogFlow(("OpenService failed (%Rwc)\n", GetLastError())); 3604 3556 CloseServiceHandle(hSCM); 3605 3557 } 3606 3558 else 3607 NonStandardLogFlow(("OpenSCManager failed (0x%x)", GetLastError())); 3559 NonStandardLogFlow(("OpenSCManager failed (%Rwc)", GetLastError())); 3560 3608 3561 /* Give up and report the error. */ 3609 3562 if (hrc == E_ABORT) 3610 3563 { 3611 if (pErrMsg) 3612 { 3613 bstr_t bstrError = bstr_printf("Querying NetCfgInstanceId failed (0x%08X)", ERROR_FILE_NOT_FOUND); 3614 *pErrMsg = bstrError.Detach(); 3564 if (pBstrErrMsg) 3565 { 3566 com::Bstr bstrError; 3567 bstrError.printfNoThrow("Querying NetCfgInstanceId failed (ERROR_FILE_NOT_FOUND)"); 3568 bstrError.detachToEx(pBstrErrMsg); 3615 3569 } 3616 3570 hrc = E_FAIL; … … 3621 3575 } 3622 3576 3623 3624 HRESULT vboxLoadIpHelpFunctions(HINSTANCE& pIpHlpInstance) 3625 { 3626 Assert(pIpHlpInstance != NULL); 3627 3628 pIpHlpInstance = loadSystemDll("Iphlpapi.dll"); 3629 if (pIpHlpInstance == NULL) 3630 return E_FAIL; 3631 3632 g_pfnInitializeIpInterfaceEntry = 3633 (PFNINITIALIZEIPINTERFACEENTRY)GetProcAddress(pIpHlpInstance, "InitializeIpInterfaceEntry"); 3634 Assert(g_pfnInitializeIpInterfaceEntry); 3635 3636 if (g_pfnInitializeIpInterfaceEntry) 3637 { 3638 g_pfnGetIpInterfaceEntry = 3639 (PFNGETIPINTERFACEENTRY)GetProcAddress(pIpHlpInstance, "GetIpInterfaceEntry"); 3640 Assert(g_pfnGetIpInterfaceEntry); 3641 } 3642 3643 if (g_pfnGetIpInterfaceEntry) 3644 { 3645 g_pfnSetIpInterfaceEntry = 3646 (PFNSETIPINTERFACEENTRY)GetProcAddress(pIpHlpInstance, "SetIpInterfaceEntry"); 3647 Assert(g_pfnSetIpInterfaceEntry); 3648 } 3649 3650 if (g_pfnInitializeIpInterfaceEntry == NULL) 3651 { 3652 FreeLibrary(pIpHlpInstance); 3653 pIpHlpInstance = NULL; 3654 return E_FAIL; 3655 } 3656 3657 return S_OK; 3658 } 3659 3660 3661 HRESULT vboxNetCfgWinGetLoopbackMetric(OUT int* Metric) 3662 { 3663 HRESULT rc = S_OK; 3664 MIB_IPINTERFACE_ROW row; 3665 3577 static HRESULT vboxNetCfgWinGetLoopbackMetric(OUT DWORD *Metric) 3578 { 3666 3579 Assert(g_pfnInitializeIpInterfaceEntry != NULL); 3667 3580 Assert(g_pfnGetIpInterfaceEntry != NULL); 3668 3581 3582 MIB_IPINTERFACE_ROW row; 3583 RT_ZERO(row); /* paranoia */ 3669 3584 g_pfnInitializeIpInterfaceEntry(&row); 3585 3670 3586 row.Family = AF_INET; 3671 3587 row.InterfaceLuid.Info.IfType = IF_TYPE_SOFTWARE_LOOPBACK; 3672 3588 3673 rc = g_pfnGetIpInterfaceEntry(&row); 3674 if (rc != NO_ERROR) 3675 return HRESULT_FROM_WIN32(rc); 3676 3677 *Metric = row.Metric; 3678 3679 return rc; 3680 } 3681 3682 3683 HRESULT vboxNetCfgWinSetInterfaceMetric( 3684 IN NET_LUID* pInterfaceLuid, 3685 IN DWORD metric) 3686 { 3687 MIB_IPINTERFACE_ROW newRow; 3688 3589 NETIO_STATUS dwErr = g_pfnGetIpInterfaceEntry(&row); 3590 if (dwErr == NO_ERROR) 3591 { 3592 *Metric = row.Metric; 3593 return S_OK; 3594 } 3595 return HRESULT_FROM_WIN32(dwErr); 3596 } 3597 3598 static HRESULT vboxNetCfgWinSetInterfaceMetric(IN NET_LUID *pInterfaceLuid, IN DWORD metric) 3599 { 3689 3600 Assert(g_pfnInitializeIpInterfaceEntry != NULL); 3690 3601 Assert(g_pfnSetIpInterfaceEntry != NULL); 3691 3602 3603 MIB_IPINTERFACE_ROW newRow; 3604 RT_ZERO(newRow); /* paranoia */ 3692 3605 g_pfnInitializeIpInterfaceEntry(&newRow); 3606 3693 3607 // identificate the interface to change 3694 3608 newRow.InterfaceLuid = *pInterfaceLuid; 3695 3609 newRow.Family = AF_INET; 3610 3696 3611 // changed settings 3697 3612 newRow.UseAutomaticMetric = false; … … 3699 3614 3700 3615 // change settings 3701 return HRESULT_FROM_WIN32(g_pfnSetIpInterfaceEntry(&newRow)); 3702 } 3703 3704 3705 HRESULT vboxNetCfgWinGetInterfaceLUID(IN HKEY hKey, OUT NET_LUID* pLUID) 3706 { 3707 HRESULT res = S_OK; 3708 DWORD luidIndex = 0; 3709 DWORD ifType = 0; 3710 DWORD cbSize = sizeof(luidIndex); 3711 DWORD dwValueType = REG_DWORD; 3712 3616 NETIO_STATUS dwErr = g_pfnSetIpInterfaceEntry(&newRow); 3617 if (dwErr == NO_ERROR) 3618 return S_OK; 3619 return HRESULT_FROM_WIN32(dwErr); 3620 } 3621 3622 static HRESULT vboxNetCfgWinSetupMetric(IN NET_LUID* pLuid) 3623 { 3624 HRESULT hrc = E_FAIL; 3625 HMODULE hmod = loadSystemDll(L"Iphlpapi.dll"); 3626 if (hmod) 3627 { 3628 g_pfnInitializeIpInterfaceEntry = (PFNINITIALIZEIPINTERFACEENTRY)GetProcAddress(hmod, "InitializeIpInterfaceEntry"); 3629 g_pfnGetIpInterfaceEntry = (PFNGETIPINTERFACEENTRY) GetProcAddress(hmod, "GetIpInterfaceEntry"); 3630 g_pfnSetIpInterfaceEntry = (PFNSETIPINTERFACEENTRY) GetProcAddress(hmod, "SetIpInterfaceEntry"); 3631 Assert(g_pfnInitializeIpInterfaceEntry); 3632 Assert(g_pfnGetIpInterfaceEntry); 3633 Assert(g_pfnSetIpInterfaceEntry); 3634 3635 if ( g_pfnInitializeIpInterfaceEntry 3636 && g_pfnGetIpInterfaceEntry 3637 && g_pfnSetIpInterfaceEntry) 3638 { 3639 DWORD loopbackMetric = 0; 3640 hrc = vboxNetCfgWinGetLoopbackMetric(&loopbackMetric); 3641 if (SUCCEEDED(hrc)) 3642 hrc = vboxNetCfgWinSetInterfaceMetric(pLuid, loopbackMetric - 1); 3643 } 3644 3645 g_pfnInitializeIpInterfaceEntry = NULL; 3646 g_pfnSetIpInterfaceEntry = NULL; 3647 g_pfnGetIpInterfaceEntry = NULL; 3648 3649 FreeLibrary(hmod); 3650 } 3651 return hrc; 3652 } 3653 3654 static HRESULT vboxNetCfgWinGetInterfaceLUID(IN HKEY hKey, OUT NET_LUID *pLUID) 3655 { 3713 3656 if (pLUID == NULL) 3714 3657 return E_INVALIDARG; 3715 3658 3716 res = RegQueryValueExW(hKey, L"NetLuidIndex", NULL, 3717 &dwValueType, (LPBYTE)&luidIndex, &cbSize); 3718 if (res != 0) 3719 return HRESULT_FROM_WIN32(res); 3720 3721 cbSize = sizeof(ifType); 3722 dwValueType = REG_DWORD; 3723 res = RegQueryValueExW(hKey, L"*IfType", NULL, 3724 &dwValueType, (LPBYTE)&ifType, &cbSize); 3725 if (res != 0) 3726 return HRESULT_FROM_WIN32(res); 3727 3728 ZeroMemory(pLUID, sizeof(NET_LUID)); 3729 pLUID->Info.IfType = ifType; 3730 pLUID->Info.NetLuidIndex = luidIndex; 3731 3732 return res; 3733 } 3734 3735 3736 HRESULT vboxNetCfgWinSetupMetric(IN NET_LUID* pLuid) 3737 { 3738 HINSTANCE hModule = NULL; 3739 HRESULT rc = vboxLoadIpHelpFunctions(hModule); 3740 if (SUCCEEDED(rc)) 3741 { 3742 int loopbackMetric; 3743 rc = vboxNetCfgWinGetLoopbackMetric(&loopbackMetric); 3744 if (SUCCEEDED(rc)) 3745 rc = vboxNetCfgWinSetInterfaceMetric(pLuid, loopbackMetric - 1); 3746 } 3747 3748 g_pfnInitializeIpInterfaceEntry = NULL; 3749 g_pfnSetIpInterfaceEntry = NULL; 3750 g_pfnGetIpInterfaceEntry = NULL; 3751 3752 FreeLibrary(hModule); 3753 return rc; 3754 } 3659 DWORD dwLuidIndex = 0; 3660 DWORD cbSize = sizeof(dwLuidIndex); 3661 DWORD dwValueType = REG_DWORD; /** @todo r=bird: This is output only. No checked after the call. So, only for debugging? */ 3662 LSTATUS lrc = RegQueryValueExW(hKey, L"NetLuidIndex", NULL, &dwValueType, (LPBYTE)&dwLuidIndex, &cbSize); 3663 if (lrc == ERROR_SUCCESS) 3664 { 3665 DWORD dwIfType = 0; 3666 cbSize = sizeof(dwIfType); 3667 dwValueType = REG_DWORD; /** @todo r=bird: This is output only. No checked after the call. So, only for debugging? */ 3668 lrc = RegQueryValueExW(hKey, L"*IfType", NULL, &dwValueType, (LPBYTE)&dwIfType, &cbSize); 3669 if (lrc == ERROR_SUCCESS) 3670 { 3671 RT_ZERO(*pLUID); 3672 pLUID->Info.IfType = dwIfType; 3673 pLUID->Info.NetLuidIndex = dwLuidIndex; 3674 return S_OK; 3675 } 3676 } 3677 3678 RT_ZERO(*pLUID); 3679 return HRESULT_FROM_WIN32(lrc); 3680 } 3681 3682 3755 3683 #ifdef VBOXNETCFG_DELAYEDRENAME 3756 3684 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinRenameHostOnlyConnection(IN const GUID *pGuid, IN LPCWSTR pwszId, OUT BSTR *pDevName) 3757 3685 { 3758 HRESULT hr = S_OK; 3759 WCHAR wszDevName[256]; 3760 WCHAR wszConnectionNewName[128]; 3761 ULONG cbName = sizeof(wszConnectionNewName); 3762 3686 if (pDevName) 3687 *pDevName = NULL; 3688 3689 HRESULT hr = S_OK; /** @todo r=bird: ODD return status for SetupDiCreateDeviceInfoList failures! */ 3763 3690 HDEVINFO hDevInfo = SetupDiCreateDeviceInfoList(&GUID_DEVCLASS_NET, NULL); 3764 3691 if (hDevInfo != INVALID_HANDLE_VALUE) 3765 3692 { 3766 SP_DEVINFO_DATA DevInfoData; 3767 3768 DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA); 3693 SP_DEVINFO_DATA DevInfoData = { sizeof(SP_DEVINFO_DATA) }; 3769 3694 if (SetupDiOpenDeviceInfo(hDevInfo, pwszId, NULL, 0, &DevInfoData)) 3770 3695 { 3696 WCHAR wszDevName[256 + 1] = {0}; 3771 3697 DWORD err = ERROR_SUCCESS; 3772 if (!SetupDiGetDeviceRegistryPropertyW(hDevInfo, &DevInfoData, 3773 SPDRP_FRIENDLYNAME, NULL,3774 (PBYTE)wszDevName, RT_ELEMENTS(wszDevName), NULL))3698 if (!SetupDiGetDeviceRegistryPropertyW(hDevInfo, &DevInfoData, SPDRP_FRIENDLYNAME, NULL /*PropertyRegDataType*/, 3699 (PBYTE)wszDevName, sizeof(wszDevName) - sizeof(WCHAR) /*yes, bytes*/, 3700 NULL /*RequiredSize*/)) 3775 3701 { 3776 3702 err = GetLastError(); 3777 3703 if (err == ERROR_INVALID_DATA) 3778 3704 { 3779 err = SetupDiGetDeviceRegistryPropertyW(hDevInfo, &DevInfoData, 3780 SPDRP_DEVICEDESC, NULL, 3781 (PBYTE)wszDevName, RT_ELEMENTS(wszDevName), NULL) 3782 ? ERROR_SUCCESS 3783 : GetLastError(); 3705 RT_ZERO(wszDevName); 3706 if (SetupDiGetDeviceRegistryPropertyW(hDevInfo, &DevInfoData, SPDRP_DEVICEDESC, NULL /*PropertyRegDataType*/, 3707 (PBYTE)wszDevName, sizeof(wszDevName) - sizeof(WCHAR) /*yes, bytes*/, 3708 NULL /*RequiredSize*/)) 3709 err = ERROR_SUCCESS; 3710 else 3711 err = GetLastError(); 3784 3712 } 3785 3713 } 3786 3714 if (err == ERROR_SUCCESS) 3787 3715 { 3788 hr = VBoxNetCfgWinGenHostonlyConnectionName(wszDevName, wszConnectionNewName, &cbName); 3716 WCHAR wszConnectionNewName[128] = {0}; 3717 hr = VBoxNetCfgWinGenHostonlyConnectionName(wszDevName, wszConnectionNewName, 3718 RT_ELEMENTS(wszConnectionNewName), NULL); 3789 3719 if (SUCCEEDED(hr)) 3790 3720 { -
trunk/src/VBox/HostDrivers/VBoxNetFlt/win/nobj/VBoxNetFltNobj.cpp
r96407 r96572 2 2 /** @file 3 3 * VBoxNetFltNobj.cpp - Notify Object for Bridged Networking Driver. 4 * 4 5 * Used to filter Bridged Networking Driver bindings 5 6 */ 7 6 8 /* 7 9 * Copyright (C) 2011-2022 Oracle and/or its affiliates. … … 34 36 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 35 37 */ 38 39 40 /********************************************************************************************************************************* 41 * Header Files * 42 *********************************************************************************************************************************/ 36 43 #include "VBoxNetFltNobj.h" 37 44 #include <iprt/win/ntddndis.h> 38 #include <assert.h> 39 #include <stdio.h> 45 #include <iprt/win/windows.h> 46 #include <winreg.h> 47 #include <Olectl.h> 40 48 41 49 #include <VBoxNetFltNobjT_i.c> 42 50 43 #include <Olectl.h> 44 51 #include <iprt/assert.h> 52 #include <iprt/utf16.h> 53 #include <iprt/string.h> 54 55 56 /********************************************************************************************************************************* 57 * Defined Constants And Macros * 58 *********************************************************************************************************************************/ 45 59 //# define VBOXNETFLTNOTIFY_DEBUG_BIND 46 60 47 61 #ifdef DEBUG 48 # define NonStandardAssert(a) assert(a)49 # define NonStandardAssertBreakpoint() assert(0)62 # define NonStandardAssert(a) Assert(a) 63 # define NonStandardAssertBreakpoint() AssertFailed() 50 64 #else 51 65 # define NonStandardAssert(a) do{}while (0) … … 53 67 #endif 54 68 55 VBoxNetFltNobj::VBoxNetFltNobj() : 56 mpNetCfg(NULL), 57 mpNetCfgComponent(NULL), 58 mbInstalling(FALSE) 69 70 /********************************************************************************************************************************* 71 * Global Variables * 72 *********************************************************************************************************************************/ 73 static HMODULE g_hModSelf = (HMODULE)~(uintptr_t)0; 74 75 76 VBoxNetFltNobj::VBoxNetFltNobj() 77 : mpNetCfg(NULL) 78 , mpNetCfgComponent(NULL) 79 , mbInstalling(FALSE) 59 80 { 60 81 } … … 126 147 static HRESULT vboxNetFltWinQueryInstanceKey(IN INetCfgComponent *pComponent, OUT PHKEY phKey) 127 148 { 128 LPWSTR pPnpId; 129 HRESULT hr = pComponent->GetPnpDevNodeId(&pPnpId); 130 if (hr == S_OK) 131 { 132 WCHAR KeyName[MAX_PATH]; 133 wcscpy(KeyName, L"SYSTEM\\CurrentControlSet\\Enum\\"); 134 wcscat(KeyName,pPnpId); 135 136 LONG winEr = RegOpenKeyExW(HKEY_LOCAL_MACHINE, KeyName, 137 0, /*__reserved DWORD ulOptions*/ 138 KEY_READ, /*__in REGSAM samDesired*/ 139 phKey); 140 141 if (winEr != ERROR_SUCCESS) 142 { 143 hr = HRESULT_FROM_WIN32(winEr); 149 LPWSTR pwszPnpId; 150 HRESULT hrc = pComponent->GetPnpDevNodeId(&pwszPnpId); 151 if (hrc == S_OK) 152 { 153 WCHAR wszKeyName[MAX_PATH]; 154 RTUtf16Copy(wszKeyName, MAX_PATH, L"SYSTEM\\CurrentControlSet\\Enum\\"); 155 int rc = RTUtf16Cat(wszKeyName, MAX_PATH, pwszPnpId); 156 if (RT_SUCCESS(rc)) 157 { 158 LSTATUS lrc = RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszKeyName, 0 /*ulOptions*/, KEY_READ, phKey); 159 if (lrc != ERROR_SUCCESS) 160 { 161 hrc = HRESULT_FROM_WIN32(lrc); 162 NonStandardAssertBreakpoint(); 163 } 164 } 165 else 166 AssertRCStmt(rc, hrc = ERROR_BUFFER_OVERFLOW); 167 168 CoTaskMemFree(pwszPnpId); 169 } 170 else 171 NonStandardAssertBreakpoint(); 172 return hrc; 173 } 174 175 static HRESULT vboxNetFltWinQueryDriverKey(IN HKEY InstanceKey, OUT PHKEY phKey) 176 { 177 HRESULT hrc = S_OK; 178 179 WCHAR wszValue[MAX_PATH]; 180 DWORD cbValue = sizeof(wszValue) - sizeof(WCHAR); 181 DWORD dwType = REG_SZ; 182 LSTATUS lrc = RegQueryValueExW(InstanceKey, L"Driver", NULL /*lpReserved*/, &dwType, (LPBYTE)wszValue, &cbValue); 183 if (lrc == ERROR_SUCCESS) 184 { 185 if (dwType == REG_SZ) 186 { 187 wszValue[RT_ELEMENTS(wszValue) - 1] = '\0'; /* registry strings does not need to be zero terminated. */ 188 189 WCHAR wszKeyName[MAX_PATH]; 190 RTUtf16Copy(wszKeyName, MAX_PATH, L"SYSTEM\\CurrentControlSet\\Control\\Class\\"); 191 int rc = RTUtf16Cat(wszKeyName, MAX_PATH, wszValue); 192 if (RT_SUCCESS(rc)) 193 { 194 lrc = RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszKeyName, 0 /*ulOptions*/, KEY_READ, phKey); 195 if (lrc != ERROR_SUCCESS) 196 { 197 hrc = HRESULT_FROM_WIN32(lrc); 198 NonStandardAssertBreakpoint(); 199 } 200 } 201 else 202 AssertRCStmt(rc, hrc = ERROR_BUFFER_OVERFLOW); 203 } 204 else 205 { 206 hrc = HRESULT_FROM_WIN32(ERROR_DATATYPE_MISMATCH); 144 207 NonStandardAssertBreakpoint(); 145 208 } 146 147 CoTaskMemFree(pPnpId);148 209 } 149 210 else 150 211 { 151 NonStandardAssertBreakpoint(); 152 } 153 154 return hr; 155 } 156 157 static HRESULT vboxNetFltWinQueryDriverKey(IN HKEY InstanceKey, OUT PHKEY phKey) 158 { 159 DWORD Type = REG_SZ; 160 WCHAR Value[MAX_PATH]; 161 DWORD cbValue = sizeof(Value); 162 HRESULT hr = S_OK; 163 LONG winEr = RegQueryValueExW(InstanceKey, 164 L"Driver", /*__in_opt LPCTSTR lpValueName*/ 165 0, /*__reserved LPDWORD lpReserved*/ 166 &Type, /*__out_opt LPDWORD lpType*/ 167 (LPBYTE)Value, /*__out_opt LPBYTE lpData*/ 168 &cbValue/*__inout_opt LPDWORD lpcbData*/ 169 ); 170 171 if (winEr == ERROR_SUCCESS) 172 { 173 WCHAR KeyName[MAX_PATH]; 174 wcscpy(KeyName, L"SYSTEM\\CurrentControlSet\\Control\\Class\\"); 175 wcscat(KeyName,Value); 176 177 winEr = RegOpenKeyExW(HKEY_LOCAL_MACHINE, KeyName, 178 0, /*__reserved DWORD ulOptions*/ 179 KEY_READ, /*__in REGSAM samDesired*/ 180 phKey); 181 182 if (winEr != ERROR_SUCCESS) 183 { 184 hr = HRESULT_FROM_WIN32(winEr); 212 hrc = HRESULT_FROM_WIN32(lrc); 213 NonStandardAssertBreakpoint(); 214 } 215 216 return hrc; 217 } 218 219 static HRESULT vboxNetFltWinQueryDriverKey(IN INetCfgComponent *pComponent, OUT PHKEY phKey) 220 { 221 HKEY hKeyInstance = NULL; 222 HRESULT hrc = vboxNetFltWinQueryInstanceKey(pComponent, &hKeyInstance); 223 if (hrc == S_OK) 224 { 225 hrc = vboxNetFltWinQueryDriverKey(hKeyInstance, phKey); 226 if (hrc != S_OK) 185 227 NonStandardAssertBreakpoint(); 186 }228 RegCloseKey(hKeyInstance); 187 229 } 188 230 else 189 { 190 hr = HRESULT_FROM_WIN32(winEr); 191 NonStandardAssertBreakpoint(); 192 } 193 194 return hr; 195 } 196 197 static HRESULT vboxNetFltWinQueryDriverKey(IN INetCfgComponent *pComponent, OUT PHKEY phKey) 198 { 199 HKEY InstanceKey; 200 HRESULT hr = vboxNetFltWinQueryInstanceKey(pComponent, &InstanceKey); 201 if (hr == S_OK) 202 { 203 hr = vboxNetFltWinQueryDriverKey(InstanceKey, phKey); 204 if (hr != S_OK) 205 { 206 NonStandardAssertBreakpoint(); 207 } 208 RegCloseKey(InstanceKey); 231 NonStandardAssertBreakpoint(); 232 return hrc; 233 } 234 235 static HRESULT vboxNetFltWinNotifyCheckNetAdp(IN INetCfgComponent *pComponent, OUT bool *pfShouldBind) 236 { 237 *pfShouldBind = false; 238 239 LPWSTR pwszDevId = NULL; 240 HRESULT hrc = pComponent->GetId(&pwszDevId); 241 if (hrc == S_OK) 242 { 243 /** @todo r=bird: This was _wcsnicmp(pwszDevId, L"sun_VBoxNetAdp", sizeof(L"sun_VBoxNetAdp")/2)) 244 * which includes the terminator, so it translates to a full compare. Goes way back. */ 245 if (RTUtf16ICmpAscii(pwszDevId, "sun_VBoxNetAdp") == 0) 246 *pfShouldBind = false; 247 else 248 hrc = S_FALSE; 249 CoTaskMemFree(pwszDevId); 209 250 } 210 251 else 211 { 212 NonStandardAssertBreakpoint(); 213 } 214 215 return hr; 216 } 217 218 static HRESULT vboxNetFltWinNotifyCheckNetAdp(IN INetCfgComponent *pComponent, OUT bool * pbShouldBind) 219 { 220 HRESULT hr; 221 LPWSTR pDevId; 222 hr = pComponent->GetId(&pDevId); 223 if (hr == S_OK) 224 { 225 if (!_wcsnicmp(pDevId, L"sun_VBoxNetAdp", sizeof(L"sun_VBoxNetAdp")/2)) 226 { 227 *pbShouldBind = false; 228 } 229 else 230 { 231 hr = S_FALSE; 232 } 233 CoTaskMemFree(pDevId); 234 } 235 else 236 { 237 NonStandardAssertBreakpoint(); 238 } 239 240 return hr; 241 } 242 243 static HRESULT vboxNetFltWinNotifyCheckMsLoop(IN INetCfgComponent *pComponent, OUT bool * pbShouldBind) 244 { 245 HRESULT hr; 246 LPWSTR pDevId; 247 hr = pComponent->GetId(&pDevId); 248 if (hr == S_OK) 249 { 250 if (!_wcsnicmp(pDevId, L"*msloop", sizeof(L"*msloop")/2)) 252 NonStandardAssertBreakpoint(); 253 254 return hrc; 255 } 256 257 static HRESULT vboxNetFltWinNotifyCheckMsLoop(IN INetCfgComponent *pComponent, OUT bool *pfShouldBind) 258 { 259 *pfShouldBind = false; 260 261 LPWSTR pwszDevId = NULL; 262 HRESULT hrc = pComponent->GetId(&pwszDevId); 263 if (hrc == S_OK) 264 { 265 /** @todo r=bird: This was _wcsnicmp(pwszDevId, L"*msloop", sizeof(L"*msloop")/2) 266 * which includes the terminator, making it a full compare. Goes way back. */ 267 if (RTUtf16ICmpAscii(pwszDevId, "*msloop") == 0) 251 268 { 252 269 /* we need to detect the medium the adapter is presenting 253 270 * to do that we could examine in the registry the *msloop params */ 254 HKEY DriverKey;255 hr = vboxNetFltWinQueryDriverKey(pComponent, &DriverKey);256 if (hr == S_OK)271 HKEY hKeyDriver; 272 hrc = vboxNetFltWinQueryDriverKey(pComponent, &hKeyDriver); 273 if (hrc == S_OK) 257 274 { 258 DWORD Type = REG_SZ; 259 WCHAR Value[64]; /* 2 should be enough actually, paranoid check for extra spaces */ 260 DWORD cbValue = sizeof(Value); 261 LONG winEr = RegQueryValueExW(DriverKey, 262 L"Medium", /*__in_opt LPCTSTR lpValueName*/ 263 0, /*__reserved LPDWORD lpReserved*/ 264 &Type, /*__out_opt LPDWORD lpType*/ 265 (LPBYTE)Value, /*__out_opt LPBYTE lpData*/ 266 &cbValue/*__inout_opt LPDWORD lpcbData*/ 267 ); 268 if (winEr == ERROR_SUCCESS) 275 WCHAR wszValue[64]; /* 2 should be enough actually, paranoid check for extra spaces */ 276 DWORD cbValue = sizeof(wszValue) - sizeof(WCHAR); 277 DWORD dwType = REG_SZ; 278 LSTATUS lrc = RegQueryValueExW(hKeyDriver, L"Medium", NULL /*lpReserved*/, &dwType, (LPBYTE)wszValue, &cbValue); 279 if (lrc == ERROR_SUCCESS) 269 280 { 270 PWCHAR endPrt; 271 ULONG enmMedium = wcstoul(Value, 272 &endPrt, 273 0 /* base*/); 274 275 winEr = errno; 276 if (winEr == ERROR_SUCCESS) 281 if (dwType == REG_SZ) 277 282 { 278 if (enmMedium == 0) /* 0 is Ethernet */ 283 wszValue[RT_ELEMENTS(wszValue) - 1] = '\0'; 284 285 char szUtf8[256]; 286 char *pszUtf8 = szUtf8; 287 RTUtf16ToUtf8Ex(wszValue, RTSTR_MAX, &pszUtf8, sizeof(szUtf8), NULL); 288 pszUtf8 = RTStrStrip(pszUtf8); 289 290 uint64_t uValue = 0; 291 int rc = RTStrToUInt64Ex(pszUtf8, NULL, 0, &uValue); 292 if (RT_SUCCESS(rc)) 279 293 { 280 *pbShouldBind = true; 294 if (uValue == 0) /* 0 is Ethernet */ 295 *pfShouldBind = true; 296 else 297 *pfShouldBind = false; 281 298 } 282 299 else 283 300 { 284 *pbShouldBind = false; 301 NonStandardAssertBreakpoint(); 302 *pfShouldBind = true; 285 303 } 286 304 } 287 305 else 288 {289 306 NonStandardAssertBreakpoint(); 290 *pbShouldBind = true;291 }292 307 } 293 308 else … … 295 310 /** @todo we should check the default medium in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\<driver_id>\Ndi\Params\Medium, REG_SZ "Default" value */ 296 311 NonStandardAssertBreakpoint(); 297 *p bShouldBind = true;312 *pfShouldBind = true; 298 313 } 299 314 300 RegCloseKey( DriverKey);315 RegCloseKey(hKeyDriver); 301 316 } 302 317 else 318 NonStandardAssertBreakpoint(); 319 } 320 else 321 hrc = S_FALSE; 322 CoTaskMemFree(pwszDevId); 323 } 324 else 325 NonStandardAssertBreakpoint(); 326 return hrc; 327 } 328 329 static HRESULT vboxNetFltWinNotifyCheckLowerRange(IN INetCfgComponent *pComponent, OUT bool *pfShouldBind) 330 { 331 *pfShouldBind = false; 332 333 HKEY hKeyDriver = NULL; 334 HRESULT hrc = vboxNetFltWinQueryDriverKey(pComponent, &hKeyDriver); 335 if (hrc == S_OK) 336 { 337 HKEY hKeyInterfaces = NULL; 338 LSTATUS lrc = RegOpenKeyExW(hKeyDriver, L"Ndi\\Interfaces", 0 /*ulOptions*/, KEY_READ, &hKeyInterfaces); 339 if (lrc == ERROR_SUCCESS) 340 { 341 WCHAR wszValue[MAX_PATH]; 342 DWORD cbValue = sizeof(wszValue) - sizeof(WCHAR); 343 DWORD dwType = REG_SZ; 344 lrc = RegQueryValueExW(hKeyInterfaces, L"LowerRange", NULL /*lpReserved*/, &dwType, (LPBYTE)wszValue, &cbValue); 345 if (lrc == ERROR_SUCCESS) 303 346 { 304 NonStandardAssertBreakpoint(); 305 } 306 } 307 else 308 { 309 hr = S_FALSE; 310 } 311 CoTaskMemFree(pDevId); 312 } 313 else 314 { 315 NonStandardAssertBreakpoint(); 316 } 317 318 return hr; 319 } 320 321 static HRESULT vboxNetFltWinNotifyCheckLowerRange(IN INetCfgComponent *pComponent, OUT bool * pbShouldBind) 322 { 323 HKEY DriverKey; 324 HKEY InterfacesKey; 325 HRESULT hr = vboxNetFltWinQueryDriverKey(pComponent, &DriverKey); 326 if (hr == S_OK) 327 { 328 LONG winEr = RegOpenKeyExW(DriverKey, L"Ndi\\Interfaces", 329 0, /*__reserved DWORD ulOptions*/ 330 KEY_READ, /*__in REGSAM samDesired*/ 331 &InterfacesKey); 332 if (winEr == ERROR_SUCCESS) 333 { 334 DWORD Type = REG_SZ; 335 WCHAR Value[MAX_PATH]; 336 DWORD cbValue = sizeof(Value); 337 winEr = RegQueryValueExW(InterfacesKey, 338 L"LowerRange", /*__in_opt LPCTSTR lpValueName*/ 339 0, /*__reserved LPDWORD lpReserved*/ 340 &Type, /*__out_opt LPDWORD lpType*/ 341 (LPBYTE)Value, /*__out_opt LPBYTE lpData*/ 342 &cbValue/*__inout_opt LPDWORD lpcbData*/ 343 ); 344 if (winEr == ERROR_SUCCESS) 345 { 346 if (wcsstr(Value,L"ethernet") || wcsstr(Value, L"wan")) 347 if (dwType == REG_SZ) 347 348 { 348 *pbShouldBind = true; 349 } 350 else 351 { 352 *pbShouldBind = false; 349 if (RTUtf16FindAscii(wszValue, "ethernet") >= 0 || RTUtf16FindAscii(wszValue, "wan") >= 0) 350 *pfShouldBind = true; 351 else 352 *pfShouldBind = false; 353 353 } 354 354 } … … 356 356 { 357 357 /* do not set err status to it */ 358 *p bShouldBind = false;358 *pfShouldBind = false; 359 359 NonStandardAssertBreakpoint(); 360 360 } 361 361 362 RegCloseKey( InterfacesKey);362 RegCloseKey(hKeyInterfaces); 363 363 } 364 364 else 365 365 { 366 hr = HRESULT_FROM_WIN32(winEr);366 hrc = HRESULT_FROM_WIN32(lrc); 367 367 NonStandardAssertBreakpoint(); 368 368 } 369 369 370 RegCloseKey( DriverKey);370 RegCloseKey(hKeyDriver); 371 371 } 372 372 else 373 { 374 NonStandardAssertBreakpoint(); 375 } 376 377 return hr; 378 } 379 380 static HRESULT vboxNetFltWinNotifyShouldBind(IN INetCfgComponent *pComponent, OUT bool *pbShouldBind) 381 { 382 DWORD fCharacteristics; 383 HRESULT hr; 384 385 do 386 { 387 /* filter out only physical adapters */ 388 hr = pComponent->GetCharacteristics(&fCharacteristics); 389 if (hr != S_OK) 390 { 391 NonStandardAssertBreakpoint(); 392 break; 393 } 394 395 396 if (fCharacteristics & NCF_HIDDEN) 397 { 398 /* we are not binding to hidden adapters */ 399 *pbShouldBind = false; 400 break; 401 } 402 403 hr = vboxNetFltWinNotifyCheckMsLoop(pComponent, pbShouldBind); 404 if (hr == S_OK) 405 { 406 /* this is a loopback adapter, 407 * the pbShouldBind already contains the result */ 408 break; 409 } 410 else if (hr != S_FALSE) 411 { 412 /* error occurred */ 413 break; 414 } 415 416 hr = vboxNetFltWinNotifyCheckNetAdp(pComponent, pbShouldBind); 417 if (hr == S_OK) 418 { 419 /* this is a VBoxNetAdp adapter, 420 * the pbShouldBind already contains the result */ 421 break; 422 } 423 else if (hr != S_FALSE) 424 { 425 /* error occurred */ 426 break; 427 } 428 429 /* hr == S_FALSE means this is not a loopback adpater, set it to S_OK */ 430 hr = S_OK; 431 432 // if (!(fCharacteristics & NCF_PHYSICAL)) 433 // { 434 // /* we are binding to physical adapters only */ 435 // *pbShouldBind = false; 436 // break; 437 // } 438 439 hr = vboxNetFltWinNotifyCheckLowerRange(pComponent, pbShouldBind); 440 if (hr == S_OK) 441 { 442 /* the vboxNetFltWinNotifyCheckLowerRange ccucceeded, 443 * the pbShouldBind already contains the result */ 444 break; 445 } 446 /* we are here because of the fail, nothing else to do */ 447 } while (0); 448 449 return hr; 450 } 451 452 453 static HRESULT vboxNetFltWinNotifyShouldBind(IN INetCfgBindingInterface *pIf, OUT bool *pbShouldBind) 454 { 455 INetCfgComponent * pAdapterComponent; 456 HRESULT hr = pIf->GetLowerComponent(&pAdapterComponent); 457 if (hr == S_OK) 458 { 459 hr = vboxNetFltWinNotifyShouldBind(pAdapterComponent, pbShouldBind); 373 NonStandardAssertBreakpoint(); 374 return hrc; 375 } 376 377 static HRESULT vboxNetFltWinNotifyShouldBind(IN INetCfgComponent *pComponent, OUT bool *pfShouldBind) 378 { 379 *pfShouldBind = false; 380 381 /* filter out only physical adapters */ 382 DWORD fCharacteristics = 0; 383 HRESULT hrc = pComponent->GetCharacteristics(&fCharacteristics); 384 if (hrc != S_OK) 385 { 386 NonStandardAssertBreakpoint(); 387 return hrc; 388 } 389 390 /* we are not binding to hidden adapters */ 391 if (fCharacteristics & NCF_HIDDEN) 392 return S_OK; 393 394 hrc = vboxNetFltWinNotifyCheckMsLoop(pComponent, pfShouldBind); 395 if ( hrc == S_OK /* this is a loopback adapter, the pfShouldBind already contains the result */ 396 || hrc != S_FALSE /* error occurred */) 397 return hrc; 398 399 hrc = vboxNetFltWinNotifyCheckNetAdp(pComponent, pfShouldBind); 400 if ( hrc == S_OK /* this is a VBoxNetAdp adapter, the pfShouldBind already contains the result */ 401 || hrc != S_FALSE /* error occurred */) 402 return hrc; 403 404 //if (!(fCharacteristics & NCF_PHYSICAL)) 405 //{ 406 // *pfShouldBind = false; /* we are binding to physical adapters only */ 407 // return S_OK; 408 //} 409 410 return vboxNetFltWinNotifyCheckLowerRange(pComponent, pfShouldBind); 411 } 412 413 414 static HRESULT vboxNetFltWinNotifyShouldBind(IN INetCfgBindingInterface *pIf, OUT bool *pfShouldBind) 415 { 416 INetCfgComponent *pAdapterComponent = NULL; 417 HRESULT hrc = pIf->GetLowerComponent(&pAdapterComponent); 418 if (hrc == S_OK) 419 { 420 hrc = vboxNetFltWinNotifyShouldBind(pAdapterComponent, pfShouldBind); 460 421 461 422 pAdapterComponent->Release(); … … 464 425 { 465 426 NonStandardAssertBreakpoint(); 466 }467 468 return hr ;469 } 470 471 static HRESULT vboxNetFltWinNotifyShouldBind(IN INetCfgBindingPath *pPath, OUT bool *p bDoBind)472 { 473 IEnumNetCfgBindingInterface *pEnumBindingIf;474 HRESULT hr = pPath->EnumBindingInterfaces(&pEnumBindingIf); 475 if (hr == S_OK)476 {477 hr = pEnumBindingIf->Reset();478 if (hr == S_OK)479 {480 ULONG ulCount;481 INetCfgBindingInterface *pBindingIf;482 do427 *pfShouldBind = false; 428 } 429 return hrc; 430 } 431 432 static HRESULT vboxNetFltWinNotifyShouldBind(IN INetCfgBindingPath *pPath, OUT bool *pfShouldBind) 433 { 434 *pfShouldBind = false; 435 436 IEnumNetCfgBindingInterface *pIEnumBinding = NULL; 437 HRESULT hrc = pPath->EnumBindingInterfaces(&pIEnumBinding); 438 if (hrc == S_OK) 439 { 440 hrc = pIEnumBinding->Reset(); 441 if (hrc == S_OK) 442 { 443 for (;;) 483 444 { 484 hr = pEnumBindingIf->Next(1, &pBindingIf, &ulCount); 485 if (hr == S_OK) 445 ULONG uCount = 0; 446 INetCfgBindingInterface *pIBinding = NULL; 447 hrc = pIEnumBinding->Next(1, &pIBinding, &uCount); 448 if (hrc == S_OK) 486 449 { 487 hr = vboxNetFltWinNotifyShouldBind(pBindingIf, pbDoBind); 488 489 pBindingIf->Release(); 490 491 if (hr == S_OK) 492 { 493 if (!(*pbDoBind)) 494 { 495 break; 496 } 497 } 498 else 499 { 500 /* break on failure */ 450 hrc = vboxNetFltWinNotifyShouldBind(pIBinding, pfShouldBind); 451 pIBinding->Release(); 452 453 if (hrc != S_OK) 454 break; /* break on failure. */ 455 if (!*pfShouldBind) 501 456 break; 502 }503 457 } 504 else if (hr == S_FALSE)458 else if (hrc == S_FALSE) 505 459 { 506 460 /* no more elements */ 507 hr = S_OK;461 hrc = S_OK; 508 462 break; 509 463 } … … 514 468 break; 515 469 } 516 } while (true);470 } 517 471 } 518 472 else 519 {520 473 NonStandardAssertBreakpoint(); 521 } 522 523 pEnumBindingIf->Release(); 474 475 pIEnumBinding->Release(); 524 476 } 525 477 else 526 { 527 NonStandardAssertBreakpoint(); 528 } 529 530 return hr; 478 NonStandardAssertBreakpoint(); 479 return hrc; 531 480 } 532 481 … … 536 485 return VBOXNETFLTNOTIFY_DEBUG_BIND; 537 486 #else 538 bool bShouldBind; 539 HRESULT hr = vboxNetFltWinNotifyShouldBind(pPath, &bShouldBind) ; 540 if (hr != S_OK) 541 { 542 bShouldBind = VBOXNETFLTNOTIFY_ONFAIL_BINDDEFAULT; 543 } 544 545 return bShouldBind; 487 bool fShouldBind; 488 HRESULT hrc = vboxNetFltWinNotifyShouldBind(pPath, &fShouldBind); 489 if (hrc != S_OK) 490 fShouldBind = VBOXNETFLTNOTIFY_ONFAIL_BINDDEFAULT; 491 492 return fShouldBind; 546 493 #endif 547 494 } … … 571 518 END_OBJECT_MAP() 572 519 520 573 521 extern "C" 574 522 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/) … … 576 524 if (dwReason == DLL_PROCESS_ATTACH) 577 525 { 526 g_hModSelf = (HMODULE)hInstance; 527 578 528 _Module.Init(ObjectMap, hInstance); 579 529 DisableThreadLibraryCalls(hInstance); … … 586 536 } 587 537 588 STDAPI DllCanUnloadNow( )589 { 590 return (_Module.GetLockCount() == 0)? S_OK : S_FALSE;591 } 592 593 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)538 STDAPI DllCanUnloadNow(void) 539 { 540 return _Module.GetLockCount() == 0 ? S_OK : S_FALSE; 541 } 542 543 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv) 594 544 { 595 545 return _Module.GetClassObject(rclsid, riid, ppv); 596 546 } 547 597 548 598 549 /* … … 602 553 */ 603 554 555 #ifdef RT_EXCEPTIONS_ENABLED 604 556 /* Someday we may want to log errors. */ 605 557 class AdHocRegError … … 608 560 AdHocRegError(LSTATUS rc) { RT_NOREF1(rc); }; 609 561 }; 610 611 /* A simple wrapper on Windows registry functions. */ 562 #endif 563 564 /** 565 * A simple wrapper on Windows registry functions. 566 */ 612 567 class AdHocRegKey 613 568 { … … 617 572 ~AdHocRegKey() { RegCloseKey(m_hKey); }; 618 573 619 AdHocRegKey *create(LPCWSTR pcwszSubkey, LPCWSTR pcwszDefaultValue = NULL); 620 void remove(LPCWSTR pcwszSubkey); 621 void setValue(LPCWSTR pcwszName, LPCWSTR pcwszValue); 574 AdHocRegKey *create(LPCWSTR pcwszSubkey); 575 LSTATUS setValue(LPCWSTR pcwszName, LPCWSTR pcwszValue); 622 576 HKEY getKey(void) { return m_hKey; }; 623 577 private: … … 625 579 }; 626 580 627 AdHocRegKey::AdHocRegKey(LPCWSTR pcwszName, HKEY hParent) 581 AdHocRegKey::AdHocRegKey(LPCWSTR pcwszName, HKEY hParent) : m_hKey(NULL) 628 582 { 629 583 LSTATUS rc = RegOpenKeyExW(hParent, pcwszName, 0, KEY_ALL_ACCESS, &m_hKey); 630 584 if (rc != ERROR_SUCCESS) 585 #ifdef RT_EXCEPTIONS_ENABLED 631 586 throw AdHocRegError(rc); 632 } 633 634 void AdHocRegKey::remove(LPCWSTR pcwszSubkey) 635 { 636 LSTATUS rc; 637 WCHAR wszName[256]; 638 DWORD dwName; 639 640 /* Remove all subkeys of subkey first */ 641 AdHocRegKey *subkey = new AdHocRegKey(pcwszSubkey, m_hKey); 642 for (;;) 643 { 644 /* Always ask for the first subkey, because we remove it before calling RegEnumKeyEx again */ 645 dwName = 255; 646 rc = RegEnumKeyExW(subkey->getKey(), 0, wszName, &dwName, NULL, NULL, NULL, NULL); 647 if (rc != ERROR_SUCCESS) 648 break; 649 subkey->remove(wszName); 650 } 651 delete subkey; 652 653 /* Remove the subkey itself */ 654 rc = RegDeleteKeyW(m_hKey, pcwszSubkey); 655 if (rc != ERROR_SUCCESS) 656 throw AdHocRegError(rc); 657 } 658 659 AdHocRegKey *AdHocRegKey::create(LPCWSTR pcwszSubkey, LPCWSTR pcwszDefaultValue) 587 #else 588 m_hKey = NULL; 589 #endif 590 } 591 592 AdHocRegKey *AdHocRegKey::create(LPCWSTR pcwszSubkey) 660 593 { 661 594 HKEY hSubkey; … … 664 597 KEY_ALL_ACCESS, NULL /*pSecAttr*/, &hSubkey, NULL /*pdwDisposition*/); 665 598 if (rc != ERROR_SUCCESS) 599 #ifdef RT_EXCEPTIONS_ENABLED 666 600 throw AdHocRegError(rc); 601 #else 602 return NULL; 603 #endif 667 604 AdHocRegKey *pSubkey = new AdHocRegKey(hSubkey); 668 if ( pcwszDefaultValue)669 pSubkey->setValue(NULL, pcwszDefaultValue);605 if (!pSubkey) 606 RegCloseKey(hSubkey); 670 607 return pSubkey; 671 608 } 672 609 673 voidAdHocRegKey::setValue(LPCWSTR pcwszName, LPCWSTR pcwszValue)610 LSTATUS AdHocRegKey::setValue(LPCWSTR pcwszName, LPCWSTR pcwszValue) 674 611 { 675 612 LSTATUS rc = RegSetValueExW(m_hKey, pcwszName, 0, REG_SZ, (const BYTE *)pcwszValue, 676 (DWORD)((wcslen(pcwszValue) + 1) * sizeof(WCHAR))); 613 (DWORD)((RTUtf16Len(pcwszValue) + 1) * sizeof(WCHAR))); 614 #ifdef RT_EXCEPTIONS_ENABLED 677 615 if (rc != ERROR_SUCCESS) 678 616 throw AdHocRegError(rc); 679 } 680 681 /* 617 #endif 618 return rc; 619 } 620 621 /** 682 622 * Auxiliary class that facilitates automatic destruction of AdHocRegKey objects 683 623 * allocated in heap. No reference counting here! … … 687 627 public: 688 628 AdHocRegKeyPtr(AdHocRegKey *pKey) : m_pKey(pKey) {}; 689 ~AdHocRegKeyPtr() { delete m_pKey; }; 690 691 AdHocRegKey *create(LPCWSTR pcwszSubkey, LPCWSTR pcwszDefaultValue = NULL) 692 { return m_pKey->create(pcwszSubkey, pcwszDefaultValue); }; 693 void remove(LPCWSTR pcwszSubkey) 694 { return m_pKey->remove(pcwszSubkey); }; 695 void setValue(LPCWSTR pcwszName, LPCWSTR pcwszValue) 696 { return m_pKey->setValue(pcwszName, pcwszValue); }; 629 ~AdHocRegKeyPtr() 630 { 631 if (m_pKey) 632 { 633 delete m_pKey; 634 m_pKey = NULL; 635 } 636 } 637 638 AdHocRegKey *create(LPCWSTR pcwszSubkey) 639 { return m_pKey ? m_pKey->create(pcwszSubkey) : NULL; }; 640 641 LSTATUS setValue(LPCWSTR pcwszName, LPCWSTR pcwszValue) 642 { return m_pKey ? m_pKey->setValue(pcwszName, pcwszValue) : ERROR_INVALID_STATE; }; 643 697 644 private: 698 645 AdHocRegKey *m_pKey; … … 703 650 704 651 705 STDAPI DllRegisterServer() 706 { 652 STDAPI DllRegisterServer(void) 653 { 654 /* Get the path to the DLL we're running inside. */ 707 655 WCHAR wszModule[MAX_PATH + 1]; 708 if (GetModuleFileNameW(GetModuleHandleW(L"VBoxNetFltNobj"), wszModule, MAX_PATH) == 0) 656 UINT cwcModule = GetModuleFileNameW(g_hModSelf, wszModule, MAX_PATH); 657 if (cwcModule == 0 || cwcModule > MAX_PATH) 709 658 return SELFREG_E_CLASS; 710 711 try { 659 wszModule[MAX_PATH] = '\0'; 660 661 /* 662 * Create registry keys and values. When exceptions are disabled, we depend 663 * on setValue() to propagate fail key creation failures. 664 */ 665 #ifdef RT_EXCEPTIONS_ENABLED 666 try 667 #endif 668 { 712 669 AdHocRegKey keyCLSID(L"CLSID"); 713 AdHocRegKeyPtr pkeyNobjClass(keyCLSID.create(L"{f374d1a0-bf08-4bdc-9cb2-c15ddaeef955}", 714 L"VirtualBox Bridged Networking Driver Notify Object v1.1")); 715 AdHocRegKeyPtr pkeyNobjSrv(pkeyNobjClass.create(L"InProcServer32", wszModule)); 716 pkeyNobjSrv.setValue(L"ThreadingModel", L"Both"); 717 } 718 catch (AdHocRegError) 719 { 720 return SELFREG_E_CLASS; 721 } 722 723 try { 670 AdHocRegKeyPtr pkeyNobjClass(keyCLSID.create(L"{f374d1a0-bf08-4bdc-9cb2-c15ddaeef955}")); 671 LSTATUS lrc = pkeyNobjClass.setValue(NULL, L"VirtualBox Bridged Networking Driver Notify Object v1.1"); 672 if (lrc != ERROR_SUCCESS) 673 return SELFREG_E_CLASS; 674 675 AdHocRegKeyPtr pkeyNobjSrv(pkeyNobjClass.create(L"InProcServer32")); 676 lrc = pkeyNobjSrv.setValue(NULL, wszModule); 677 if (lrc != ERROR_SUCCESS) 678 return SELFREG_E_CLASS; 679 lrc = pkeyNobjSrv.setValue(L"ThreadingModel", L"Both"); 680 if (lrc != ERROR_SUCCESS) 681 return SELFREG_E_CLASS; 682 } 683 #ifdef RT_EXCEPTIONS_ENABLED 684 catch (AdHocRegError) { return SELFREG_E_CLASS; } 685 #endif 686 687 #ifdef RT_EXCEPTIONS_ENABLED 688 try 689 #endif 690 { 724 691 AdHocRegKey keyTypeLib(L"TypeLib"); 725 AdHocRegKeyPtr pkeyNobjLib(keyTypeLib.create(L"{2A0C94D1-40E1-439C-8FE8-24107CAB0840}\\1.1", 726 L"VirtualBox Bridged Networking Driver Notify Object v1.1 Type Library")); 727 AdHocRegKeyPtr pkeyNobjLib0(pkeyNobjLib.create(L"0\\win64", wszModule)); 728 AdHocRegKeyPtr pkeyNobjLibFlags(pkeyNobjLib.create(L"FLAGS", L"0")); 692 AdHocRegKeyPtr pkeyNobjLib(keyTypeLib.create(L"{2A0C94D1-40E1-439C-8FE8-24107CAB0840}\\1.1")); 693 LSTATUS lrc = pkeyNobjLib.setValue(NULL, L"VirtualBox Bridged Networking Driver Notify Object v1.1 Type Library"); 694 if (lrc != ERROR_SUCCESS) 695 return SELFREG_E_TYPELIB; 696 697 AdHocRegKeyPtr pkeyNobjLib0(pkeyNobjLib.create(L"0\\win64")); 698 lrc = pkeyNobjLib0.setValue(NULL, wszModule); 699 if (lrc != ERROR_SUCCESS) 700 return SELFREG_E_TYPELIB; 701 AdHocRegKeyPtr pkeyNobjLibFlags(pkeyNobjLib.create(L"FLAGS")); 702 lrc = pkeyNobjLibFlags.setValue(NULL, L"0"); 703 if (lrc != ERROR_SUCCESS) 704 return SELFREG_E_TYPELIB; 705 729 706 if (GetSystemDirectoryW(wszModule, MAX_PATH) == 0) 730 707 return SELFREG_E_TYPELIB; 731 AdHocRegKeyPtr pkeyNobjLibHelpDir(pkeyNobjLib.create(L"HELPDIR", wszModule)); 732 } 733 catch (AdHocRegError) 734 { 735 return SELFREG_E_CLASS; 736 } 708 AdHocRegKeyPtr pkeyNobjLibHelpDir(pkeyNobjLib.create(L"HELPDIR")); 709 lrc = pkeyNobjLibHelpDir.setValue(NULL, wszModule); 710 if (lrc != ERROR_SUCCESS) 711 return SELFREG_E_TYPELIB; 712 } 713 #ifdef RT_EXCEPTIONS_ENABLED 714 catch (AdHocRegError) { return SELFREG_E_TYPELIB; } 715 #endif 737 716 738 717 return S_OK; 739 718 } 740 719 741 STDAPI DllUnregisterServer() 742 { 743 try { 744 AdHocRegKey keyTypeLib(L"TypeLib"); 745 keyTypeLib.remove(L"{2A0C94D1-40E1-439C-8FE8-24107CAB0840}"); 746 } 747 catch (AdHocRegError) { return SELFREG_E_TYPELIB; } 748 749 try { 750 AdHocRegKey keyCLSID(L"CLSID"); 751 keyCLSID.remove(L"{f374d1a0-bf08-4bdc-9cb2-c15ddaeef955}"); 752 } 753 catch (AdHocRegError) { return SELFREG_E_CLASS; } 720 721 STDAPI DllUnregisterServer(void) 722 { 723 static struct { HKEY hKeyRoot; wchar_t const *pwszParentKey; wchar_t const *pwszKeyToDelete; HRESULT hrcFail; } 724 s_aKeys[] = 725 { 726 { HKEY_CLASSES_ROOT, L"TypeLib", L"{2A0C94D1-40E1-439C-8FE8-24107CAB0840}", SELFREG_E_TYPELIB }, 727 { HKEY_CLASSES_ROOT, L"CLSID", L"{f374d1a0-bf08-4bdc-9cb2-c15ddaeef955}", SELFREG_E_CLASS }, 728 }; 729 730 HRESULT hrc = S_OK; 731 for (size_t i = 0; i < RT_ELEMENTS(s_aKeys); i++) 732 { 733 HKEY hKey = NULL; 734 LSTATUS lrc = RegOpenKeyExW(s_aKeys[i].hKeyRoot, s_aKeys[i].pwszParentKey, 0, KEY_ALL_ACCESS, &hKey); 735 if (lrc == ERROR_SUCCESS) 736 { 737 lrc = RegDeleteTreeW(hKey, s_aKeys[i].pwszKeyToDelete); /* Vista and later */ 738 RegCloseKey(hKey); 739 } 740 741 if (lrc != ERROR_SUCCESS && lrc != ERROR_FILE_NOT_FOUND && hrc == S_OK) 742 hrc = s_aKeys[i].hrcFail; 743 } 754 744 755 745 return S_OK; 756 746 } 747 -
trunk/src/VBox/HostDrivers/VBoxNetFlt/win/nobj/VBoxNetFltNobj.h
r96407 r96572 53 53 * Needed to make our driver bind to "real" host adapters only 54 54 */ 55 class ATL_NO_VTABLE VBoxNetFltNobj :56 public ATL::CComObjectRootEx<ATL::CComMultiThreadModel>,57 public ATL::CComCoClass<VBoxNetFltNobj, &CLSID_VBoxNetFltNobj>,58 public INetCfgComponentControl,59 public INetCfgComponentNotifyBinding55 class ATL_NO_VTABLE VBoxNetFltNobj 56 : public ATL::CComObjectRootEx<ATL::CComMultiThreadModel> 57 , public ATL::CComCoClass<VBoxNetFltNobj, &CLSID_VBoxNetFltNobj> 58 , public INetCfgComponentControl 59 , public INetCfgComponentNotifyBinding 60 60 { 61 61 public: -
trunk/src/VBox/HostDrivers/VBoxNetFlt/win/tools/VBoxNetAdpInstall.cpp
r96407 r96572 41 41 #include <VBox/VBoxNetCfg-win.h> 42 42 #include <VBox/VBoxDrvCfg-win.h> 43 #include <stdio.h>44 43 #include <devguid.h> 44 45 #include <iprt/initterm.h> 46 #include <iprt/message.h> 47 #include <iprt/process.h> 48 #include <iprt/stream.h> 45 49 46 50 … … 60 64 static DECLCALLBACK(void) winNetCfgLogger(const char *pszString) 61 65 { 62 printf("%s\n", pszString);66 RTMsgInfo("%s", pszString); 63 67 } 64 68 … … 109 113 static int VBoxNetAdpInstall(void) 110 114 { 111 VBoxNetCfgWinSetLogging(winNetCfgLogger); 112 113 HRESULT hr = CoInitialize(NULL); 114 if (SUCCEEDED(hr)) 115 { 116 wprintf(L"adding host-only interface..\n"); 117 115 RTMsgInfo("Adding host-only interface..."); 116 VBoxNetCfgWinSetLogging(winNetCfgLogger); 117 118 HRESULT hr = CoInitialize(NULL); 119 if (SUCCEEDED(hr)) 120 { 118 121 WCHAR wszInfFile[MAX_PATH]; 119 122 DWORD cwcInfFile = MyGetfullPathNameW(VBOX_NETADP_INF, RT_ELEMENTS(wszInfFile), wszInfFile); … … 129 132 130 133 if (hr == S_OK) 131 wprintf(L"installed successfully\n");134 RTMsgInfo("Installed successfully!"); 132 135 else 133 wprintf(L"error installing VBoxNetAdp (%#lx)\n", hr);136 RTMsgError("failed to install VBoxNetAdp: %Rhrc", hr); 134 137 135 138 VBoxNetCfgWinReleaseINetCfg(pnc, TRUE); 136 139 } 137 140 else 138 wprintf(L"VBoxNetCfgWinQueryINetCfg failed: hr=%#lx\n", hr);141 RTMsgError("VBoxNetCfgWinQueryINetCfg failed: %Rhrc", hr); 139 142 /* 140 143 hr = VBoxDrvCfgInfInstall(MpInf); … … 171 174 { 172 175 DWORD dwErr = GetLastError(); 173 wprintf(L"GetFullPathNameW failed: winEr = %lu\n", dwErr);176 RTMsgError("MyGetfullPathNameW failed: %Rwc", dwErr); 174 177 hr = HRESULT_FROM_WIN32(dwErr); 175 178 } … … 177 180 } 178 181 else 179 wprintf(L"Error initializing COM (%#lx)\n", hr);182 RTMsgError("Failed initializing COM: %Rhrc", hr); 180 183 181 184 VBoxNetCfgWinSetLogging(NULL); … … 186 189 static int VBoxNetAdpUninstall(void) 187 190 { 188 VBoxNetCfgWinSetLogging(winNetCfgLogger); 189 190 printf("uninstalling all host-only interfaces..\n"); 191 RTMsgInfo("Uninstalling all host-only interfaces..."); 192 VBoxNetCfgWinSetLogging(winNetCfgLogger); 191 193 192 194 HRESULT hr = CoInitialize(NULL); … … 198 200 hr = VBoxDrvCfgInfUninstallAllSetupDi(&GUID_DEVCLASS_NET, L"Net", VBOX_NETADP_HWID, 0/* could be SUOI_FORCEDELETE */); 199 201 if (SUCCEEDED(hr)) 200 printf("uninstallation successful\n");202 RTMsgInfo("Uninstallation successful!"); 201 203 else 202 printf("uninstalled successfully, but failed to remove infs\n");203 } 204 else 205 printf("uninstall failed, hr=%#lx\n", hr);206 CoUninitialize(); 207 } 208 else 209 printf("Error initializing COM (%#lx)\n", hr);204 RTMsgWarning("uninstalled successfully, but failed to remove infs (%Rhrc)\n", hr); 205 } 206 else 207 RTMsgError("uninstall failed: %Rhrc", hr); 208 CoUninitialize(); 209 } 210 else 211 RTMsgError("Failed initializing COM: %Rhrc", hr); 210 212 211 213 VBoxNetCfgWinSetLogging(NULL); … … 216 218 static int VBoxNetAdpUpdate(void) 217 219 { 218 VBoxNetCfgWinSetLogging(winNetCfgLogger); 219 220 printf("uninstalling all host-only interfaces..\n"); 220 RTMsgInfo("Uninstalling all host-only interfaces..."); 221 VBoxNetCfgWinSetLogging(winNetCfgLogger); 221 222 222 223 HRESULT hr = CoInitialize(NULL); … … 235 236 { 236 237 if (fRebootRequired) 237 printf("!!REBOOT REQUIRED!!\n");238 printf("updated successfully\n");239 } 240 else 241 printf("update failed, hr=%#lx\n", hr);242 243 CoUninitialize(); 244 } 245 else 246 printf("Error initializing COM (%#lx)\n", hr);238 RTMsgWarning("!!REBOOT REQUIRED!!"); 239 RTMsgInfo("Updated successfully!"); 240 } 241 else 242 RTMsgError("update failed: %Rhrc", hr); 243 244 CoUninitialize(); 245 } 246 else 247 RTMsgError("Failed initializing COM: %Rhrc", hr); 247 248 248 249 VBoxNetCfgWinSetLogging(NULL); … … 253 254 static int VBoxNetAdpDisable(void) 254 255 { 255 VBoxNetCfgWinSetLogging(winNetCfgLogger); 256 257 printf("disabling all host-only interfaces..\n"); 256 RTMsgInfo("Disabling all host-only interfaces..."); 257 VBoxNetCfgWinSetLogging(winNetCfgLogger); 258 258 259 259 HRESULT hr = CoInitialize(NULL); … … 262 262 hr = VBoxNetCfgWinPropChangeAllNetDevicesOfId(VBOX_NETADP_HWID, VBOXNECTFGWINPROPCHANGE_TYPE_DISABLE); 263 263 if (SUCCEEDED(hr)) 264 printf("disabling successful\n");265 else 266 printf("disable failed, hr=%#lx\n", hr);267 268 CoUninitialize(); 269 } 270 else 271 printf("Error initializing COM (%#lx)\n", hr);264 RTMsgInfo("Disabling successful"); 265 else 266 RTMsgError("disable failed: %Rhrc", hr); 267 268 CoUninitialize(); 269 } 270 else 271 RTMsgError("Failed initializing COM: %Rhrc", hr); 272 272 273 273 VBoxNetCfgWinSetLogging(NULL); … … 278 278 static int VBoxNetAdpEnable(void) 279 279 { 280 VBoxNetCfgWinSetLogging(winNetCfgLogger); 281 282 printf("enabling all host-only interfaces..\n"); 280 RTMsgInfo("Enabling all host-only interfaces..."); 281 VBoxNetCfgWinSetLogging(winNetCfgLogger); 283 282 284 283 HRESULT hr = CoInitialize(NULL); … … 287 286 hr = VBoxNetCfgWinPropChangeAllNetDevicesOfId(VBOX_NETADP_HWID, VBOXNECTFGWINPROPCHANGE_TYPE_ENABLE); 288 287 if (SUCCEEDED(hr)) 289 printf("enabling successful\n");290 else 291 printf("enabling failed, hr=%#lx\n", hr);292 293 CoUninitialize(); 294 } 295 else 296 printf("Error initializing COM (%#lx)\n", hr);288 RTMsgInfo("Enabling successful!"); 289 else 290 RTMsgError("enabling failed: %hrc", hr); 291 292 CoUninitialize(); 293 } 294 else 295 RTMsgError("Failed initializing COM: %Rhrc", hr); 297 296 298 297 VBoxNetCfgWinSetLogging(NULL); … … 303 302 static void printUsage(void) 304 303 { 305 printf("host-only network adapter configuration tool\n" 306 " Usage: VBoxNetAdpInstall [cmd]\n" 307 " cmd can be one of the following values:\n" 308 " i - install a new host-only interface (default command)\n" 309 " u - uninstall all host-only interfaces\n" 310 " a - update the host-only driver\n" 311 " d - disable all host-only interfaces\n" 312 " e - enable all host-only interfaces\n" 313 " h - print this message\n"); 304 RTPrintf("host-only network adapter configuration tool\n" 305 " Usage: %s [cmd]\n" 306 " cmd can be one of the following values:\n" 307 " i - install a new host-only interface (default command)\n" 308 " u - uninstall all host-only interfaces\n" 309 " a - update the host-only driver\n" 310 " d - disable all host-only interfaces\n" 311 " e - enable all host-only interfaces\n" 312 " h - print this message\n", 313 RTProcShortName()); 314 314 } 315 315 316 316 int __cdecl main(int argc, char **argv) 317 317 { 318 RTR3InitExe(argc, &argv, 0); 319 318 320 if (argc < 2) 319 321 return VBoxNetAdpInstall(); -
trunk/src/VBox/HostDrivers/VBoxNetFlt/win/tools/VBoxNetAdpUninstall.cpp
r96407 r96572 35 35 */ 36 36 37 38 /********************************************************************************************************************************* 39 * Header Files * 40 *********************************************************************************************************************************/ 37 41 #include <VBox/VBoxNetCfg-win.h> 38 42 #include <VBox/VBoxDrvCfg-win.h> 39 #include <stdio.h>40 43 41 44 #include <devguid.h> 42 45 46 #include <iprt/initterm.h> 47 #include <iprt/message.h> 48 49 50 /********************************************************************************************************************************* 51 * Defined Constants And Macros * 52 *********************************************************************************************************************************/ 43 53 #ifdef NDIS60 44 54 # define VBOX_NETADP_HWID L"sun_VBoxNetAdp6" … … 47 57 #endif 48 58 59 49 60 static DECLCALLBACK(void) winNetCfgLogger(const char *pszString) 50 61 { 51 printf("%s", pszString);62 RTMsgInfo("%s", pszString); 52 63 } 53 64 54 65 static int VBoxNetAdpUninstall(void) 55 66 { 67 RTMsgInfo("Uninstalling all Host-Only interfaces ..."); 68 56 69 int rcExit = RTEXITCODE_FAILURE; 57 70 VBoxNetCfgWinSetLogging(winNetCfgLogger); 58 59 printf("uninstalling all Host-Only interfaces..\n");60 71 61 72 HRESULT hr = CoInitialize(NULL); … … 67 78 hr = VBoxDrvCfgInfUninstallAllSetupDi(&GUID_DEVCLASS_NET, L"Net", VBOX_NETADP_HWID, 0/* could be SUOI_FORCEDELETE */); 68 79 if (hr == S_OK) 69 printf("uninstalled successfully\n");80 RTMsgInfo("Uninstalled successfully!"); 70 81 else 71 printf("uninstalled successfully, but failed to remove infs\n");82 RTMsgError("uninstalled successfully, but failed to remove infs (%Rhrc)\n", hr); 72 83 rcExit = RTEXITCODE_SUCCESS; 73 84 } 74 85 else 75 printf("uninstall failed, hr=%#lx\n", hr);86 RTMsgError("uninstall failed: %Rhrc", hr); 76 87 77 88 CoUninitialize(); 78 89 } 79 90 else 80 wprintf(L"Error initializing COM (%#lx)\n", hr);91 RTMsgError("Failed initializing COM: %Rhrc", hr); 81 92 82 93 VBoxNetCfgWinSetLogging(NULL); … … 87 98 int __cdecl main(int argc, char **argv) 88 99 { 89 RT_NOREF2(argc, argv); 100 RTR3InitExeNoArguments(0); 101 if (argc != 1) 102 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "This utility takes no arguments\n"); 103 NOREF(argv); 104 90 105 return VBoxNetAdpUninstall(); 91 106 } 107 -
trunk/src/VBox/HostDrivers/VBoxNetFlt/win/tools/VBoxNetFltInstall.cpp
r96407 r96572 35 35 */ 36 36 37 38 /********************************************************************************************************************************* 39 * Header Files * 40 *********************************************************************************************************************************/ 37 41 #include <VBox/VBoxNetCfg-win.h> 38 42 #include <devguid.h> 39 43 #include <stdio.h> 40 44 45 #include <iprt/initterm.h> 46 #include <iprt/message.h> 47 48 49 /********************************************************************************************************************************* 50 * Header Files * 51 *********************************************************************************************************************************/ 41 52 #define NETFLT_ID L"sun_VBoxNetFlt" 42 53 #define VBOX_NETCFG_APP_NAME L"NetFltInstall" … … 181 192 int __cdecl main(int argc, char **argv) 182 193 { 183 RT_NOREF2(argc, argv); 194 RTR3InitExeNoArguments(0); 195 if (argc != 1) 196 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "This utility takes no arguments\n"); 197 NOREF(argv); 198 184 199 return VBoxNetFltInstall(); 185 200 } 201 -
trunk/src/VBox/HostDrivers/VBoxNetFlt/win/tools/VBoxNetFltUninstall.cpp
r96407 r96572 35 35 */ 36 36 37 38 /********************************************************************************************************************************* 39 * Header Files * 40 *********************************************************************************************************************************/ 37 41 #include <VBox/VBoxNetCfg-win.h> 38 42 #include <stdio.h> 39 43 44 #include <iprt/initterm.h> 45 #include <iprt/message.h> 46 47 48 /********************************************************************************************************************************* 49 * Defined Constants And Macros * 50 *********************************************************************************************************************************/ 40 51 #define NETFLT_ID L"sun_VBoxNetFlt" 41 52 #define VBOX_NETCFG_APP_NAME L"NetFltUninstall" … … 43 54 #define VBOX_NETFLT_MP_INF L".\\VBoxNetFltM.inf" 44 55 #define VBOX_NETFLT_RETRIES 10 56 45 57 46 58 static DECLCALLBACK(void) winNetCfgLogger(const char *pszString) … … 112 124 int __cdecl main(int argc, char **argv) 113 125 { 114 RT_NOREF2(argc, argv); 126 RTR3InitExeNoArguments(0); 127 if (argc != 1) 128 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "This utility takes no arguments\n"); 129 NOREF(argv); 130 115 131 return VBoxNetFltUninstall(); 116 132 } 133 -
trunk/src/VBox/HostDrivers/VBoxNetFlt/win/tools/VBoxNetLwfInstall.cpp
r96407 r96572 35 35 */ 36 36 37 38 /********************************************************************************************************************************* 39 * Header Files * 40 *********************************************************************************************************************************/ 37 41 #include <VBox/VBoxNetCfg-win.h> 38 42 #include <devguid.h> 39 #include <stdio.h>40 43 44 #include <iprt/initterm.h> 45 #include <iprt/message.h> 46 #include <iprt/utf16.h> 47 48 49 /********************************************************************************************************************************* 50 * Defined Constants And Macros * 51 *********************************************************************************************************************************/ 41 52 #define VBOX_NETCFG_APP_NAME L"NetLwfInstall" 42 53 #define VBOX_NETLWF_INF L".\\VBoxNetLwf.inf" … … 46 57 static DECLCALLBACK(void) winNetCfgLogger(const char *pszString) 47 58 { 48 printf("%s", pszString);59 RTMsgInfo("%s", pszString); 49 60 } 50 61 … … 116 127 if (hr == S_OK) 117 128 { 118 wprintf(L"installed successfully\n");129 RTMsgInfo("Installed successfully!"); 119 130 rcExit = RTEXITCODE_SUCCESS; 120 131 } 121 132 else 122 wprintf(L"error installing VBoxNetLwf (%#lx)\n", hr);133 RTMsgError("Failed installing VBoxNetLwf: %Rhrc", hr); 123 134 } 124 135 else 125 136 { 126 137 hr = HRESULT_FROM_WIN32(GetLastError()); 127 wprintf(L"error getting full inf path for VBoxNetLwf.inf (%#lx)\n", hr);138 RTMsgError("Failed getting full inf path for VBoxNetLwf.inf: %Rhrc", hr); 128 139 } 129 140 … … 134 145 if (hr == NETCFG_E_NO_WRITE_LOCK && pwszLockedBy) 135 146 { 136 if (i < VBOX_NETLWF_RETRIES && !wcscmp(pwszLockedBy, L"6to4svc.dll"))147 if (i < VBOX_NETLWF_RETRIES && RTUtf16ICmpAscii(pwszLockedBy, "6to4svc.dll") == 0) 137 148 { 138 wprintf(L"6to4svc.dll is holding the lock,retrying %d out of %d\n", i + 1, VBOX_NETLWF_RETRIES);149 RTMsgInfo("6to4svc.dll is holding the lock - retrying %d out of %d\n", i + 1, VBOX_NETLWF_RETRIES); 139 150 CoTaskMemFree(pwszLockedBy); 140 151 } 141 152 else 142 153 { 143 wprintf(L"Error: write lock is owned by another application (%s), close the application and retry installing\n",144 pwszLockedBy);154 RTMsgError("write lock is owned by another application (%ls), close the application and retry installing", 155 pwszLockedBy); 145 156 CoTaskMemFree(pwszLockedBy); 146 157 break; … … 149 160 else 150 161 { 151 wprintf(L"Error getting the INetCfg interface (%#lx)\n", hr);162 RTMsgError("Failed getting the INetCfg interface: %Rhrc", hr); 152 163 break; 153 164 } … … 157 168 } 158 169 else 159 wprintf(L"Error initializing COM (%#lx)\n", hr);170 RTMsgError("Failed initializing COM: %Rhrc", hr); 160 171 161 172 VBoxNetCfgWinSetLogging(NULL); … … 166 177 int __cdecl main(int argc, char **argv) 167 178 { 168 RT_NOREF2(argc, argv); 179 RTR3InitExeNoArguments(0); 180 if (argc != 1) 181 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "This utility takes no arguments\n"); 182 NOREF(argv); 183 169 184 return VBoxNetLwfInstall(); 170 185 } 186 -
trunk/src/VBox/HostDrivers/VBoxNetFlt/win/tools/VBoxNetLwfUninstall.cpp
r96407 r96572 35 35 */ 36 36 37 38 /********************************************************************************************************************************* 39 * Header Files * 40 *********************************************************************************************************************************/ 37 41 #include <VBox/VBoxNetCfg-win.h> 38 #include <stdio.h>39 42 43 #include <iprt/initterm.h> 44 #include <iprt/message.h> 45 #include <iprt/utf16.h> 46 47 48 /********************************************************************************************************************************* 49 * Defined Constants And Macros * 50 *********************************************************************************************************************************/ 40 51 #define VBOX_NETCFG_APP_NAME L"NetLwfUninstall" 41 52 #define VBOX_NETLWF_RETRIES 10 42 53 54 43 55 static DECLCALLBACK(void) winNetCfgLogger(const char *pszString) 44 56 { 45 printf("%s", pszString);57 RTMsgInfo("%s", pszString); 46 58 } 47 59 48 60 static int VBoxNetLwfUninstall() 49 61 { 50 INetCfg *pnc;51 62 int rcExit = RTEXITCODE_FAILURE; 52 63 … … 59 70 { 60 71 LPWSTR pwszLockedBy = NULL; 72 INetCfg *pnc = NULL; 61 73 hr = VBoxNetCfgWinQueryINetCfg(&pnc, TRUE, VBOX_NETCFG_APP_NAME, 10000, &pwszLockedBy); 62 74 if (hr == S_OK) … … 65 77 if (hr == S_OK) 66 78 { 67 wprintf(L"uninstalled successfully\n");79 RTMsgInfo("uninstalled successfully!"); 68 80 rcExit = RTEXITCODE_SUCCESS; 69 81 } 70 82 else 71 wprintf(L"error uninstalling VBoxNetLwf (%#lx)\n", hr);83 RTMsgError("error uninstalling VBoxNetLwf: %Rhrc"); 72 84 73 85 VBoxNetCfgWinReleaseINetCfg(pnc, TRUE); … … 77 89 if (hr == NETCFG_E_NO_WRITE_LOCK && pwszLockedBy) 78 90 { 79 if (i < VBOX_NETLWF_RETRIES && !wcscmp(pwszLockedBy, L"6to4svc.dll"))91 if (i < VBOX_NETLWF_RETRIES && RTUtf16ICmpAscii(pwszLockedBy, "6to4svc.dll") == 0) 80 92 { 81 wprintf(L"6to4svc.dll is holding the lock, retrying %d out of %d\n", i + 1, VBOX_NETLWF_RETRIES);93 RTMsgInfo("6to4svc.dll is holding the lock - retry %d out of %d ...", i + 1, VBOX_NETLWF_RETRIES); 82 94 CoTaskMemFree(pwszLockedBy); 83 95 } 84 96 else 85 97 { 86 wprintf(L"Error: write lock is owned by another application (%s), close the application and retry uninstalling\n",87 pwszLockedBy);98 RTMsgError("Write lock is owned by another application (%ls), close the application and retry uninstalling", 99 pwszLockedBy); 88 100 CoTaskMemFree(pwszLockedBy); 89 101 break; … … 92 104 else 93 105 { 94 wprintf(L"Error getting the INetCfg interface (%#lx)\n", hr);106 RTMsgError("Failed getting the INetCfg interface: %Rhrc", hr); 95 107 break; 96 108 } … … 100 112 } 101 113 else 102 wprintf(L"Error initializing COM (%#lx)\n", hr);114 RTMsgError("Failed initializing COM: %Rhrc", hr); 103 115 104 116 VBoxNetCfgWinSetLogging(NULL); … … 109 121 int __cdecl main(int argc, char **argv) 110 122 { 111 RT_NOREF2(argc, argv); 123 RTR3InitExeNoArguments(0); 124 if (argc != 1) 125 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "This utility takes no arguments\n"); 126 NOREF(argv); 127 112 128 return VBoxNetLwfUninstall(); 113 129 } 130
Note:
See TracChangeset
for help on using the changeset viewer.