Changeset 36487 in vbox for trunk/src/VBox/HostDrivers/VBoxNetFlt/win/cfg
- Timestamp:
- Apr 1, 2011 8:21:30 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/HostDrivers/VBoxNetFlt/win/cfg/VBoxNetCfg.cpp
r36184 r36487 15 15 */ 16 16 #include "VBox/VBoxNetCfg-win.h" 17 #include "VBox/VBoxDrvCfg-win.h" 17 18 18 19 #define _WIN32_DCOM … … 54 55 #define VBOX_NETCFG_LOCK_TIME_OUT 5000 55 56 56 class VBoxNetCfgStringList57 {58 public:59 VBoxNetCfgStringList(int aSize);60 61 ~VBoxNetCfgStringList();62 63 HRESULT add(LPWSTR pStr);64 65 int size() {return mSize;}66 67 LPWSTR get(int i) {return maList[i];}68 private:69 HRESULT resize(int newSize);70 71 LPWSTR *maList;72 int mBufSize;73 int mSize;74 };75 76 VBoxNetCfgStringList::VBoxNetCfgStringList(int aSize)77 {78 maList = (LPWSTR*)CoTaskMemAlloc( sizeof(maList[0]) * aSize);79 mBufSize = aSize;80 mSize = 0;81 }82 83 VBoxNetCfgStringList::~VBoxNetCfgStringList()84 {85 if (!mBufSize)86 return;87 88 for (int i = 0; i < mSize; ++i)89 {90 CoTaskMemFree(maList[i]);91 }92 93 CoTaskMemFree(maList);94 }95 96 HRESULT VBoxNetCfgStringList::add(LPWSTR pStr)97 {98 if (mSize == mBufSize)99 {100 int hr = resize(mBufSize+10);101 if (SUCCEEDED(hr))102 return hr;103 }104 size_t cStr = wcslen(pStr) + 1;105 LPWSTR str = (LPWSTR)CoTaskMemAlloc( sizeof(maList[0][0]) * cStr);106 memcpy(str, pStr, sizeof(maList[0][0]) * cStr);107 maList[mSize] = str;108 ++mSize;109 return S_OK;110 }111 112 HRESULT VBoxNetCfgStringList::resize(int newSize)113 {114 Assert(newSize >= mSize);115 if (newSize < mSize)116 return E_FAIL;117 LPWSTR* pOld = maList;118 maList = (LPWSTR*)CoTaskMemAlloc( sizeof(maList[0]) * newSize);119 mBufSize = newSize;120 memcpy(maList, pOld, mSize*sizeof(maList[0]));121 CoTaskMemFree(pOld);122 return S_OK;123 }124 125 /*126 * inf file manipulation API127 */128 typedef bool (*PFNVBOXNETCFG_ENUMERATION_CALLBACK) (LPCWSTR lpszFileName, PVOID pContext);129 130 typedef struct _INF_INFO131 {132 LPCWSTR lpszClassName;133 LPCWSTR lpszPnPId;134 } INF_INFO, *PINF_INFO;135 136 typedef struct _INFENUM_CONTEXT137 {138 INF_INFO InfInfo;139 DWORD Flags;140 HRESULT hr;141 } INFENUM_CONTEXT, *PINFENUM_CONTEXT;142 143 static HRESULT vboxNetCfgWinInfQueryKeyValue(HINF hInf, LPCWSTR lpszSection, LPCWSTR lpszKey, DWORD iValue, LPWSTR *lppszValue)144 {145 INFCONTEXT InfContext;146 DWORD winEr;147 148 if (!SetupFindFirstLineW(hInf, lpszSection, lpszKey, &InfContext))149 {150 winEr = GetLastError();151 Log(__FUNCTION__ ": SetupFindFirstLine failed WinEr (%d) for Section(%S), Key(%S)\n", winEr, lpszSection, lpszKey);152 return HRESULT_FROM_WIN32(winEr);153 }154 155 DWORD cbValue;156 if (SetupGetStringFieldW(&InfContext, iValue, NULL, 0, &cbValue))157 {158 Assert(0);159 /* SetupGetStringField succeeded when it should not */160 DbgLog(__FUNCTION__ ": SetupGetStringField succeeded when it should not, for Section(%S), Key(%S), iValue(%d)\n", lpszSection, lpszKey, iValue);161 return E_FAIL;162 }163 164 winEr = GetLastError();165 Assert(winEr == ERROR_INSUFFICIENT_BUFFER);166 if (winEr != ERROR_INSUFFICIENT_BUFFER)167 {168 Log(__FUNCTION__ ": SetupGetStringField failed WinEr (%d) for Section(%S), Key(%S), iValue(%d)\n", winEr, lpszSection, lpszKey, iValue);169 return HRESULT_FROM_WIN32(winEr);170 }171 172 LPWSTR lpszValue = (LPWSTR)CoTaskMemAlloc(cbValue * sizeof (lpszValue[0]));173 Assert(lpszValue);174 if (!lpszValue)175 {176 Log(__FUNCTION__ ": SetCoTaskMemAlloc failed to alloc mem of size (%d), for Section(%S), Key(%S), iValue(%d)\n", cbValue * sizeof (lpszValue[0]), winEr, lpszSection, lpszKey, iValue);177 return E_FAIL;178 }179 180 if (!SetupGetStringFieldW(&InfContext, iValue, lpszValue, cbValue, &cbValue))181 {182 winEr = GetLastError();183 Log(__FUNCTION__ ": SetupGetStringField failed WinEr (%d) for Section(%S), Key(%S), , iValue(%d)\n", winEr, lpszSection, lpszKey, iValue);184 Assert(0);185 return HRESULT_FROM_WIN32(winEr);186 }187 188 *lppszValue = lpszValue;189 return S_OK;190 }191 192 static HRESULT vboxNetCfgWinInfQueryFirstPnPId(HINF hInf, LPWSTR *lppszPnPId)193 {194 LPWSTR lpszModels;195 LPWSTR lpszPnPId;196 HRESULT hr = vboxNetCfgWinInfQueryKeyValue(hInf, L"Manufacturer", NULL, 1, &lpszModels);197 if (hr != S_OK)198 {199 DbgLog(__FUNCTION__ ": vboxNetCfgWinRegQueryKeyValue for Manufacturer failed, hr = (0x%x)\n", hr);200 return hr;201 }202 203 hr = vboxNetCfgWinInfQueryKeyValue(hInf, lpszModels, NULL, 2, &lpszPnPId);204 /* free models string right away */205 CoTaskMemFree(lpszModels);206 if (hr != S_OK)207 {208 Log(__FUNCTION__ ": vboxNetCfgWinRegQueryKeyValue for models failed, hr = (0x%x)\n", hr);209 return hr;210 }211 212 *lppszPnPId = lpszPnPId;213 return S_OK;214 }215 216 static bool vboxNetCfgWinInfEnumerationCallback(LPCWSTR lpszFileName, PVOID pCtxt);217 218 static HRESULT vboxNetCfgWinInfCopyEx(IN LPCWSTR lpszInfPath, IN DWORD fCopyStyle, OUT LPWSTR lpszDstName, IN DWORD cbDstName, OUT PDWORD pcbDstNameSize, OUT LPWSTR* lpszDstNameComponent)219 {220 WCHAR aMediaLocation[_MAX_DIR];221 WCHAR aDir[_MAX_DIR];222 223 _wsplitpath(lpszInfPath, aMediaLocation, aDir, NULL, NULL);224 wcscat(aMediaLocation, aDir);225 226 if (!SetupCopyOEMInfW(lpszInfPath, aMediaLocation, SPOST_PATH, fCopyStyle,227 lpszDstName, cbDstName, pcbDstNameSize,228 lpszDstNameComponent))229 {230 DWORD winEr = GetLastError();231 Log(__FUNCTION__ ": SetupCopyOEMInf fail winEr (%d)\n", winEr);232 return HRESULT_FROM_WIN32(winEr);233 }234 235 return S_OK;236 }237 238 static HRESULT vboxNetCfgWinInfCopy(IN LPCWSTR lpszInfPath)239 {240 return vboxNetCfgWinInfCopyEx(lpszInfPath, 0, NULL, 0, NULL, NULL);241 }242 243 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinInfInstall(IN LPCWSTR lpszInfPath)244 {245 return vboxNetCfgWinInfCopy(lpszInfPath);246 }247 248 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinInfUninstall(IN LPCWSTR lpszInfPath, DWORD fFlags)249 {250 if (!SetupUninstallOEMInfW(lpszInfPath, fFlags, NULL /*__in PVOID Reserved == NULL */))251 {252 DWORD winEr = GetLastError();253 Log(__FUNCTION__ ": SetupUninstallOEMInf failed for file (%S), winEr (%d)\n", lpszInfPath, winEr);254 Assert(0);255 return HRESULT_FROM_WIN32(winEr);256 }257 return S_OK;258 }259 260 261 static HRESULT vboxNetCfgWinCollectInfs(const GUID * pGuid, LPCWSTR pPnPId, VBoxNetCfgStringList & list)262 {263 DWORD winEr = ERROR_SUCCESS;264 int counter = 0;265 HDEVINFO hDevInfo = SetupDiCreateDeviceInfoList(266 pGuid, /* IN LPGUID ClassGuid, OPTIONAL */267 NULL /*IN HWND hwndParent OPTIONAL */268 );269 if (hDevInfo != INVALID_HANDLE_VALUE)270 {271 if (SetupDiBuildDriverInfoList(hDevInfo,272 NULL, /*IN OUT PSP_DEVINFO_DATA DeviceInfoData, OPTIONAL*/273 SPDIT_CLASSDRIVER /*IN DWORD DriverType*/274 ))275 {276 SP_DRVINFO_DATA DrvInfo;277 DrvInfo.cbSize = sizeof(SP_DRVINFO_DATA);278 char DetailBuf[16384];279 PSP_DRVINFO_DETAIL_DATA pDrvDetail = (PSP_DRVINFO_DETAIL_DATA)DetailBuf;280 281 for (DWORD i = 0; ; i++)282 {283 if (SetupDiEnumDriverInfo(hDevInfo,284 NULL, /* IN PSP_DEVINFO_DATA DeviceInfoData, OPTIONAL*/285 SPDIT_CLASSDRIVER , /*IN DWORD DriverType,*/286 i, /*IN DWORD MemberIndex,*/287 &DrvInfo /*OUT PSP_DRVINFO_DATA DriverInfoData*/288 ))289 {290 DWORD dwReq;291 pDrvDetail->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);292 if (SetupDiGetDriverInfoDetail(293 hDevInfo, /*IN HDEVINFO DeviceInfoSet,*/294 NULL, /*IN PSP_DEVINFO_DATA DeviceInfoData, OPTIONAL*/295 &DrvInfo, /*IN PSP_DRVINFO_DATA DriverInfoData,*/296 pDrvDetail, /*OUT PSP_DRVINFO_DETAIL_DATA DriverInfoDetailData, OPTIONAL*/297 sizeof(DetailBuf), /*IN DWORD DriverInfoDetailDataSize,*/298 &dwReq /*OUT PDWORD RequiredSize OPTIONAL*/299 ))300 {301 for (WCHAR * pHwId = pDrvDetail->HardwareID; pHwId && *pHwId && pHwId < (TCHAR*)(DetailBuf + sizeof(DetailBuf)/sizeof(DetailBuf[0])) ;pHwId += _tcslen(pHwId) + 1)302 {303 if (!wcsicmp(pHwId, pPnPId))304 {305 Assert(pDrvDetail->InfFileName[0]);306 if (pDrvDetail->InfFileName)307 {308 list.add(pDrvDetail->InfFileName);309 }310 }311 }312 }313 else314 {315 DWORD winEr = GetLastError();316 Log(__FUNCTION__": SetupDiGetDriverInfoDetail fail winEr (%d), size(%d)", winEr, dwReq);317 // Assert(0);318 }319 320 }321 else322 {323 DWORD winEr = GetLastError();324 if (winEr == ERROR_NO_MORE_ITEMS)325 {326 break;327 }328 329 Assert(0);330 }331 }332 333 SetupDiDestroyDriverInfoList(hDevInfo,334 NULL, /*IN PSP_DEVINFO_DATA DeviceInfoData, OPTIONAL*/335 SPDIT_CLASSDRIVER/*IN DWORD DriverType*/336 );337 }338 else339 {340 winEr = GetLastError();341 Assert(0);342 }343 344 SetupDiDestroyDeviceInfoList(hDevInfo);345 }346 else347 {348 winEr = GetLastError();349 Assert(0);350 }351 352 return HRESULT_FROM_WIN32(winEr);353 }354 355 VBOXNETCFGWIN_DECL(HRESULT) VBoxNetCfgWinInfUninstallAll(IN const GUID * pGuidClass, IN LPCWSTR lpszClassName, IN LPCWSTR lpszPnPId, IN DWORD Flags)356 {357 VBoxNetCfgStringList list(128);358 HRESULT hr = vboxNetCfgWinCollectInfs(pGuidClass, lpszPnPId, list);359 if (hr == S_OK)360 {361 INFENUM_CONTEXT Context;362 Context.InfInfo.lpszClassName = lpszClassName;363 Context.InfInfo.lpszPnPId = lpszPnPId;364 Context.Flags = Flags;365 Context.hr = S_OK;366 int size = list.size();367 for (int i = 0; i < size; ++i)368 {369 LPCWSTR pInf = list.get(i);370 const WCHAR* pRel = wcsrchr(pInf, '\\');371 if (pRel)372 ++pRel;373 else374 pRel = pInf;375 376 vboxNetCfgWinInfEnumerationCallback(pRel, &Context);377 // Log("inf : %S\n", list.get(i));378 }379 }380 return hr;381 }382 383 static HRESULT vboxNetCfgWinEnumFiles(LPCWSTR pPattern, PFNVBOXNETCFG_ENUMERATION_CALLBACK pfnCallback, PVOID pContext)384 {385 WIN32_FIND_DATA Data;386 memset(&Data, 0, sizeof(Data));387 HRESULT hr = S_OK;388 389 HANDLE hEnum = FindFirstFile(pPattern,&Data);390 if (hEnum != INVALID_HANDLE_VALUE)391 {392 393 do394 {395 if (!pfnCallback(Data.cFileName, pContext))396 {397 break;398 }399 400 /* next iteration */401 memset(&Data, 0, sizeof(Data));402 BOOL bNext = FindNextFile(hEnum,&Data);403 if (!bNext)404 {405 int winEr = GetLastError();406 if (winEr != ERROR_NO_MORE_FILES)407 {408 Log(__FUNCTION__": FindNextFile fail winEr (%d)\n", winEr);409 Assert(0);410 hr = HRESULT_FROM_WIN32(winEr);411 }412 break;413 }414 }while (true);415 FindClose(hEnum);416 }417 else418 {419 int winEr = GetLastError();420 if (winEr != ERROR_NO_MORE_FILES)421 {422 Log(__FUNCTION__": FindFirstFile fail winEr (%d)\n", winEr);423 Assert(0);424 hr = HRESULT_FROM_WIN32(winEr);425 }426 }427 428 return hr;429 }430 431 static bool vboxNetCfgWinInfEnumerationCallback(LPCWSTR lpszFileName, PVOID pCtxt)432 {433 PINFENUM_CONTEXT pContext = (PINFENUM_CONTEXT)pCtxt;434 DWORD winEr;435 // Log("vboxNetCfgWinInfEnumerationCallback: pFileName (%S)\n", pFileName);436 437 HINF hInf = SetupOpenInfFileW(lpszFileName, pContext->InfInfo.lpszClassName, INF_STYLE_WIN4, NULL /*__in PUINT ErrorLine */);438 if (hInf == INVALID_HANDLE_VALUE)439 {440 winEr = GetLastError();441 Assert(winEr == ERROR_CLASS_MISMATCH);442 if (winEr != ERROR_CLASS_MISMATCH)443 {444 DbgLog(__FUNCTION__ ": SetupOpenInfFileW err winEr (%d)\n", winEr);445 }446 447 return true;448 }449 450 LPWSTR lpszPnPId;451 HRESULT hr = vboxNetCfgWinInfQueryFirstPnPId(hInf, &lpszPnPId);452 if (hr == S_OK)453 {454 if (!wcsicmp(pContext->InfInfo.lpszPnPId, lpszPnPId))455 {456 if (!SetupUninstallOEMInfW(lpszFileName,457 pContext->Flags, /*DWORD Flags could be SUOI_FORCEDELETE */458 NULL /*__in PVOID Reserved == NULL */459 ))460 {461 winEr = GetLastError();462 Log(__FUNCTION__ ": SetupUninstallOEMInf failed for file (%S), winEr (%d)\n", lpszFileName, winEr);463 Assert(0);464 hr = HRESULT_FROM_WIN32( winEr );465 }466 }467 468 CoTaskMemFree(lpszPnPId);469 }470 else471 {472 DbgLog(__FUNCTION__ ": vboxNetCfgWinInfQueryFirstPnPId failed, hr = (0x%x)\n", hr);473 }474 475 SetupCloseInfFile(hInf);476 477 return true;478 }479 480 static HRESULT VBoxNetCfgWinInfUninstallAll(LPCWSTR lpszClassName, LPCWSTR lpszPnPId, DWORD Flags)481 {482 WCHAR InfDirPath[MAX_PATH];483 HRESULT hr = SHGetFolderPathW(NULL, /* HWND hwndOwner*/484 CSIDL_WINDOWS, /* int nFolder*/485 NULL, /*HANDLE hToken*/486 SHGFP_TYPE_CURRENT, /*DWORD dwFlags*/487 InfDirPath);488 Assert(hr == S_OK);489 if (hr == S_OK)490 {491 wcscat(InfDirPath, L"\\inf\\oem*.inf");492 493 INFENUM_CONTEXT Context;494 Context.InfInfo.lpszClassName = lpszClassName;495 Context.InfInfo.lpszPnPId = lpszPnPId;496 Context.Flags = Flags;497 Context.hr = S_OK;498 hr = vboxNetCfgWinEnumFiles(InfDirPath, vboxNetCfgWinInfEnumerationCallback, &Context);499 Assert(hr == S_OK);500 if (hr == S_OK)501 {502 hr = Context.hr;503 }504 else505 {506 Log(__FUNCTION__": vboxNetCfgWinEnumFiles failed, hr = (0x%x)\n", hr);507 }508 }509 else510 {511 Log(__FUNCTION__": SHGetFolderPathW failed, hr = (0x%x)\n", hr);512 }513 514 return hr;515 516 }517 57 518 58 static HRESULT vboxNetCfgWinINetCfgLock(IN INetCfg *pNetCfg, … … 773 313 for (; i < cInfPaths; i++) 774 314 { 775 hr = VBox NetCfgWinInfInstall(apInfPaths[i]);315 hr = VBoxDrvCfgInfInstall(apInfPaths[i]); 776 316 Assert(hr == S_OK); 777 317 if (hr != S_OK) … … 796 336 for (UINT j = i-1; j != 0; j--) 797 337 { 798 VBox NetCfgWinInfUninstall(apInfPaths[j], 0);338 VBoxDrvCfgInfUninstall(apInfPaths[j], 0); 799 339 } 800 340 } … … 2404 1944 } 2405 1945 2406 VBox NetCfgWinInfUninstallAll(L"NetService", VBOXNETCFGWIN_NETFLT_ID, InfRmFlags);2407 VBox NetCfgWinInfUninstallAll(L"Net", VBOXNETCFGWIN_NETFLT_MP_ID, InfRmFlags);1946 VBoxDrvCfgInfUninstallAllF(L"NetService", VBOXNETCFGWIN_NETFLT_ID, InfRmFlags); 1947 VBoxDrvCfgInfUninstallAllF(L"Net", VBOXNETCFGWIN_NETFLT_MP_ID, InfRmFlags); 2408 1948 2409 1949 return hr;
Note:
See TracChangeset
for help on using the changeset viewer.