I'm working on an MFC tool that check some driver information's of connected USB device such as location info, parent, hardware id, etc. I have got all other information i want but i'm stuck in getting device parent information.
I have used Setupapi.dll to get device information in my code. Got USB device information handle using SetupDiGetClassDevs function. Got specific device info data using SetupDiEnumDeviceInfo function. Got device description and hardware id using SetupDiGetDeviceRegistryProperty function.
// 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);
}
It is very useful for me to get the parent information of USB device. Please help with some clues.