我们需要有/得到表示设备的设备实例 ID的字符串。有了这个,我们首先通过以下方式获取设备节点的设备实例句柄,CM_Locate_DevNode
然后调用:CM_Get_DevNode_Property
DEVPKEY_NAME
如果设置了DEVPKEY_Device_FriendlyName ,则检索到的属性值与
DEVPKEY_Device_FriendlyName设备属性
的值相同。否则,
DEVPKEY_NAME的值与DEVPKEY_Device_DeviceDesc
设备属性的值相同。
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;
}
如果我们有标识设备接口实例的字符串 - 我们可以通过CM_Get_Device_Interface_Property
使用DEVPKEY_Device_InstanceId
key 调用从中获取设备实例标识符,然后调用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 个选择:仅通过调用检索设备实例 ID 列表CM_Get_Device_ID_List
并CM_GETIDLIST_FILTER_CLASS|CM_GETIDLIST_FILTER_PRESENT
用作"{36fc9e60-c465-11cf-8056-444553540000}"
过滤器 - 这是众所周知的字符串表示形式,GUID_DEVCLASS_USB
定义在devguid.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枚举设备接口并调用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));
列出的方法(哪些设备/接口)的结果可能不同。枚举时说 om my comp GUID_DEVINTERFACE_USB_DEVICE
:
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