int usb_install_needs_restart_np(void) { HDEVINFO dev_info; SP_DEVINFO_DATA dev_info_data; int dev_index = 0; SP_DEVINSTALL_PARAMS install_params; int ret = FALSE; dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA); dev_info = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT); SetEnvironmentVariable("LIBUSB_NEEDS_REBOOT", "1"); if(dev_info == INVALID_HANDLE_VALUE) { usb_error("usb_install_needs_restart_np(): getting " "device info set failed"); return ret; } while(SetupDiEnumDeviceInfo(dev_info, dev_index, &dev_info_data)) { memset(&install_params, 0, sizeof(SP_PROPCHANGE_PARAMS)); install_params.cbSize = sizeof(SP_DEVINSTALL_PARAMS); if(SetupDiGetDeviceInstallParams(dev_info, &dev_info_data, &install_params)) { if(install_params.Flags & (DI_NEEDRESTART | DI_NEEDREBOOT)) { usb_message("usb_install_needs_restart_np(): restart needed"); ret = TRUE; } } dev_index++; } SetupDiDestroyDeviceInfoList(dev_info); return ret; }
/* Find an attached dongle device and return the COM port name via the output * parameter tty. tty is a user-supplied buffer of size len. Return the COM * port number, if anyone cares. On error, return -1. */ int Mobot_dongleGetTTY (char *tty, size_t len) { /* Get all USB devices that provide a serial or parallel port interface. */ HDEVINFO devices = SetupDiGetClassDevs( &GUID_DEVCLASS_PORTS, "USB", NULL, DIGCF_PRESENT); if (INVALID_HANDLE_VALUE == devices) { win32_error(_T("SetupDiGetClassDevs"), GetLastError()); exit(1); } /* Now iterate over each device in the COM port interface class. */ SP_DEVINFO_DATA dev; dev.cbSize = sizeof(SP_DEVINFO_DATA); DWORD i = 0; BOOL b = SetupDiEnumDeviceInfo(devices, i, &dev); int ret = -1; while (b) { if (isDongle(devices, &dev)) { ret = getCOMPort(devices, &dev, tty, len); if (-1 == ret) { fprintf(stderr, "Found dongle, but could not get COM port\n"); exit(1); } /* Found the dongle. */ break; } /* And get the next device. */ dev.cbSize = sizeof(SP_DEVINFO_DATA); b = SetupDiEnumDeviceInfo(devices, ++i, &dev); } DWORD err = GetLastError(); if (ERROR_SUCCESS != err && ERROR_NO_MORE_ITEMS != err) { win32_error(_T("SetupDiEnumDeviceInfo"), GetLastError()); exit(1); } /* Done with our COM port devices. */ if (!SetupDiDestroyDeviceInfoList(devices)) { win32_error(_T("SetupDiDestroyDeviceInfoList"), GetLastError()); exit(1); } return ret; }
Status getDeviceList(const device_infoset_t& infoset, std::vector<SP_DEVINFO_DATA>& rDevices) { SP_DEVINSTALL_PARAMS installParams; ZeroMemory(&installParams, sizeof(SP_DEVINSTALL_PARAMS)); installParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS); installParams.FlagsEx |= DI_FLAGSEX_ALLOWEXCLUDEDDRVS | DI_FLAGSEX_INSTALLEDDRIVER; DWORD i = 0; BOOL devicesLeft = TRUE; do { SP_DEVINFO_DATA devInfo; devInfo.cbSize = sizeof(SP_DEVINFO_DATA); devicesLeft = SetupDiEnumDeviceInfo(infoset.get(), i, &devInfo); if (devicesLeft == TRUE) { // Set install params to make any subsequent driver enumerations on this // device more efficient SetupDiSetDeviceInstallParams(infoset.get(), &devInfo, &installParams); rDevices.push_back(devInfo); } i++; } while (devicesLeft == TRUE); auto err = GetLastError(); if (err != ERROR_NO_MORE_ITEMS) { rDevices.clear(); return Status(GetLastError(), "Error enumerating installed devices"); } return Status(); }
// //////////////////////////////////////////////////////////////////////////////// // @private StateChange // static BOOL StateChange(DWORD NewState, DWORD SelectedItem,HDEVINFO hDevInfo) { SP_PROPCHANGE_PARAMS PropChangeParams = {sizeof(SP_CLASSINSTALL_HEADER)}; SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)}; if ( !SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData) ) { return FALSE; } PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; PropChangeParams.Scope = DICS_FLAG_GLOBAL; PropChangeParams.StateChange = NewState; if ( !SetupDiSetClassInstallParams( hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER *)&PropChangeParams, sizeof(PropChangeParams)) ) { return FALSE; } if ( !SetupDiCallClassInstaller( DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData) ) { return TRUE; } return TRUE; }
BOOL getDeviceInfo(const GUID* category, CMIDEV* pDev) { TCHAR szServiceName[128]; int nIndex = 0; pDev->Info = SetupDiGetClassDevs(category, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (pDev->Info == INVALID_HANDLE_VALUE) { PrintLastError("SetupDiGetClassDevs()"); return FALSE; } pDev->InfoData.cbSize = sizeof(SP_DEVINFO_DATA); while (SetupDiEnumDeviceInfo(pDev->Info, nIndex, &(pDev->InfoData))) { if (!SetupDiGetDeviceRegistryProperty(pDev->Info, &(pDev->InfoData), SPDRP_SERVICE, NULL, (PBYTE)szServiceName, sizeof(szServiceName), NULL)) { PrintLastError("SetupDiGetDeviceRegistryProperty()"); SetupDiDestroyDeviceInfoList(pDev->Info); pDev->Info = NULL; return FALSE; } if (CompareString(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), NORM_IGNORECASE, szServiceName, -1, TEXT("cmipci"), -1) == CSTR_EQUAL) { return TRUE; } nIndex++; } SetupDiDestroyDeviceInfoList(pDev->Info); pDev->Info = NULL; return FALSE; }
nsresult sbWinGetDeviceInstanceIDFromDeviceInterfaceName(nsAString& aDeviceInterfaceName, nsAString& aDeviceInstanceID) { BOOL success; nsresult rv; // Create a device info set and set it up for auto-disposal. HDEVINFO devInfoSet = SetupDiCreateDeviceInfoList(NULL, NULL); NS_ENSURE_TRUE(devInfoSet != INVALID_HANDLE_VALUE, NS_ERROR_FAILURE); sbAutoHDEVINFO autoDevInfoSet(devInfoSet); // Add the device interface data, including the device info data, to the // device info set. SP_DEVICE_INTERFACE_DATA devIfData; ZeroMemory(&devIfData, sizeof(SP_DEVICE_INTERFACE_DATA)); devIfData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); success = SetupDiOpenDeviceInterfaceW(devInfoSet, aDeviceInterfaceName.BeginReading(), 0, &devIfData); NS_ENSURE_TRUE(success, NS_ERROR_FAILURE); // Get the device info data. SP_DEVINFO_DATA devInfoData; devInfoData.cbSize = sizeof(SP_DEVINFO_DATA); success = SetupDiEnumDeviceInfo(devInfoSet, 0, &devInfoData); NS_ENSURE_TRUE(success, NS_ERROR_FAILURE); // Get the device instance ID. rv = sbWinGetDeviceInstanceID(devInfoData.DevInst, aDeviceInstanceID); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; }
void FillDeviceInstanceId(HDEVINFO hDevHandle, SP_DEVINFO_DATA deviceInfoData, WCHAR *wAdapter) { DWORD i; for (i = 0; SetupDiEnumDeviceInfo(hDevHandle, i, &deviceInfoData); i++) { BYTE buffer[BUFFER_SIZE]; WCHAR deviceInfo[BUFFER_SIZE]; DWORD dwProperty = 0; memset(buffer, 0, BUFFER_SIZE); if (SetupDiGetDeviceRegistryProperty(hDevHandle, &deviceInfoData, SPDRP_DEVICEDESC , &dwProperty, buffer, BUFFER_SIZE, NULL) == TRUE) { wcscpy_s(deviceInfo, BUFFER_SIZE, (LPWSTR)buffer); if (wcscmp(wAdapter, deviceInfo) == 0) { RtlZeroMemory(wDeviceInstanceId, MAX_PATH); SetupDiGetDeviceInstanceId(hDevHandle, &deviceInfoData, wDeviceInstanceId, MAX_PATH, 0); } } } }
nsresult sbWinGetDevInfoData(DEVINST aDevInst, HDEVINFO aDevInfo, PSP_DEVINFO_DATA aDevInfoData) { // Validate arguments. NS_ENSURE_ARG_POINTER(aDevInfoData); // Function variables. BOOL success; // Find the device info data for the device instance. DWORD devIndex = 0; while (1) { // Get the next device info data. aDevInfoData->cbSize = sizeof(SP_DEVINFO_DATA); success = SetupDiEnumDeviceInfo(aDevInfo, devIndex, aDevInfoData); if (!success) return NS_ERROR_NOT_AVAILABLE; // The search is done if the device info data is for the device instance. if (aDevInfoData->DevInst == aDevInst) break; // Check the next device info data. devIndex++; } return NS_OK; }
void Serial::enumerateWin32Ports() { if (bPortsEnumerated == true) return; HDEVINFO hDevInfo = NULL; SP_DEVINFO_DATA DeviceInterfaceData; int i = 0; DWORD dataType, actualSize = 0; unsigned char dataBuf[MAX_PATH + 1]; // Reset Port List nPorts = 0; // Search device set hDevInfo = SetupDiGetClassDevs((struct _GUID *)&GUID_SERENUM_BUS_ENUMERATOR,0,0,DIGCF_PRESENT); if ( hDevInfo ){ while (TRUE){ ZeroMemory(&DeviceInterfaceData, sizeof(DeviceInterfaceData)); DeviceInterfaceData.cbSize = sizeof(DeviceInterfaceData); if (!SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInterfaceData)){ // SetupDiEnumDeviceInfo failed break; } if (SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInterfaceData, SPDRP_FRIENDLYNAME, &dataType, dataBuf, sizeof(dataBuf), &actualSize)){ sprintf(portNamesFriendly[nPorts], "%s", dataBuf); portNamesShort[nPorts][0] = 0; // turn blahblahblah(COM4) into COM4 char * begin = NULL; char * end = NULL; begin = strstr((char *)dataBuf, "COM"); if (begin) { end = strstr(begin, ")"); if (end) { *end = 0; // get rid of the )... strcpy(portNamesShort[nPorts], begin); } if (nPorts++ > MAX_SERIAL_PORTS) break; } } i++; } } SetupDiDestroyDeviceInfoList(hDevInfo); bPortsEnumerated = false; }
bool QextSerialEnumerator::matchAndDispatchChangedDevice(const QString & deviceID, const GUID & guid, WPARAM wParam) { bool rv = false; DWORD dwFlag = (DBT_DEVICEARRIVAL == wParam) ? DIGCF_PRESENT : DIGCF_ALLCLASSES; HDEVINFO devInfo; if( (devInfo = SetupDiGetClassDevs(&guid,NULL,NULL,dwFlag)) != INVALID_HANDLE_VALUE ) { SP_DEVINFO_DATA spDevInfoData; spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for(int i=0; SetupDiEnumDeviceInfo(devInfo, i, &spDevInfoData); i++) { DWORD nSize=0 ; TCHAR buf[MAX_PATH]; if ( SetupDiGetDeviceInstanceId(devInfo, &spDevInfoData, buf, MAX_PATH, &nSize) && deviceID.contains(TCHARToQString(buf))) // we found a match { rv = true; QextPortInfo info; info.productID = info.vendorID = 0; getDeviceDetailsWin( &info, devInfo, &spDevInfoData, wParam ); if( wParam == DBT_DEVICEARRIVAL ) emit deviceDiscovered(info); else if( wParam == DBT_DEVICEREMOVECOMPLETE ) emit deviceRemoved(info); break; } } SetupDiDestroyDeviceInfoList(devInfo); } return rv; }
static int ti_reset(struct tip_cygwin *priv) { HDEVINFO hdi; SP_DEVINFO_DATA did; int i; int rc = -1; hdi = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT); if (hdi == INVALID_HANDLE_VALUE) return -1; /* find device */ for (i = 0;; i++) { did.cbSize = sizeof(did); if (!SetupDiEnumDeviceInfo(hdi, i, &did)) break; if (!ti_is_us(priv, &hdi, &did)) continue; rc = ti_do_reset(&hdi, &did); if (rc) break; rc = ti_restart(priv); break; } SetupDiDestroyDeviceInfoList(hdi); return rc; }
// Code for enumerating hardware devices that use the XINPUT device driver. // The MSDN recommended code suffers from massive performance problems when using language packs, // if the system and user languages differ then WMI Queries become incredibly slow (multiple // seconds). This is more or less equivalent and much faster. std::unordered_set<DWORD> GetXInputGUIDS() { static const GUID s_GUID_devclass_HID = { 0x745a17a0, 0x74d3, 0x11d0, {0xb6, 0xfe, 0x00, 0xa0, 0xc9, 0x0f, 0x57, 0xda} }; std::unordered_set<DWORD> guids; // Enumerate everything under the "Human Interface Devices" tree in the Device Manager // NOTE: Some devices show up multiple times due to sub-devices, we rely on the set to // prevent duplicates. HDEVINFO setup_enum = SetupDiGetClassDevsW(&s_GUID_devclass_HID, nullptr, nullptr, DIGCF_PRESENT); if (setup_enum == INVALID_HANDLE_VALUE) return guids; std::vector<wchar_t> buffer(128); SP_DEVINFO_DATA dev_info; dev_info.cbSize = sizeof(SP_DEVINFO_DATA); for (DWORD i = 0; SetupDiEnumDeviceInfo(setup_enum, i, &dev_info); ++i) { // Need to find the size of the data and set the buffer appropriately DWORD buffer_size = 0; while (!SetupDiGetDeviceRegistryPropertyW(setup_enum, &dev_info, SPDRP_HARDWAREID, nullptr, reinterpret_cast<BYTE*>(buffer.data()), static_cast<DWORD>(buffer.size()), &buffer_size)) { if (buffer_size > buffer.size()) buffer.resize(buffer_size); else break; } if (GetLastError() != ERROR_SUCCESS) continue; // HARDWAREID is a REG_MULTI_SZ // There are multiple strings separated by NULs, the list is ended by an empty string. for (std::size_t j = 0; buffer[j]; j += std::wcslen(&buffer[j]) + 1) { // XINPUT devices have "IG_xx" embedded in their IDs which is what we look for. if (!std::wcsstr(&buffer[j], L"IG_")) continue; unsigned int vid = 0; unsigned int pid = 0; // Extract Vendor and Product IDs for matching against DirectInput's device list. wchar_t* pos = std::wcsstr(&buffer[j], L"VID_"); if (!pos || !std::swscanf(pos, L"VID_%4X", &vid)) continue; pos = std::wcsstr(&buffer[j], L"PID_"); if (!pos || !std::swscanf(pos, L"PID_%4X", &pid)) continue; guids.insert(MAKELONG(vid, pid)); break; } } SetupDiDestroyDeviceInfoList(setup_enum); return guids; }
/* * Flag phantom/removed devices for reinstallation. See: * http://msdn.microsoft.com/en-us/library/aa906206.aspx */ void check_removed(char* device_hardware_id) { unsigned i, removed = 0; DWORD size, reg_type, config_flags; ULONG status, pbm_number; HDEVINFO dev_info; SP_DEVINFO_DATA dev_info_data; char hardware_id[STR_BUFFER_SIZE]; // List all known USB devices (including non present ones) dev_info = SetupDiGetClassDevsA(NULL, "USB", NULL, DIGCF_ALLCLASSES); if (dev_info == INVALID_HANDLE_VALUE) { return; } // Find the ones that are driverless for (i = 0; ; i++) { dev_info_data.cbSize = sizeof(dev_info_data); if (!SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data)) { break; } // Find the hardware ID if (!SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_HARDWAREID, ®_type, (BYTE*)hardware_id, STR_BUFFER_SIZE, &size)) { continue; } // Match? if (safe_strncmp(hardware_id, device_hardware_id, STR_BUFFER_SIZE) != 0) { continue; } // Unplugged? if (CM_Get_DevNode_Status(&status, &pbm_number, dev_info_data.DevInst, 0) != CR_NO_SUCH_DEVNODE) { continue; } // Flag for reinstall on next plugin if (!SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_CONFIGFLAGS, ®_type, (BYTE*)&config_flags, sizeof(DWORD), &size)) { plog("could not read SPDRP_CONFIGFLAGS for phantom device %s", hardware_id); continue; } config_flags |= CONFIGFLAG_REINSTALL; if (!SetupDiSetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_CONFIGFLAGS, (BYTE*)&config_flags, sizeof(DWORD))) { plog("could not write SPDRP_CONFIGFLAGS for phantom device %s", hardware_id); continue; } removed++; } if (removed) { plog("flagged %d removed devices for reinstallation", removed); } }
void uart_list_devices() { char name[]="Bluegiga Bluetooth Low Energy"; BYTE* pbuf = NULL; DWORD reqSize = 0; DWORD n=0; HDEVINFO hDevInfo; //guid for ports static const GUID guid = { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } }; char *str; char tmp[MAX_PATH+1]; int i; SP_DEVINFO_DATA DeviceInfoData; snprintf(tmp,MAX_PATH,"%s (COM%%d)",name); DeviceInfoData.cbSize=sizeof(SP_DEVINFO_DATA); hDevInfo = SetupDiGetClassDevs(&guid, //Retrieve all ports 0L, NULL, DIGCF_PRESENT ); if(hDevInfo==INVALID_HANDLE_VALUE) return; while(1) { if(!SetupDiEnumDeviceInfo( hDevInfo, n++, &DeviceInfoData )) { SetupDiDestroyDeviceInfoList(hDevInfo); return; } reqSize = 0; SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, NULL, 0, &reqSize); pbuf = (BYTE*)malloc(reqSize>1?reqSize:1); if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, pbuf, reqSize, NULL)) { free(pbuf); continue; } str = (char*)pbuf; if(sscanf(str,tmp,&i)==1) { printf("%s\n", str); //emit DeviceFound(str,QString("\\\\.\\COM%1").arg(i)); } free(pbuf); } return; }
void SMNetworkAdapterSettings::_FingSetupInfo(HDEVINFO* devInfo, SP_DEVINFO_DATA* devInfoData) const { SM_ASSERT( !_hardwareId.IsEmpty() ); SMString hwID(_hardwareId); hwID.MakeUpper(); *devInfo = ::SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT); if( INVALID_HANDLE_VALUE == *devInfo ) throw SMRuntimeException(::GetLastError()); ::ZeroMemory(devInfoData, sizeof(SP_DEVINFO_DATA)); devInfoData->cbSize = sizeof(SP_DEVINFO_DATA); PTSTR buf = NULL; DWORD bufSize = 0; DWORD reqSize = 0; for(int i=0; SetupDiEnumDeviceInfo(*devInfo,i, devInfoData); i++) { if ( !SetupDiGetDeviceInstanceId(*devInfo, devInfoData, buf, bufSize, &reqSize) ) { if ( bufSize < reqSize ) { if ( NULL != buf ) { delete[] buf; buf = NULL; bufSize = 0; } buf = new TCHAR[reqSize]; if ( NULL == buf ) { SM_LOG(1, E_FAIL, SMString("Insufficient memory : ") + _com_error(GetLastError()).ErrorMessage()); break; } bufSize = reqSize; if ( !SetupDiGetDeviceInstanceId(*devInfo, devInfoData, buf, bufSize, &reqSize) ) throw SMRuntimeException(::GetLastError()); } else { SM_LOG(1, E_FAIL, SMString("SetupDiGetDeviceInstanceId(): ") + _com_error(GetLastError()).ErrorMessage()); break; } } SMString szBuf = buf; szBuf.MakeUpper(); if ( szBuf.Find(hwID.DataW()) != -1 ) return; } }
int uart_find_serialport(char *name) { BYTE* pbuf = NULL; DWORD reqSize = 0; DWORD n=0; HDEVINFO hDevInfo; //guid for ports static const GUID guid = { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } }; char *str; char tmp[MAX_PATH+1]; int i; SP_DEVINFO_DATA DeviceInfoData; snprintf(tmp,MAX_PATH,"%s (COM%%d)",name); DeviceInfoData.cbSize=sizeof(SP_DEVINFO_DATA); hDevInfo = SetupDiGetClassDevs(&guid, //Retrieve all ports 0L, NULL, DIGCF_PRESENT ); if(hDevInfo==INVALID_HANDLE_VALUE) return -1; while(1) { if(!SetupDiEnumDeviceInfo( hDevInfo, n++, &DeviceInfoData )) { SetupDiDestroyDeviceInfoList(hDevInfo); return -1; } reqSize = 0; SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, NULL, 0, &reqSize); pbuf = malloc(reqSize>1?reqSize:1); if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, pbuf, reqSize, NULL)) { free(pbuf); continue; } str = (char*)pbuf; if(sscanf(str,tmp,&i)==1) { free(pbuf); SetupDiDestroyDeviceInfoList(hDevInfo); return i; } free(pbuf); } return -1; }
int main( int argc, char *argv[ ], char *envp[ ] ) { HDEVINFO hDevInfo; SP_DEVINFO_DATA DeviceInfoData; DWORD i; /* hDevInfo = SetupDiGetClassDevs(NULL, REGSTR_KEY_PCIENUM, // Enumerator 0, DIGCF_PRESENT | DIGCF_ALLCLASSES ); */ // Create a HDEVINFO with all present devices. hDevInfo = SetupDiGetClassDevs( &GUID_DEVINTERFACE_TAPE, 0, // Enumerator 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); // DIGCF_PRESENT | DIGCF_ALLCLASSES ); if (hDevInfo == INVALID_HANDLE_VALUE) { // Insert error handling here. return 1; } // Enumerate through all devices in Set. DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for (i=0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) { show_property(hDevInfo, &DeviceInfoData, SPDRP_DEVTYPE, "Type"); show_property(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, "Description"); show_property(hDevInfo, &DeviceInfoData, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, "PhysDevName"); } if ( GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS ) { // Insert error handling here. return 1; } // Cleanup SetupDiDestroyDeviceInfoList(hDevInfo); return 0; }
QList<SerialPortId> enumerateSerialPorts(int) { DWORD index=0; SP_DEVINFO_DATA info; GUID guid = GUID_DEVCLASS_PORTS; HDEVINFO infoset = SetupDiGetClassDevs(&guid, 0, 0, DIGCF_PRESENT); QString valueName(16384, 0); QList<SerialPortId> list; for (index=0;;index++) { ZeroMemory(&info, sizeof(SP_DEVINFO_DATA)); info.cbSize = sizeof(SP_DEVINFO_DATA); if (!SetupDiEnumDeviceInfo(infoset, index, &info)) break; QString friendlyName; QString portName; DWORD size=0; SetupDiGetDeviceRegistryProperty(infoset, &info, SPDRP_FRIENDLYNAME, 0, 0, 0, &size); QByteArray ba(size, 0); if(SetupDiGetDeviceRegistryProperty(infoset, &info, SPDRP_FRIENDLYNAME, 0, (BYTE*)(ba.data()), size, 0)) { friendlyName = QString((const QChar*)(ba.constData()), ba.size() / 2 - 1); } HKEY key = SetupDiOpenDevRegKey(infoset, &info, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); if(key != INVALID_HANDLE_VALUE) { //RegGetValue not supported on XP, SHRegGetValue not supported by mingw, so use the old method of enumerating all the values for (DWORD dwi=0;;dwi++) { DWORD vsize = valueName.size(); if (ERROR_SUCCESS == RegEnumValue(key, dwi, (WCHAR*)(valueName.data()), &vsize, 0, 0, 0, &size)) { if (valueName.startsWith("PortName")) { QByteArray ba(size, 0); vsize = valueName.size(); if(ERROR_SUCCESS == RegEnumValue(key, dwi, (WCHAR*)(valueName.data()), &vsize, 0, 0, (BYTE*)(ba.data()), &size)) { portName = QString((const QChar*)(ba.constData()), ba.size() / 2 - 1); } } } else { break; } } RegCloseKey(key); } SerialPortId id; id.portName = portName; id.friendlyName = friendlyName; list.append(id); } SetupDiDestroyDeviceInfoList(infoset); return list; }
std::string WinPortFactory::next() { int rc; BYTE devName[16]; SP_DEVINFO_DATA devData; HKEY devKey; DWORD len; if (_devInfo == INVALID_HANDLE_VALUE) return end(); while (1) { devData.cbSize = sizeof(SP_DEVINFO_DATA); if (!SetupDiEnumDeviceInfo(_devInfo, _devNum, &devData)) return error(); rc = _devNode(devData.DevInst, KEY_QUERY_VALUE, 0, 1, &devKey, 0); if (rc != ERROR_SUCCESS) return error(); len = sizeof(devName); rc = RegQueryValueEx(devKey, "portname", NULL, NULL, devName, &len); RegCloseKey(devKey); if (rc != ERROR_SUCCESS) return error(); _devNum++; if (strncmp("COM", (char*) devName, 3) == 0) break; } return std::string((char*) devName, len); }
void QextSerialEnumerator::enumerateDevicesWin(const GUID & guid, QList<QextPortInfo> *infoList) { HDEVINFO devInfo; if ((devInfo = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT)) != INVALID_HANDLE_VALUE) { SP_DEVINFO_DATA devInfoData; devInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for (int i = 0; SetupDiEnumDeviceInfo(devInfo, i, &devInfoData); i++) { QextPortInfo info; info.productID = info.vendorID = 0; getDeviceDetailsWin(&info, devInfo, &devInfoData); infoList->append(info); } SetupDiDestroyDeviceInfoList(devInfo); } }
static void SetDeviceDetails(HWND * hDlgCtrls, LPCGUID classGUID, LPGUID * deviceGUID) { HDEVINFO hInfo; DWORD dwIndex = 0; SP_DEVINFO_DATA InfoData; WCHAR szText[100]; /* create the setup list */ hInfo = SetupDiGetClassDevsW(classGUID, NULL, NULL, DIGCF_PRESENT|DIGCF_PROFILE); if (hInfo == INVALID_HANDLE_VALUE) return; do { ZeroMemory(&InfoData, sizeof(InfoData)); InfoData.cbSize = sizeof(InfoData); if (SetupDiEnumDeviceInfo(hInfo, dwIndex, &InfoData)) { /* set device name */ if (SetupDiGetDeviceRegistryPropertyW(hInfo, &InfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szText, sizeof(szText), NULL)) SendMessageW(hDlgCtrls[0], WM_SETTEXT, 0, (LPARAM)szText); /* set the manufacturer name */ if (SetupDiGetDeviceRegistryPropertyW(hInfo, &InfoData, SPDRP_MFG, NULL, (PBYTE)szText, sizeof(szText), NULL)) SendMessageW(hDlgCtrls[1], WM_SETTEXT, 0, (LPARAM)szText); /* FIXME * we currently enumerate only the first adapter */ EnumerateDrivers(&hDlgCtrls[2], hInfo, &InfoData); break; } if (GetLastError() == ERROR_NO_MORE_ITEMS) break; dwIndex++; }while(TRUE); /* destroy the setup list */ SetupDiDestroyDeviceInfoList(hInfo); }
static void test_SetupDiGetClassDevsExW(void) { const GUID not_existing_guid = { 0xdeadbeef, 0xdead, 0xbeef, { 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff } }; HDEVINFO dev_info; BOOL ret; SP_DEVINFO_DATA info; dev_info = SetupDiGetClassDevsExW(¬_existing_guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE, NULL, NULL, NULL); ok(dev_info != INVALID_HANDLE_VALUE, "Expected success\n"); ZeroMemory(&info, sizeof(info)); info.cbSize = sizeof(info); ret = SetupDiEnumDeviceInfo(dev_info, 0, &info); ok (!ret, "Expected failure.\n"); ok_lasterr(ERROR_NO_MORE_ITEMS); SetupDiDestroyDeviceInfoList(dev_info); }
void WinNetCard::EnumNetCards(NetCardList *NetDeviceList) { CString DevValue; DWORD Status, Problem; LPTSTR Buffer = NULL; DWORD BufSize = 0; HDEVINFO hDevInfo = 0; if( INVALID_HANDLE_VALUE == (hDevInfo = SetupDiGetClassDevs(NULL,NULL,0,DIGCF_PRESENT|DIGCF_ALLCLASSES))) return; SP_DEVINFO_DATA DeviceInfoData ={sizeof(SP_DEVINFO_DATA)}; PNetCardStruct NetCard = new TNetCardStruct; for(DWORD DeviceId=0;SetupDiEnumDeviceInfo(hDevInfo,DeviceId,&DeviceInfoData);DeviceId++) { if (CM_Get_DevNode_Status(&Status, &Problem, DeviceInfoData.DevInst,0) != CR_SUCCESS) continue; if(GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASS , &Buffer, (PULONG)&BufSize)) DevValue = Buffer; if (DevValue == _T("Net")) { if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_ENUMERATOR_NAME , &Buffer, (PULONG)&BufSize)) DevValue = Buffer; if (/*DevValue != "ROOT"*/1) { NetCard->Id = DeviceId; NetCard->Name = "<Unknown Device>"; if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DRIVER , &Buffer, (PULONG)&BufSize)) if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC , &Buffer, (PULONG)&BufSize)) NetCard->Name = Buffer; NetCard->Disabled = (Status & DN_HAS_PROBLEM) && (CM_PROB_DISABLED == Problem); NetCard->Changed = false; NetDeviceList->push_back(*NetCard); } } } delete NetCard; }
bool GetSizeForDevID(wxString &TargetDevID, int *WidthMm, int *HeightMm) { HDEVINFO devInfo = SetupDiGetClassDevsEx( &GUID_CLASS_MONITOR, //class GUID NULL, //enumerator NULL, //HWND DIGCF_PRESENT, // Flags //DIGCF_ALLCLASSES| NULL, // device info, create a new one. NULL, // machine name, local machine NULL);// reserved if (NULL == devInfo) return false; bool bRes = false; for (ULONG i=0; ERROR_NO_MORE_ITEMS != GetLastError(); ++i) { SP_DEVINFO_DATA devInfoData; memset(&devInfoData,0,sizeof(devInfoData)); devInfoData.cbSize = sizeof(devInfoData); if (SetupDiEnumDeviceInfo(devInfo,i,&devInfoData)) { wchar_t Instance[80]; SetupDiGetDeviceInstanceId(devInfo, &devInfoData, Instance, MAX_PATH, NULL); wxString instance(Instance); if(instance.Upper().Find( TargetDevID.Upper() ) == wxNOT_FOUND ) continue; HKEY hDevRegKey = SetupDiOpenDevRegKey(devInfo,&devInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); if(!hDevRegKey || (hDevRegKey == INVALID_HANDLE_VALUE)) continue; bRes = GetMonitorSizeFromEDID(hDevRegKey, WidthMm, HeightMm); RegCloseKey(hDevRegKey); } } SetupDiDestroyDeviceInfoList(devInfo); return bRes; }
void AddRmForAllDevice(DWORD AffinityMask, BOOL fAdd, int targetIndex) { HDEVINFO hDevInfo = INVALID_HANDLE_VALUE; int deviceIndex; SP_DEVINFO_DATA devInfoData; devInfoData.cbSize = sizeof(SP_DEVINFO_DATA); //API required. do { hDevInfo = SetupDiGetClassDevs( NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT | DIGCF_PROFILE ); if (hDevInfo == INVALID_HANDLE_VALUE) { Warn("unable to get a list of devices.\n"); break; } for ( deviceIndex = 0; SetupDiEnumDeviceInfo(hDevInfo, deviceIndex, &devInfoData); deviceIndex++) { if ((targetIndex < 0) || (targetIndex == deviceIndex)) { if (fAdd) AddFilterForDevice(hDevInfo, &devInfoData, AffinityMask); else RmFilterForDevice(hDevInfo, &devInfoData); } } } while (FALSE); if (hDevInfo != INVALID_HANDLE_VALUE) { SetupDiDestroyDeviceInfoList(hDevInfo); hDevInfo = INVALID_HANDLE_VALUE; } }
BOOL FindNetworkAdapter(HDEVINFO hInfo, SP_DEVINFO_DATA *pDevInfo, LPWSTR pGuid) { DWORD dwIndex, dwSize; HKEY hSubKey; WCHAR szNetCfg[50]; WCHAR szDetail[200] = L"SYSTEM\\CurrentControlSet\\Control\\Class\\"; dwIndex = 0; do { ZeroMemory(pDevInfo, sizeof(SP_DEVINFO_DATA)); pDevInfo->cbSize = sizeof(SP_DEVINFO_DATA); /* get device info */ if (!SetupDiEnumDeviceInfo(hInfo, dwIndex++, pDevInfo)) break; /* get device software registry path */ if (!SetupDiGetDeviceRegistryPropertyW(hInfo, pDevInfo, SPDRP_DRIVER, NULL, (LPBYTE)&szDetail[39], sizeof(szDetail)/sizeof(WCHAR) - 40, &dwSize)) break; /* open device registry key */ if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szDetail, 0, KEY_READ, &hSubKey) != ERROR_SUCCESS) break; /* query NetCfgInstanceId for current device */ dwSize = sizeof(szNetCfg); if (RegQueryValueExW(hSubKey, L"NetCfgInstanceId", NULL, NULL, (LPBYTE)szNetCfg, &dwSize) != ERROR_SUCCESS) { RegCloseKey(hSubKey); break; } RegCloseKey(hSubKey); if (!_wcsicmp(pGuid, szNetCfg)) { return TRUE; } } while (TRUE); return FALSE; }
LRESULT QextSerialEnumerator::onDeviceChangeWin( WPARAM wParam, LPARAM lParam ) { if ( DBT_DEVICEARRIVAL == wParam || DBT_DEVICEREMOVECOMPLETE == wParam ) { PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam; if( pHdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE ) { PDEV_BROADCAST_DEVICEINTERFACE pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)pHdr; QString devId = TCHARToQString(pDevInf->dbcc_name); // devId: \\?\USB#Vid_04e8&Pid_503b#0002F9A9828E0F06#{a5dcbf10-6530-11d2-901f-00c04fb951ed} devId.remove("\\\\?\\"); // USB#Vid_04e8&Pid_503b#0002F9A9828E0F06#{a5dcbf10-6530-11d2-901f-00c04fb951ed} devId.remove(QRegExp("#\\{(.+)\\}")); // USB#Vid_04e8&Pid_503b#0002F9A9828E0F06 devId.replace("#", "\\"); // USB\Vid_04e8&Pid_503b\0002F9A9828E0F06 devId = devId.toUpper(); //qDebug() << "devname:" << devId; DWORD dwFlag = DBT_DEVICEARRIVAL == wParam ? (DIGCF_ALLCLASSES | DIGCF_PRESENT) : DIGCF_ALLCLASSES; HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS,NULL,NULL,dwFlag); SP_DEVINFO_DATA spDevInfoData; spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for(int i=0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++) { DWORD nSize=0 ; TCHAR buf[MAX_PATH]; if ( !SetupDiGetDeviceInstanceId(hDevInfo, &spDevInfoData, buf, sizeof(buf), &nSize) ) qDebug() << "SetupDiGetDeviceInstanceId():" << GetLastError(); if( devId == TCHARToQString(buf) ) // we found a match { QextPortInfo info; getDeviceDetailsWin( &info, hDevInfo, &spDevInfoData, wParam ); if( wParam == DBT_DEVICEARRIVAL ) emit deviceDiscovered(info); else if( wParam == DBT_DEVICEREMOVECOMPLETE ) emit deviceRemoved(info); break; } } SetupDiDestroyDeviceInfoList(hDevInfo); } } return 0; }
void vm_sys_info_get_vga_card(vm_char *vga_card) { SP_DEVINFO_DATA sp_dev_info; HDEVINFO DeviceInfoSet; vm_char string1[] = VM_STRING("Display"); Ipp32u dwIndex = 0; vm_char data[_MAX_LEN] = {0,}; /* check error(s) */ if (NULL == vga_card) return; ZeroMemory(&sp_dev_info, sizeof(SP_DEVINFO_DATA)); ZeroMemory(&DeviceInfoSet, sizeof(HDEVINFO)); DeviceInfoSet = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT); sp_dev_info.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); while (SetupDiEnumDeviceInfo(DeviceInfoSet, dwIndex, &sp_dev_info)) { SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &sp_dev_info, SPDRP_CLASS, NULL, (Ipp8u *) data, _MAX_LEN, NULL); if (!vm_string_strcmp((vm_char*)data, string1)) { SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &sp_dev_info, SPDRP_DEVICEDESC, NULL, (PBYTE) vga_card, _MAX_LEN, NULL); break; } dwIndex++; } SetupDiDestroyDeviceInfoList(DeviceInfoSet); } /* void vm_sys_info_get_vga_card(vm_char *vga_card) */
void enum_mobile_devices(pfc::list_t<device_instance_info_t> & p_out) { HDEVINFO di = SetupDiGetClassDevs(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE); if (di != INVALID_HANDLE_VALUE) { SP_DEVINFO_DATA did; memset(&did, 0, sizeof(did)); did.cbSize = sizeof(did); DWORD i; for (i=0; SetupDiEnumDeviceInfo(di, i, &did); i++) { //if (did.ClassGuid == GUID_DEVCLASS_USB) { ULONG DevDiskLen=0; pfc::array_t<WCHAR> DevDisk; if (CR_SUCCESS == CM_Get_Device_ID_Size(&DevDiskLen, did.DevInst, NULL)) { DevDisk.set_size(DevDiskLen+1); DevDisk.fill_null(); if (CR_SUCCESS == CM_Get_Device_ID(did.DevInst, DevDisk.get_ptr(), DevDisk.get_size(), NULL)) { if (g_check_devid_is_mobile_device(DevDisk.get_ptr())) { device_instance_info_t temp; temp.m_handle = did.DevInst; temp.m_path = DevDisk.get_ptr(); p_out.add_item(temp); console::formatter() << "iPod manager: USB AMD enumerator: Found " << Tu(DevDisk.get_ptr()); } } } } } SetupDiDestroyDeviceInfoList(di); } if (!p_out.get_count()) console::formatter() << "iPod manager: USB AMD enumerator: No devices found!"; }
QStringList QPCSC::drivers() const { #ifdef Q_OS_WIN HDEVINFO h = SetupDiGetClassDevs( 0, 0, 0, DIGCF_ALLCLASSES | DIGCF_PRESENT ); if( !h ) return QStringList(); SP_DEVINFO_DATA info = { sizeof(SP_DEVINFO_DATA) }; QStringList list; for( DWORD i = 0; SetupDiEnumDeviceInfo( h, i, &info ); i++ ) { DWORD size = 0; WCHAR data[1024]; SetupDiGetDeviceRegistryPropertyW( h, &info, SPDRP_CLASS, 0, LPBYTE(data), sizeof(data), &size ); if( _wcsicmp( data, L"SmartCardReader" ) != 0 ) continue; DWORD conf = 0; SetupDiGetDeviceRegistryPropertyW( h, &info, SPDRP_CONFIGFLAGS, 0, LPBYTE(&conf), sizeof(conf), &size ); if( conf & CONFIGFLAG_DISABLED ) continue; SetupDiGetDeviceRegistryPropertyW( h, &info, SPDRP_DEVICEDESC, 0, LPBYTE(data), sizeof(data), &size ); QString name( (QChar*)data ); SetupDiGetDeviceRegistryPropertyW( h, &info, SPDRP_HARDWAREID, 0, LPBYTE(data), sizeof(data), &size ); list << QString( "%1 (%2)").arg( name, QString( (QChar*)data ) ); } SetupDiDestroyDeviceInfoList( h ); return list; #else return readers(); #endif }