gpt4 book ai didi

windows - 使用 Windows SetupAPI 获取 USB 设备描述符

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

我想知道是否可以使用 SetupAPI 函数(如 SetupDiGetDeviceRegistryProperty)获取 USB 设备描述符?谢谢!

编辑

到目前为止,我只能接收 windows 友好名称:

SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC,
&dwPropertyRegDataType, (BYTE*)szDesc, sizeof(szDesc), &dwSize)

最佳答案

我们需要 have/got 代表 Device Instance ID 的字符串一个设备。有了这个,我们首先通过 CM_Locate_DevNode 获得设备节点的设备实例句柄。然后调用CM_Get_DevNode_PropertyDEVPKEY_NAME :

The retrieved property value is the same as the value of the DEVPKEY_Device_FriendlyName device property, if DEVPKEY_Device_FriendlyName is set. Otherwise, the value of DEVPKEY_NAME is same as the value of the DEVPKEY_Device_DeviceDesc device property.

static volatile UCHAR guz;

CONFIGRET PrintFriendlyNameByDeviceID(PWSTR DeviceID)
{
DEVINST dnDevInst;

CONFIGRET status = CM_Locate_DevNodeW(&dnDevInst, DeviceID, CM_LOCATE_DEVNODE_NORMAL);

if (status == CR_SUCCESS)
{
ULONG cb = 0, rcb = 16;

PVOID stack = alloca(guz);

DEVPROPTYPE PropertyType;

union {
PVOID pv;
PWSTR sz;
PBYTE pb;
};

do
{
if (cb < rcb)
{
rcb = cb = RtlPointerToOffset(pv = alloca(rcb - cb), stack);
}

status = CM_Get_DevNode_PropertyW(dnDevInst, &DEVPKEY_NAME, &PropertyType, pb, &rcb, 0);

if (status == CR_SUCCESS)
{
if (PropertyType == DEVPROP_TYPE_STRING)
{
DbgPrint("NAME = %S\n", sz);
}
else
{
status = CR_WRONG_TYPE;
}
}

} while (status == CR_BUFFER_SMALL);
}

return status;
}

如果我们有标识设备接口(interface)实例的字符串 - 我们可以通过调用 CM_Get_Device_Interface_Property 从中获取设备实例标识符与 DEVPKEY_Device_InstanceId键,然后调用 PrintFriendlyNameByDeviceID

CONFIGRET PrintFriendlyNameByInterface(PCWSTR pszDeviceInterface)
{
ULONG cb = 0, rcb = 64;

PVOID stack = alloca(guz);
DEVPROPTYPE PropertyType;

CONFIGRET status;

union {
PVOID pv;
PWSTR DeviceID;
PBYTE pb;
};

do
{
if (cb < rcb)
{
rcb = cb = RtlPointerToOffset(pv = alloca(rcb - cb), stack);
}

status = CM_Get_Device_Interface_PropertyW(pszDeviceInterface, &DEVPKEY_Device_InstanceId, &PropertyType, pb, &rcb, 0);

if (status == CR_SUCCESS)
{
if (PropertyType == DEVPROP_TYPE_STRING)
{
DbgPrint("DeviceID = %S\n", DeviceID);

status = PrintFriendlyNameByDeviceID(DeviceID);
}
else
{
status = CR_WRONG_TYPE;
}

break;
}

} while (status == CR_BUFFER_SMALL);

return status;
}

一开始我们有 2 个选择:通过调用 CM_Get_Device_ID_List 检索设备实例 ID 列表。使用 CM_GETIDLIST_FILTER_CLASS|CM_GETIDLIST_FILTER_PRESENT 并使用 "{36fc9e60-c465-11cf-8056-444553540000}" 作为过滤器 - 这是众所周知的 GUID_DEVCLASS_USBdevguid.h 中定义:

void PrintFriendlyNames(PCWSTR pszFilter)
{
CONFIGRET status;
ULONG len = 0, cb = 0, rcb;
PVOID stack = alloca(guz);
PWSTR buf = 0;

do
{
if (status = CM_Get_Device_ID_List_SizeW(&len, pszFilter, CM_GETIDLIST_FILTER_CLASS|CM_GETIDLIST_FILTER_PRESENT))
{
break;
}

if (cb < (rcb = len * sizeof(WCHAR)))
{
len = (cb = RtlPointerToOffset(buf = (PWSTR)alloca(rcb - cb), stack)) / sizeof(WCHAR);
}

status = CM_Get_Device_ID_ListW(pszFilter, buf, len, CM_GETIDLIST_FILTER_CLASS|CM_GETIDLIST_FILTER_PRESENT);

if (status == CR_SUCCESS)
{
while (*buf)
{
DbgPrint("DeviceID = %S\n", buf);

PrintFriendlyNameByDeviceID(buf);

buf += 1 + wcslen(buf);
}
}

} while (status == CR_BUFFER_SMALL);
}
PrintFriendlyNames(L"{36fc9e60-c465-11cf-8056-444553540000}");

或通过CM_Get_Device_Interface_List枚举设备接口(interface)并为每个设备接口(interface)调用 PrintFriendlyNameByInterface

void PrintFriendlyNames(PGUID InterfaceClassGuid)
{
CONFIGRET status;
ULONG len = 0, cb = 0, rcb;
PVOID stack = alloca(guz);
PWSTR buf = 0;

do
{
if (status = CM_Get_Device_Interface_List_SizeW(&len, InterfaceClassGuid, 0, CM_GET_DEVICE_INTERFACE_LIST_PRESENT))
{
break;
}

if (cb < (rcb = len * sizeof(WCHAR)))
{
len = (cb = RtlPointerToOffset(buf = (PWSTR)alloca(rcb - cb), stack)) / sizeof(WCHAR);
}

status = CM_Get_Device_Interface_ListW(InterfaceClassGuid, 0, buf, len, CM_GET_DEVICE_INTERFACE_LIST_PRESENT);

if (status == CR_SUCCESS)
{
while (*buf)
{
DbgPrint("Interface = %S\n", buf);

PrintFriendlyNameByInterface(buf);

buf += 1 + wcslen(buf);
}
}

} while (status == CR_BUFFER_SMALL);
}

你可以说 GUID_DEVINTERFACE_USB_DEVICE

PrintFriendlyNames(const_cast<PGUID>(&GUID_DEVINTERFACE_USB_DEVICE));

列出的方法(哪些设备/接口(interface))的结果可能不同。在 GUID_DEVINTERFACE_USB_DEVICE 枚举时说 om my comp:

Interface = \\?\USB#VID_046D&PID_C52E#5&18d671f8&0&4#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
DeviceID = USB\VID_046D&PID_C52E\5&18d671f8&0&4
NAME = USB Composite Device
Interface = \\?\USB#VID_051D&PID_0002#5B1120T12418__#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
DeviceID = USB\VID_051D&PID_0002\5B1120T12418__
NAME = American Power Conversion USB UPS
Interface = \\?\USB#VID_045E&PID_077B#5&18d671f8&0&3#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
DeviceID = USB\VID_045E&PID_077B\5&18d671f8&0&3
NAME = USB Input Device

当通过 GUID_DEVCLASS_USB 字符串过滤器枚举时:

DeviceID = USB\VID_1F75&PID_0916\120709860570000024
NAME = USB Mass Storage Device
DeviceID = USB\ROOT_HUB30\4&33ed72c&0&0
NAME = USB Root Hub (xHCI)
DeviceID = USB\VID_0951&PID_168F\001A92053B6A0CA101340008
NAME = USB Mass Storage Device
DeviceID = PCI\VEN_8086&DEV_A2AF&SUBSYS_7A741462&REV_00\3&11583659&0&A0
NAME = Intel(R) USB 3.0 eXtensible Host Controller - 1.0 (Microsoft)
DeviceID = USB\VID_046D&PID_C52E\5&18d671f8&0&4
NAME = USB Composite Device

关于windows - 使用 Windows SetupAPI 获取 USB 设备描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46114110/

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