static PWSTR pCDevSettings_GetDeviceInstanceId(const WCHAR *pszDevice) { DEVINST DevInst; CONFIGRET cr; ULONG BufLen; LPWSTR lpDevInstId = NULL; DPRINT1("CDevSettings::GetDeviceInstanceId(%ws) UNIMPLEMENTED!\n", pszDevice); cr = CM_Locate_DevNodeW(&DevInst, (DEVINSTID_W)pszDevice, CM_LOCATE_DEVNODE_NORMAL); if (cr == CR_SUCCESS) { DPRINT1("Success1\n"); cr = CM_Get_Device_ID_Size(&BufLen, DevInst, 0); if (cr == CR_SUCCESS) { DPRINT1("Success2\n"); lpDevInstId = LocalAlloc(LMEM_FIXED, (BufLen + 1) * sizeof(WCHAR)); if (lpDevInstId != NULL) { DPRINT1("Success3\n"); cr = CM_Get_Device_IDW(DevInst, lpDevInstId, BufLen, 0); if (cr != CR_SUCCESS) { LocalFree((HLOCAL)lpDevInstId); lpDevInstId = NULL; } DPRINT1("instance id: %ws\n", lpDevInstId); } } } return lpDevInstId; }
int removeNetworkInterface(MSIHANDLE hModule, const WCHAR *pwszGUID) { int rc = 1; do { WCHAR wszPnPInstanceId[512] = {0}; /* We have to find the device instance ID through a registry search */ HKEY hkeyNetwork = 0; HKEY hkeyConnection = 0; do /* break-loop */ { WCHAR wszRegLocation[256]; swprintf(wszRegLocation, L"SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s", pwszGUID); LONG lStatus = RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRegLocation, 0, KEY_READ, &hkeyNetwork); if ((lStatus != ERROR_SUCCESS) || !hkeyNetwork) SetErrBreak((L"VBox HostInterfaces: Host interface network was not found in registry (%s)! [1]", wszRegLocation)); lStatus = RegOpenKeyExW(hkeyNetwork, L"Connection", 0, KEY_READ, &hkeyConnection); if ((lStatus != ERROR_SUCCESS) || !hkeyConnection) SetErrBreak((L"VBox HostInterfaces: Host interface network was not found in registry (%s)! [2]", wszRegLocation)); DWORD len = sizeof(wszPnPInstanceId); DWORD dwKeyType; lStatus = RegQueryValueExW(hkeyConnection, L"PnPInstanceID", NULL, &dwKeyType, (LPBYTE)&wszPnPInstanceId[0], &len); if ((lStatus != ERROR_SUCCESS) || (dwKeyType != REG_SZ)) SetErrBreak((L"VBox HostInterfaces: Host interface network was not found in registry (%s)! [3]", wszRegLocation)); } while (0); if (hkeyConnection) RegCloseKey(hkeyConnection); if (hkeyNetwork) RegCloseKey(hkeyNetwork); /* * Now we are going to enumerate all network devices and * wait until we encounter the right device instance ID */ HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE; BOOL fResult; do { DWORD ret = 0; GUID netGuid; SP_DEVINFO_DATA DeviceInfoData; DWORD index = 0; DWORD size = 0; /* initialize the structure size */ DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); /* copy the net class GUID */ memcpy(&netGuid, &GUID_DEVCLASS_NET, sizeof (GUID_DEVCLASS_NET)); /* return a device info set contains all installed devices of the Net class */ hDeviceInfo = SetupDiGetClassDevs(&netGuid, NULL, NULL, DIGCF_PRESENT); if (hDeviceInfo == INVALID_HANDLE_VALUE) { logStringW(hModule, L"VBox HostInterfaces: SetupDiGetClassDevs failed (0x%08X)!", GetLastError()); SetErrBreak(L"VBox HostInterfaces: Uninstallation failed!"); } BOOL fFoundDevice = FALSE; /* enumerate the driver info list */ while (TRUE) { WCHAR *pwszDeviceHwid; fResult = SetupDiEnumDeviceInfo(hDeviceInfo, index, &DeviceInfoData); if (!fResult) { if (GetLastError() == ERROR_NO_MORE_ITEMS) break; else { index++; continue; } } /* try to get the hardware ID registry property */ fResult = SetupDiGetDeviceRegistryProperty(hDeviceInfo, &DeviceInfoData, SPDRP_HARDWAREID, NULL, NULL, 0, &size); if (!fResult) { if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { index++; continue; } pwszDeviceHwid = (WCHAR *)malloc(size); if (pwszDeviceHwid) { fResult = SetupDiGetDeviceRegistryProperty(hDeviceInfo, &DeviceInfoData, SPDRP_HARDWAREID, NULL, (PBYTE)pwszDeviceHwid, size, NULL); if (!fResult) { free(pwszDeviceHwid); pwszDeviceHwid = NULL; index++; continue; } } } else { /* something is wrong. This shouldn't have worked with a NULL buffer */ index++; continue; } for (WCHAR *t = pwszDeviceHwid; t && *t && t < &pwszDeviceHwid[size / sizeof(WCHAR)]; t += wcslen(t) + 1) { if (!_wcsicmp(L"vboxtap", t)) { /* get the device instance ID */ WCHAR wszDevID[MAX_DEVICE_ID_LEN]; if (CM_Get_Device_IDW(DeviceInfoData.DevInst, wszDevID, MAX_DEVICE_ID_LEN, 0) == CR_SUCCESS) { /* compare to what we determined before */ if (!wcscmp(wszDevID, wszPnPInstanceId)) { fFoundDevice = TRUE; break; } } } } if (pwszDeviceHwid) { free(pwszDeviceHwid); pwszDeviceHwid = NULL; } if (fFoundDevice) break; index++; } if (fFoundDevice) { fResult = SetupDiSetSelectedDevice(hDeviceInfo, &DeviceInfoData); if (!fResult) { logStringW(hModule, L"VBox HostInterfaces: SetupDiSetSelectedDevice failed (0x%08X)!", GetLastError()); SetErrBreak(L"VBox HostInterfaces: Uninstallation failed!"); } fResult = SetupDiCallClassInstaller(DIF_REMOVE, hDeviceInfo, &DeviceInfoData); if (!fResult) { logStringW(hModule, L"VBox HostInterfaces: SetupDiCallClassInstaller (DIF_REMOVE) failed (0x%08X)!", GetLastError()); SetErrBreak(L"VBox HostInterfaces: Uninstallation failed!"); } } else SetErrBreak(L"VBox HostInterfaces: Host interface network device not found!"); } while (0); /* clean up the device info set */ if (hDeviceInfo != INVALID_HANDLE_VALUE) SetupDiDestroyDeviceInfoList(hDeviceInfo); } while (0); return rc; }