根据MSDN文档,SetupDiGetClassDevs可以通过
device instance ID获取特定设备的
device information set:
To return only a specific device,set
the DIFCF_DEVICEINTERFACE flag and use
the Enumerator parameter to supply the
device instance ID of the device.
我通过解析WM_DEVICECHANGE消息DBT_DEVICEARRIVAL事件中的符号名称来获取设备实例ID,并通过将其与从SetupDiGetDeviceInstanceId返回的ID进行比较来验证结果ID.即使传递操作系统提供的设备实例ID也不起作用(即SetupDiGetClassDevs调用与ERROR_INVALID_ParaMETER失败).
我当前为新到设备获取SP_DEVINFO_DATA结构的解决方法是枚举同一类中的所有设备,并将SetupDiGetDeviceInstanceId的结果与符号名称进行比较.但是,根据文件,我不明白为什么这是必要的……
有没有人让SetupDiGetClassDevs以这种方式工作?有没有更好的方法可以使用DBT_DEVICEARRIVAL事件中的数据获取设备的更多信息?
您似乎必须指定DIGCF_ALLCLASSES标志以查找与给定设备实例ID匹配的所有类,或者指定ClassGuid并使用DIGCF_DEFAULT标志.
这对我有用:
void error(DWORD err)
{
WCHAR buf[0x200];
FormatMessage(FORMAT_MESSAGE_FROM_SYstem,NULL,err,buf,0x200,NULL);
wprintf(L"%x: %s\n",buf);
}
int _tmain(int argc,_TCHAR* argv[])
{
PCWSTR devinst = L"HID\\VID_413C&PID_2105\\6&22CE0F66&0&0000";
HDEVINFO hinfo = SetupDiGetClassDevs(NULL,devinst,DIGCF_DEVICEINTERFACE | DIGCF_ALLCLASSES);
if (hinfo == INVALID_HANDLE_VALUE)
{
error(GetLastError());
return 1;
}
SP_DEVINFO_DATA dinfo;
dinfo.cbSize = sizeof(dinfo);
int ix = 0;
while (SetupDiEnumDeviceInfo(hinfo,ix++,&dinfo))
{
wprintf(L"Match\n");
}
error(GetLastError());
SetupDiDestroyDeviceInfoList(hinfo);
return 0;
}
带输出:
Match 103: No more data is available.