Changeset 17172 in vbox for trunk/src/VBox/Main
- Timestamp:
- Feb 26, 2009 3:25:20 PM (16 years ago)
- Location:
- trunk/src/VBox/Main
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/Makefile.kmk
r17086 r17172 329 329 330 330 ifdef VBOX_WITH_NETFLT 331 #needed to include intsafe.h indirectly included by comdef.h 332 VBoxSVC_INCS.win += $(foreach VARSDK, $(VBoxSVC_SDKS), $(PATH_SDK_$(VARSDK)_INC)/crt) 333 #needed to include comsupp.lib 334 VBoxSVC_LIBPATH += $(if $(VBOX_USE_VCC80),$(PATH_TOOL_VCC80_LIB),$(PATH_TOOL_VCC70_LIB)) 335 VBoxSVC_LIBS.win += wbemuuid.lib comsupp.lib 331 336 VBoxSVC_LIBS.win += $(PATH_LIB)/WinNetConfig.lib 332 337 ifdef VBOX_NETFLT_ONDEMAND_BIND -
trunk/src/VBox/Main/win/NetIfList-win.cpp
r16968 r17172 46 46 #include "HostNetworkInterfaceImpl.h" 47 47 #include "netif.h" 48 49 #include <Wbemidl.h> 50 #include <comdef.h> 51 52 static HRESULT netIfWinCreateIWbemServices(IWbemServices ** ppSvc) 53 { 54 HRESULT hres; 55 56 // Step 3: --------------------------------------------------- 57 // Obtain the initial locator to WMI ------------------------- 58 59 IWbemLocator *pLoc = NULL; 60 61 hres = CoCreateInstance( 62 CLSID_WbemLocator, 63 0, 64 CLSCTX_INPROC_SERVER, 65 IID_IWbemLocator, (LPVOID *) &pLoc); 66 if(SUCCEEDED(hres)) 67 { 68 // Step 4: ----------------------------------------------------- 69 // Connect to WMI through the IWbemLocator::ConnectServer method 70 71 IWbemServices *pSvc = NULL; 72 73 // Connect to the root\cimv2 namespace with 74 // the current user and obtain pointer pSvc 75 // to make IWbemServices calls. 76 hres = pLoc->ConnectServer( 77 _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace 78 NULL, // User name. NULL = current user 79 NULL, // User password. NULL = current 80 0, // Locale. NULL indicates current 81 NULL, // Security flags. 82 0, // Authority (e.g. Kerberos) 83 0, // Context object 84 &pSvc // pointer to IWbemServices proxy 85 ); 86 if(SUCCEEDED(hres)) 87 { 88 LogRel(("Connected to ROOT\\CIMV2 WMI namespace\n")); 89 90 // Step 5: -------------------------------------------------- 91 // Set security levels on the proxy ------------------------- 92 93 hres = CoSetProxyBlanket( 94 pSvc, // Indicates the proxy to set 95 RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx 96 RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx 97 NULL, // Server principal name 98 RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx 99 RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx 100 NULL, // client identity 101 EOAC_NONE // proxy capabilities 102 ); 103 if(SUCCEEDED(hres)) 104 { 105 *ppSvc = pSvc; 106 /* do not need it any more */ 107 pLoc->Release(); 108 return hres; 109 } 110 else 111 { 112 LogRel(("Could not set proxy blanket. Error code = 0x%x\n", hres)); 113 } 114 115 pSvc->Release(); 116 } 117 else 118 { 119 LogRel(("Could not connect. Error code = 0x%x\n", hres)); 120 } 121 122 pLoc->Release(); 123 } 124 else 125 { 126 LogRel(("Failed to create IWbemLocator object. Err code = 0x%x\n", hres)); 127 // CoUninitialize(); 128 } 129 130 return hres; 131 } 132 133 static HRESULT netIfWinFindAdapterClassById(IWbemServices * pSvc, GUID * pGuid, IWbemClassObject **pAdapterConfig) 134 { 135 HRESULT hres; 136 WCHAR aQueryString[256]; 137 char uuidStr[RTUUID_STR_LENGTH]; 138 int rc = RTUuidToStr((PCRTUUID)pGuid, uuidStr, sizeof(uuidStr)); 139 if(RT_SUCCESS(rc)) 140 { 141 swprintf(aQueryString, L"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE SettingID = \"{%S}\"", uuidStr); 142 // Step 6: -------------------------------------------------- 143 // Use the IWbemServices pointer to make requests of WMI ---- 144 145 IEnumWbemClassObject* pEnumerator = NULL; 146 hres = pSvc->ExecQuery( 147 bstr_t("WQL"), 148 bstr_t(aQueryString), 149 WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 150 NULL, 151 &pEnumerator); 152 if(SUCCEEDED(hres)) 153 { 154 // Step 7: ------------------------------------------------- 155 // Get the data from the query in step 6 ------------------- 156 157 IWbemClassObject *pclsObj; 158 ULONG uReturn = 0; 159 160 while (pEnumerator) 161 { 162 HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, 163 &pclsObj, &uReturn); 164 165 if(SUCCEEDED(hres)) 166 { 167 if(uReturn) 168 { 169 pEnumerator->Release(); 170 *pAdapterConfig = pclsObj; 171 hres = S_OK; 172 return hres; 173 } 174 else 175 { 176 hres = S_FALSE; 177 } 178 } 179 180 } 181 pEnumerator->Release(); 182 } 183 else 184 { 185 Log(("Query for operating system name failed. Error code = 0x%x\n", hres)); 186 } 187 } 188 else 189 { 190 hres = -1; 191 } 192 193 return hres; 194 } 195 196 static HRESULT netIfAdapterConfigPath(IWbemClassObject *pObj, BSTR * pStr) 197 { 198 VARIANT index; 199 200 // Get the value of the key property 201 HRESULT hr = pObj->Get(L"Index", 0, &index, 0, 0); 202 if(SUCCEEDED(hr)) 203 { 204 WCHAR strIndex[8]; 205 swprintf(strIndex, L"%u", index.uintVal); 206 *pStr = (bstr_t(L"Win32_NetworkAdapterConfiguration.Index='") + strIndex + "'").copy(); 207 } 208 else 209 { 210 DWORD dwError = GetLastError(); 211 Assert(0); 212 hr = HRESULT_FROM_WIN32( dwError ); 213 } 214 return hr; 215 } 216 217 static HRESULT netIfExecMethod(IWbemServices * pSvc, IWbemClassObject *pClass, BSTR ObjPath, 218 BSTR MethodName, LPWSTR *pArgNames, LPVARIANT *pArgs, UINT cArgs, 219 IWbemClassObject** ppOutParams 220 ) 221 { 222 HRESULT hres; 223 // Step 6: -------------------------------------------------- 224 // Use the IWbemServices pointer to make requests of WMI ---- 225 226 IWbemClassObject* pInParamsDefinition = NULL; 227 hres = pClass->GetMethod(MethodName, 0, 228 &pInParamsDefinition, NULL); 229 if(SUCCEEDED(hres)) 230 { 231 IWbemClassObject* pClassInstance = NULL; 232 hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance); 233 234 if(SUCCEEDED(hres)) 235 { 236 for(UINT i = 0; i < cArgs; i++) 237 { 238 // Store the value for the in parameters 239 hres = pClassInstance->Put(pArgNames[i], 0, 240 pArgs[i], 0); 241 if(FAILED(hres)) 242 { 243 break; 244 } 245 } 246 247 if(SUCCEEDED(hres)) 248 { 249 IWbemClassObject* pOutParams = NULL; 250 hres = pSvc->ExecMethod(ObjPath, MethodName, 0, 251 NULL, pClassInstance, &pOutParams, NULL); 252 if(SUCCEEDED(hres)) 253 { 254 *ppOutParams = pOutParams; 255 } 256 } 257 258 pClassInstance->Release(); 259 } 260 261 pInParamsDefinition->Release(); 262 } 263 264 return hres; 265 } 266 267 static HRESULT createIpArray(SAFEARRAY **ppArray, in_addr* aIp, UINT cIp) 268 { 269 HRESULT hr; 270 SAFEARRAY * pIpArray = SafeArrayCreateVector(VT_BSTR, 0, cIp); 271 if(pIpArray) 272 { 273 for(UINT i = 0; i < cIp; i++) 274 { 275 char* addr = inet_ntoa(aIp[i]); 276 BSTR val = bstr_t(addr).copy(); 277 long aIndex[1]; 278 aIndex[0] = i; 279 hr = SafeArrayPutElement(pIpArray, aIndex, val); 280 if(FAILED(hr)) 281 { 282 SysFreeString(val); 283 SafeArrayDestroy(pIpArray); 284 break; 285 } 286 } 287 288 if(SUCCEEDED(hr)) 289 { 290 *ppArray = pIpArray; 291 } 292 } 293 else 294 { 295 DWORD dwError = GetLastError(); 296 Assert(0); 297 hr = HRESULT_FROM_WIN32( dwError ); 298 } 299 300 return hr; 301 } 302 303 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinEnableStatic(IWbemServices * pSvc, IWbemClassObject *pObj, in_addr* aIp, in_addr * aMask, UINT cIp) 304 { 305 IWbemClassObject * pClass; 306 BSTR ClassName = SysAllocString(L"Win32_NetworkAdapterConfiguration"); 307 HRESULT hr; 308 if(ClassName) 309 { 310 hr = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL); 311 if(SUCCEEDED(hr)) 312 { 313 BSTR ObjPath; 314 hr = netIfAdapterConfigPath(pObj, &ObjPath); 315 if(SUCCEEDED(hr)) 316 { 317 LPWSTR argNames[] = {L"IPAddress", L"SubnetMask"}; 318 VARIANT ipAddresses; 319 VariantInit(&ipAddresses); 320 ipAddresses.vt = VT_ARRAY | VT_BSTR; 321 SAFEARRAY *pIpArray; 322 hr = createIpArray(&pIpArray, aIp, cIp); 323 if(SUCCEEDED(hr)) 324 { 325 ipAddresses.parray = pIpArray; 326 VARIANT ipMasks; 327 VariantInit(&ipMasks); 328 ipMasks.vt = VT_ARRAY | VT_BSTR; 329 SAFEARRAY *pMaskArray; 330 hr = createIpArray(&pMaskArray, aMask, cIp); 331 if(SUCCEEDED(hr)) 332 { 333 ipMasks.parray = pMaskArray; 334 LPVARIANT args[] = {&ipAddresses, &ipMasks}; 335 IWbemClassObject * pOutParams; 336 337 hr = netIfExecMethod(pSvc, pClass, ObjPath, 338 bstr_t(L"EnableStatic"), argNames, args, 2, &pOutParams); 339 if(SUCCEEDED(hr)) 340 { 341 } 342 SafeArrayDestroy(pMaskArray); 343 } 344 SafeArrayDestroy(pIpArray); 345 } 346 SysFreeString(ObjPath); 347 } 348 pClass->Release(); 349 } 350 SysFreeString(ClassName); 351 } 352 else 353 { 354 DWORD dwError = GetLastError(); 355 Assert(0); 356 hr = HRESULT_FROM_WIN32( dwError ); 357 } 358 359 return hr; 360 } 48 361 49 362 … … 338 651 } 339 652 653 //TODO: this is sample currently, hardcoded balues should be removed and exposed to the API 654 static int enableStatic() 655 { 656 INetCfg *pnc; 657 LPWSTR lpszLockedBy = NULL; 658 int r = 1; 659 HRESULT hr; 660 661 hr = VBoxNetCfgWinQueryINetCfg(FALSE, L"VirtualBox", &pnc, &lpszLockedBy); 662 if(hr == S_OK) 663 { 664 INetCfgComponent *pComponent; 665 HRESULT hr = pnc->FindComponent(L"*msloop", &pComponent); 666 if(hr == S_OK) 667 { 668 GUID guid; 669 hr = pComponent->GetInstanceGuid(&guid); 670 if(SUCCEEDED(hr)) 671 { 672 IWbemServices * pSvc; 673 hr = netIfWinCreateIWbemServices(&pSvc); 674 if(SUCCEEDED(hr)) 675 { 676 IWbemClassObject *pAdapterConfig; 677 hr = netIfWinFindAdapterClassById(pSvc, &guid, &pAdapterConfig); 678 if(SUCCEEDED(hr)) 679 { 680 in_addr ip[1]; 681 in_addr mask[1]; 682 ip[0].S_un.S_addr = inet_addr("192.168.5.1"); 683 mask[0].S_un.S_addr = inet_addr("255.255.255.0"); 684 685 hr = VBoxNetCfgWinEnableStatic(pSvc, pAdapterConfig, ip, mask, 1); 686 if(SUCCEEDED(hr)) 687 { 688 printf("succees!!!\n"); 689 r = 0; 690 } 691 } 692 } 693 694 } 695 } 696 } 697 698 699 return r; 700 } 701 702 static bool bTest = true; 703 340 704 int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list) 341 705 { 706 if(bTest) 707 { 708 bTest = false; 709 Assert(0); 710 enableStatic(); 711 } 712 342 713 #ifndef VBOX_WITH_NETFLT 343 714 static const char *NetworkKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\"
Note:
See TracChangeset
for help on using the changeset viewer.