gpt4 book ai didi

windows - 如何枚举所有连接的 USB 设备的设备路径?

转载 作者:可可西里 更新时间:2023-11-01 12:25:21 25 4
gpt4 key购买 nike

我正在尝试使用 SetupDi 函数枚举所有连接的 USB 设备的设备路径。设备路径是 CreateFile() 中使用的路径,因此我可以与设备通信。

然而,SetupDiGetDeviceInterface需要接口(interface) GUID,但我并不是专门寻找特定接口(interface)(除了所有连接的 USB)。这部分被注释为/* ??? */在下面的源代码中。

尝试的解决方案:

我尝试提供 GUID_DEVCLASS_UNKNOWN = {0x4d36e97e, 0xe325, 0x11ce, {0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}};但这引发了“没有更多接口(interface)”错误。

我也曾尝试将 deviceInfoData.ClassGuid 提供给 SetupDiGetDeviceInterface,但我遇到了与上述相同的错误,“没有更多接口(interface)”。

问题:

是否有一个涵盖所有 USB 设备的通用接口(interface)类? (HID、通用等)

或者是否有替代函数可以为我提供设备路径? (Instread 由 SetupDiGetDeviceInterfaceDetail 返回的 SP_DEVICE_INTERFACE_DETAIL_DATA 结构)。

来源:

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
}

最佳答案

MSDN表示有一个名为 GUID_DEVINTERFACE_USB_DEVICE 的通用 USB 设备接口(interface)类,GUID {A5DCBF10-6530-11D2-901F-00C04FB951ED}:

The system-supplied USB hub driver registers instances of GUID_DEVINTERFACE_USB_DEVICE to notify the system and applications of the presence of USB devices that are attached to a USB hub.

Here's a code example使用 DEVINTERFACE_USB_DEVICE GUID,这似乎可以做您想做的事。

关于windows - 如何枚举所有连接的 USB 设备的设备路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13927475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com