Changeset 77875 in vbox for trunk/src/VBox/HostDrivers/VBoxNetFlt/win/cfg
- Timestamp:
- Mar 26, 2019 8:11:25 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/HostDrivers/VBoxNetFlt/win/cfg/VBoxNetCfg.cpp
r77804 r77875 49 49 #include <iprt/win/iphlpapi.h> 50 50 51 #include <set> 51 52 52 53 #ifndef Assert /** @todo r=bird: where would this be defined? */ … … 2951 2952 } 2952 2953 2954 /* We assume the following name matches the device description in vboxnetadp6.inf */ 2955 #define HOSTONLY_ADAPTER_NAME "VirtualBox Host-Only Ethernet Adapter" 2956 2957 /* 2958 * Enumerate all host-only adapters collecting their names into a set, then 2959 * come up with the next available name by taking the first unoccupied index. 2960 */ 2961 static HRESULT vboxNetCfgWinNextAvailableDevName(bstr_t& bstrName) 2962 { 2963 SP_DEVINFO_DATA DeviceInfoData; 2964 /* initialize the structure size */ 2965 DeviceInfoData.cbSize = sizeof (SP_DEVINFO_DATA); 2966 2967 HDEVINFO DeviceInfoSet = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT); 2968 if (DeviceInfoSet == INVALID_HANDLE_VALUE) 2969 return GetLastError(); 2970 2971 DWORD i; 2972 std::set<bstr_t> aExistingNames; 2973 for (i = 0; SetupDiEnumDeviceInfo(DeviceInfoSet, i, &DeviceInfoData); ++i) 2974 { 2975 /* Should be more than enough for both our device id and our device name, we do not care about the rest */ 2976 WCHAR wszDevName[64]; 2977 if (!SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_HARDWAREID, 2978 NULL, (PBYTE)wszDevName, sizeof(wszDevName), NULL)) 2979 continue; 2980 /* Ignore everything except our host-only adapters */ 2981 if (_wcsicmp(wszDevName, DRIVERHWID)) 2982 continue; 2983 if ( SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_FRIENDLYNAME, 2984 NULL, (PBYTE)wszDevName, sizeof(wszDevName), NULL) 2985 || SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_DEVICEDESC, 2986 NULL, (PBYTE)wszDevName, sizeof(wszDevName), NULL)) 2987 aExistingNames.insert(bstr_t(wszDevName)); 2988 } 2989 /* Try the name without index first */ 2990 bstrName = HOSTONLY_ADAPTER_NAME; 2991 if (aExistingNames.find(bstrName) != aExistingNames.end()) 2992 { 2993 WCHAR wszSuffix[16]; 2994 /* Try indexed names until we find unused one */ 2995 for (i = 2;; ++i) 2996 { 2997 wsprintf(wszSuffix, L" #%u", i); 2998 if (aExistingNames.find(bstrName + wszSuffix) == aExistingNames.end()) 2999 { 3000 bstrName += wszSuffix; 3001 break; 3002 } 3003 } 3004 } 3005 3006 if (DeviceInfoSet) 3007 SetupDiDestroyDeviceInfoList(DeviceInfoSet); 3008 return S_OK; 3009 } 3010 2953 3011 static HRESULT vboxNetCfgWinCreateHostOnlyNetworkInterface(IN LPCWSTR pInfPath, IN bool bIsInfPathFile, IN BSTR bstrDesiredName, 2954 3012 OUT GUID *pGuid, OUT BSTR *lppszName, OUT BSTR *pErrMsg) … … 2966 3024 HKEY hkey = (HKEY)INVALID_HANDLE_VALUE; 2967 3025 bstr_t bstrError; 3026 bstr_t bstrNewInterfaceName; 3027 3028 if (SysStringLen(bstrDesiredName) != 0) 3029 bstrNewInterfaceName = bstrDesiredName; 3030 else 3031 { 3032 hrc = vboxNetCfgWinNextAvailableDevName(bstrNewInterfaceName); 3033 if (FAILED(hrc)) 3034 NonStandardLogFlow(("vboxNetCfgWinNextAvailableDevName failed with 0x%x\n", hrc)); 3035 } 2968 3036 2969 3037 do … … 3324 3392 3325 3393 3326 #ifndef VBOXNETCFG_DELAYEDRENAME3327 3394 /* 3328 3395 * We need to query the device name after we have succeeded in querying its … … 3356 3423 } 3357 3424 } 3358 #else /* !VBOXNETCFG_DELAYEDRENAME */ 3425 /* No need to rename the device if the names match. */ 3426 if (!wcscmp(bstrNewInterfaceName.GetBSTR(), DevName)) 3427 bstrNewInterfaceName.Assign(NULL); 3428 #ifdef VBOXNETCFG_DELAYEDRENAME 3359 3429 /* Re-use DevName for device instance id retrieval. */ 3360 3430 if (!SetupDiGetDeviceInstanceId(hDeviceInfo, &DeviceInfoData, DevName, RT_ELEMENTS(DevName), &cbSize)) 3361 3431 SetErrBreak (("SetupDiGetDeviceInstanceId failed (0x%08X)", 3362 3432 GetLastError())); 3363 #endif /* !VBOXNETCFG_DELAYEDRENAME */3433 #endif /* VBOXNETCFG_DELAYEDRENAME */ 3364 3434 } 3365 3435 while (0); … … 3413 3483 if (hr == S_OK) 3414 3484 { 3415 if ( SysStringLen(bstrDesiredName) != 0)3416 { 3417 /* Rename only if the desired name has been provided*/3418 context.bstrName = bstr DesiredName;3485 if (!!bstrNewInterfaceName) 3486 { 3487 /* The assigned name does not match the desired one, rename the device */ 3488 context.bstrName = bstrNewInterfaceName.GetBSTR(); 3419 3489 context.pGuid = pGuid; 3420 3490 hr = vboxNetCfgWinEnumNetCfgComponents(pNetCfg,
Note:
See TracChangeset
for help on using the changeset viewer.