windows通过GUID枚举系统中所有USB设备并且显示该设备的所有属性信息
2021-11-18
2292
1
微信群里有同学问怎么获取USB设备的位置信息,这个学名叫做SPDRP_LOCATION_PATHS,刚好自己以前写过这个代码,就分享了一下。
代码如下:
#include<windows.h>
#include <winioctl.h>
#include <setupapi.h>
#include <initguid.h>
#include <stdio.h>
#include<hidsdi.h>
#include<tchar.h>
#pragma comment(lib,"Setupapi.lib ")
#pragma comment(lib,"hid.lib ")
BOOL USBEnumDevice(OUT PTCHAR pDeviceName, IN LPGUID pGuid, IN int instance)
{
HDEVINFO info = SetupDiGetClassDevs((GUID*)pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
if (info == INVALID_HANDLE_VALUE)
{
printf("No HDEVINFO available for this GUID\n");
return FALSE;
}
// Get interface data for the requested instance
SP_INTERFACE_DEVICE_DATA ifdata;
ifdata.cbSize = sizeof(ifdata);
if (!SetupDiEnumDeviceInterfaces(info, NULL, (GUID*)pGuid, instance, &ifdata))
{
_tprintf(TEXT("No SP_INTERFACE_DEVICE_DATA available for this GUID instance\n"));
SetupDiDestroyDeviceInfoList(info);
return FALSE;
}
// Get size of symbolic link name
DWORD ReqLen;
SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL);
PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new TCHAR[ReqLen]);
if (ifDetail == NULL)
{
SetupDiDestroyDeviceInfoList(info);
return FALSE;
}
// Get symbolic link name
ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL))
{
SetupDiDestroyDeviceInfoList(info);
delete[] ifDetail;
return FALSE;
}
//printf("Symbolic link is %s\n", ifDetail->DevicePath);
memcpy(pDeviceName, ifDetail->DevicePath, _tcslen(ifDetail->DevicePath) * sizeof(TCHAR));
pDeviceName[_tcslen(ifDetail->DevicePath)] = TEXT('\0');
delete[] ifDetail;
SetupDiDestroyDeviceInfoList(info);
return TRUE;
}
#define TO_SEARCH_DEVICE_UUID L"{A5DCBF10-6530-11D2-901F-00C04FB951ED}"
int main()
{
//GUID hidGuid;
//::HidD_GetHidGuid((LPGUID)&hidGuid);
GUID AGuid;
CLSIDFromString(TO_SEARCH_DEVICE_UUID, &AGuid);
HDEVINFO hDevInfo = SetupDiGetClassDevs(&AGuid, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
return 0;
}
SP_DEVINFO_DATA DeviceInfoData;
for (int iIndex = 0;; iIndex++)
{
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
if (!SetupDiEnumDeviceInfo(hDevInfo, iIndex, &DeviceInfoData))
{
break;
}
char szHardWareID[512] = { 0 };
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID,
NULL, (PBYTE)szHardWareID, sizeof(szHardWareID), NULL))
{
continue;
}
{
printf("%s\r\n", szHardWareID);
}
#define SHOW_PRO(X) \
{ \
if (SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, X,NULL, (PBYTE)szHardWareID, sizeof(szHardWareID), NULL))\
{\
printf(#X ":%s\r\n", szHardWareID);\
}\
}
SHOW_PRO(SPDRP_DEVICEDESC);
SHOW_PRO(SPDRP_HARDWAREID);
SHOW_PRO(SPDRP_COMPATIBLEIDS);
SHOW_PRO(SPDRP_UNUSED0);
SHOW_PRO(SPDRP_SERVICE);
SHOW_PRO(SPDRP_UNUSED1);
SHOW_PRO(SPDRP_UNUSED2);
SHOW_PRO(SPDRP_CLASS);
SHOW_PRO(SPDRP_CLASSGUID);
SHOW_PRO(SPDRP_DRIVER);
SHOW_PRO(SPDRP_CONFIGFLAGS);
SHOW_PRO(SPDRP_MFG);
SHOW_PRO(SPDRP_FRIENDLYNAME);
SHOW_PRO(SPDRP_LOCATION_INFORMATION);
SHOW_PRO(SPDRP_PHYSICAL_DEVICE_OBJECT_NAME);
SHOW_PRO(SPDRP_CAPABILITIES);
SHOW_PRO(SPDRP_UI_NUMBER);
SHOW_PRO(SPDRP_UPPERFILTERS);
SHOW_PRO(SPDRP_LOWERFILTERS);
SHOW_PRO(SPDRP_BUSTYPEGUID);
SHOW_PRO(SPDRP_LEGACYBUSTYPE);
SHOW_PRO(SPDRP_BUSNUMBER);
SHOW_PRO(SPDRP_ENUMERATOR_NAME);
SHOW_PRO(SPDRP_SECURITY);
SHOW_PRO(SPDRP_SECURITY_SDS);
SHOW_PRO(SPDRP_DEVTYPE);
SHOW_PRO(SPDRP_EXCLUSIVE);
SHOW_PRO(SPDRP_CHARACTERISTICS);
SHOW_PRO(SPDRP_ADDRESS);
SHOW_PRO(SPDRP_UI_NUMBER_DESC_FORMAT);
SHOW_PRO(SPDRP_DEVICE_POWER_DATA);
SHOW_PRO(SPDRP_REMOVAL_POLICY);
SHOW_PRO(SPDRP_REMOVAL_POLICY_HW_DEFAULT);
SHOW_PRO(SPDRP_REMOVAL_POLICY_OVERRIDE);
SHOW_PRO(SPDRP_INSTALL_STATE);
SHOW_PRO(SPDRP_LOCATION_PATHS);
SHOW_PRO(SPDRP_BASE_CONTAINERID);
}
getchar();
printf("pk");
return 0;
}
这样系统中就打印出了所有关于USB设备的信息。关于GUID的信息可见:http://www.pnpon.com/article/detail-146.html
如这里我显示一个UVC摄像头的信息:
USB\VID_0C45&PID_636D&REV_0100&MI_00
SPDRP_DEVICEDESC:USB 视频设备
SPDRP_HARDWAREID:USB\VID_0C45&PID_636D&REV_0100&MI_00
SPDRP_COMPATIBLEIDS:USB\Class_0e&SubClass_03&Prot_00
SPDRP_SERVICE:usbvideo
SPDRP_CLASS:Camera
SPDRP_CLASSGUID:{ca3e7ab9-b4c3-4ae6-8251-579ef933890f}
SPDRP_DRIVER:{ca3e7ab9-b4c3-4ae6-8251-579ef933890f}\0005
SPDRP_CONFIGFLAGS:
SPDRP_MFG:Microsoft
SPDRP_FRIENDLYNAME:USB Camera
SPDRP_LOCATION_INFORMATION:0000.0014.0000.002.000.000.000.000.000
SPDRP_PHYSICAL_DEVICE_OBJECT_NAME:\Device\000002bf
SPDRP_CAPABILITIES:?
SPDRP_LOWERFILTERS:WdmCompanionFilter
SPDRP_BUSTYPEGUID:茧}漖妊灤
SPDRP_LEGACYBUSTYPE:
SPDRP_BUSNUMBER:
SPDRP_ENUMERATOR_NAME:USB
SPDRP_ADDRESS:
SPDRP_DEVICE_POWER_DATA:8
SPDRP_REMOVAL_POLICY:
SPDRP_REMOVAL_POLICY_HW_DEFAULT:
SPDRP_INSTALL_STATE:
SPDRP_LOCATION_PATHS:PCIROOT(0)#PCI(1400)#USBROOT(0)#USB(2)#USBMI(0)
SPDRP_BASE_CONTAINERID:{
当然,这里会出现一些乱码,是因为我把所有信息都当字符串输出了,对某些项有兴趣的同学可以自行根据类型来进行格式化输出。
HID人机交互QQ群:564808376
UAC音频QQ群:218581009
UVC相机QQ群:331552032
BOT&UASP大容量存储QQ群:258159197
STC-USB单片机QQ群:315457461
USB技术交流QQ群2:580684376
USB技术交流QQ群:952873936