我正在开发一个 MFC 工具来检查连接的 USB 设备的一些驱动程序信息,例如位置信息、父级、硬件 ID 等。我已经获得了我想要的所有其他信息,但我一直在获取设备父级信息。
我使用Setupapi.dll在我的代码中获取设备信息。使用SetupDiGetClassDevs函数获取 USB 设备信息句柄。使用SetupDiEnumDeviceInfo函数获取特定设备信息数据。使用SetupDiGetDeviceRegistryProperty函数 获取设备描述和硬件 ID 。
// List all connected USB devices
hDevInfo = SetupDiGetClassDevs(pClassGuid, pszEnumerator,
NULL, pClassGuid != NULL ? DIGCF_PRESENT : DIGCF_ALLCLASSES |
DIGCF_PRESENT);
// Get device info data
SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData);
// Get device instance id
CM_Get_Device_ID(DeviceInfoData.DevInst, szDeviceInstanceID, MAX_PATH,
0);
// Get device description
SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData,
SPDRP_DEVICEDESC,
&dwPropertyRegDataType, (BYTE*)szDesc, sizeof(szDesc), &dwSize);
#define DEFINE_DEVPROPKEY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7,
b8, pid) EXTERN_C const DEVPROPKEY DECLSPEC_SELECTANY
name = { { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }, pid
}
// DEVPROP_TYPE_STRING
DEFINE_DEVPROPKEY(DEVPKEY_Device_Manufacturer, 0xa45c254e, 0xdf1c,
0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 13);
// DEVPROP_TYPE_GUID
DEFINE_DEVPROPKEY(DEVPKEY_Device_ContainerId, 0x8c7ed206, 0x3f8a, 0x4827,
0xb3, 0xab, 0xae, 0x9e, 0x1f, 0xae, 0xfc, 0x6c, 2);
typedef BOOL(WINAPI *FN_SetupDiGetDevicePropertyW)(
__in HDEVINFO DeviceInfoSet,
__in PSP_DEVINFO_DATA DeviceInfoData,
__in const DEVPROPKEY *PropertyKey,
__out DEVPROPTYPE *PropertyType,
__out_opt PBYTE PropertyBuffer,
__in DWORD PropertyBufferSize,
__out_opt PDWORD RequiredSize,
__in DWORD Flags
);
FN_SetupDiGetDevicePropertyW fn_SetupDiGetDevicePropertyW =
(FN_SetupDiGetDevicePropertyW)
GetProcAddress(GetModuleHandle(TEXT("Setupapi.dll")),
"SetupDiGetDevicePropertyW");
if (fn_SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData,
&DEVPKEY_Device_Manufacturer, &ulPropertyType, (BYTE*)szBuffer,
sizeof(szBuffer), &dwSize, 0))
{
_tprintf(TEXT(" Device Manufacturer: \"%ls\"\n"), szBuffer);
}
if (fn_SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData,
&DEVPKEY_Device_ContainerId, &ulPropertyType, (BYTE*)szDesc,
sizeof(szDesc), &dwSize, 0))
{
StringFromGUID2((REFGUID)szDesc, szBuffer, ARRAY_SIZE(szBuffer));
_tprintf(TEXT(" ContainerId: \"%ls\"\n"), szBuffer);
}
获取 USB 设备的父级信息对我来说非常有用。请帮忙提供一些线索。