我正在尝试使用SetupDi函数枚举所有连接的USB设备的设备路径。设备路径是CreateFile()中使用的路径,因此我可以与设备通信。
但是,SetupDiGetDeviceInterface需要接口GUID,但我不是专门寻找特定接口(除了所有连接的USB)。这部分被评为/ * ??? * /在下面的来源。
尝试解决方案:
我试图提供GUID_DEVCLASS_UNKNOWN = {0x4d36e97e,0xe325,0x11ce,{0xbf,0xc1,0x08,0x00,0x2b,0xe1,0x03,0x18}};但这引起了“没有更多接口”的错误。
我也尝试将deviceInfoData.ClassGuid提供给SetupDiGetDeviceInterface,但是我得到了与上面相同的错误,“没有更多接口”。
问题:
是否有涵盖所有USB设备的通用接口类? (HID,通用等)
或者是否有替代功能可以为我提供设备的路径? (SetupDiGetDeviceInterfaceDetail返回的SP_DEVICE_INTERFACE_DETAIL_DATA结构的Instread)。
来源:
HDEVINFO deviceInfoList
SP_DEVINFO_DATA deviceInfoData;
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData = NULL;
DWORD requiredLength = 0;
char *hardwareID = 0;
// Retrieve a list of all present devices
deviceInfoList = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE | DIGCF_ALLCLASSES);
if (deviceInfoList == INVALID_HANDLE_VALUE) {
SetupDiDestroyDeviceInfoList(deviceInfoList);
return false;
}
// Iterate over the list
for (DWORD i = 0; SetupDiEnumDeviceInfo(deviceInfoList, i, &deviceInfoData); i++) {
if (deviceInterfaceDetailData) LocalFree(deviceInterfaceDetailData);
requiredLength = 0;
SetupDiGetDeviceRegistryProperty(deviceInfoList, &deviceInfoData, SPDRP_HARDWAREID, &DataT, NULL, 0, &requiredLength);
if (requiredLength <= 0) {
SetupDiDestroyDeviceInfoList(deviceInfoList);
return false;
}
hardwareID = new char[requiredLength]();
SetupDiGetDeviceRegistryProperty(deviceInfoList, &deviceInfoData, SPDRP_HARDWAREID, &DataT, (PBYTE)hardwareID, requiredLength, NULL);
// Parse hardwareID for vendor ID and product ID
delete hardwareID;
hardwareID = 0;
deviceInterfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
// Requires an interface GUID, for which I have none to specify
if (!SetupDiEnumDeviceInterfaces(deviceInfoList, &deviceInfoData, /* ??? */, 0, &deviceInterfaceData)) {
SetupDiDestroyDeviceInfoList(deviceInfoList);
return false;
}
if (!SetupDiGetDeviceInterfaceDetail(deviceInfoList, &deviceInterfaceData, NULL, 0, &requiredLength, NULL)) {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER && requiredLength > 0) {
deviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR, requiredLength);
if (!deviceInterfaceDetailData) {
SetupDiDestroyDeviceInfoList(deviceInfoList);
return false;
}
} else {
SetupDiDestroyDeviceInfoList(deviceInfoList);
return false;
}
}
deviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(deviceInfoList, &deviceInterfaceData, deviceInterfaceDetailData, requiredLength, NULL, &deviceInfoData)) {
SetupDiDestroyDeviceInfoList(deviceInfoList);
return false;
}
SetupDiDestroyDeviceInfoList(deviceInfoList);
// deviceInterfaceDetailData->DevicePath yields the device path
}
答案 0 :(得分:18)
MSDN表示有一个名为GUID_DEVINTERFACE_USB_DEVICE
的通用USB设备接口类,其GUID为{A5DCBF10-6530-11D2-901F-00C04FB951ED}
:
使用DEVINTERFACE_USB_DEVICE GUID,系统提供的USB集线器驱动程序注册GUID_DEVINTERFACE_USB_DEVICE实例,以通知系统和应用程序存在连接到USB集线器的USB设备。
Here's a code example似乎可以做你想做的事。