MFC USB通信枚举HID设备出现一大堆VID和PID一样的设备

为天下苍生拔剑 2018-08-01 03:09:37
最近在做一个usb通信的工程,搜了一下资料,大概是HID类设备的写法。

枚举USB设备的函数如下

m_combobox.ResetContent();
SP_DEVINFO_DATA DeviceInfoData;
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData;
//get device id
GUID deviceId;

HidD_GetHidGuid(&deviceId);
//get device info
HDEVINFO handle;
handle = SetupDiGetClassDevs(&deviceId, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

bool result1 = false;
bool result2 = false;
DWORD strSize = 0;
DWORD requiredSize = 0;
CString temp;
int countNum = 0;
DWORD dwError;
do
{
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
result1 = SetupDiEnumDeviceInterfaces(handle, NULL, &deviceId, countNum, &DeviceInterfaceData);
if (result1 == FALSE)
{
dwError = GetLastError();
break;

}
SetupDiGetDeviceInterfaceDetail(handle, &DeviceInterfaceData, NULL, 0, &strSize, NULL);
requiredSize = strSize;
DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiredSize);
DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
result2 = SetupDiGetDeviceInterfaceDetail(handle, &DeviceInterfaceData, DeviceInterfaceDetailData,
strSize, &requiredSize, &DeviceInfoData);
temp = DeviceInterfaceDetailData->DevicePath;
UpdateData(FALSE);
m_combobox.AddString(temp);
countNum++;
free(DeviceInterfaceDetailData);
} while (result1);


m_combobox是一个combobox按钮绑定的变量。

这段代码能枚举出来设备,只不过结果我有点不太理解。


里面的vid和pid全都一样,第一个是我的键盘,下面那些都是设备管理器里的人体输入学设备和通用串行总线控制器里的东西。
我想要的就是那个键盘,代码哪里有问题?
...全文
1060 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
songyachao 2018-10-15
  • 打赏
  • 举报
回复
引用 11 楼 wangyiyu3 的回复:
[quote=引用 10 楼 songyachao 的回复:]

int CTestapiDlg::JudgeHid(void)
{
BOOL res;
int finddevice=0;
// Windows objects for interacting with the driver.
GUID InterfaceClassGuid = {0x4d1e55b2, 0xf16f, 0x11cf, {0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30} };
SP_DEVINFO_DATA devinfo_data;
SP_DEVICE_INTERFACE_DATA device_interface_data;
SP_DEVICE_INTERFACE_DETAIL_DATA_A *device_interface_detail_data = NULL;
HDEVINFO device_info_set = INVALID_HANDLE_VALUE;
int device_index = 0;

// Initialize the Windows objects.
devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

// Get information for all the devices belonging to the HID class.
device_info_set = SetupDiGetClassDevs(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

// Iterate over each device in the HID class, looking for the right one.

for (;;) {
HANDLE write_handle = INVALID_HANDLE_VALUE;
DWORD required_size = 0;
//HIDD_ATTRIBUTES attrib;

res = SetupDiEnumDeviceInterfaces(device_info_set,
NULL,
&InterfaceClassGuid,
device_index,
&device_interface_data);

if (!res) {
// A return of FALSE from this function means that
// there are no more devices.
break;
}

// Call with 0-sized detail size, and let the function
// tell us how long the detail struct needs to be. The
// size is put in &required_size.
res = SetupDiGetDeviceInterfaceDetail(device_info_set,
&device_interface_data,
NULL,
0,
&required_size,
NULL);

// Allocate a long enough structure for device_interface_detail_data.
device_interface_detail_data = (SP_DEVICE_INTERFACE_DETAIL_DATA_A*) malloc(required_size);
device_interface_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);

// Get the detailed data for this device. The detail data gives us
// the device path for this device, which is then passed into
// CreateFile() to get a handle to the device.
res = SetupDiGetDeviceInterfaceDetail(device_info_set,
&device_interface_data,
device_interface_detail_data,
required_size,
NULL,
NULL);

if (!res) {
//register_error(dev, "Unable to call SetupDiGetDeviceInterfaceDetail");
// Continue to the next device.
goto cont;
}

if (strstr(device_interface_detail_data->DevicePath,"vid_b111&pid_a111")!=NULL)
{
finddevice = 1;
//PID,VID添加到标签控件
SetDlgItemText(IDC_STATIC_PID, "a111");
SetDlgItemText(IDC_STATIC_VID, "b111");
free(device_interface_detail_data);
hidpos_connect_num ++;
break;
}
else if(strstr(device_interface_detail_data->DevicePath,"vid_c111&pid_d111")!=NULL)
{
finddevice = 1;
//PID,VID添加到标签控件
SetDlgItemText(IDC_STATIC_PID, "d111");
SetDlgItemText(IDC_STATIC_VID, "c111");
free(device_interface_detail_data);
hidpos_connect_num ++;
break;
}
else
{
SetDlgItemText(IDC_STATIC_PID, "");
SetDlgItemText(IDC_STATIC_VID, "");
hidpos_connect_num = 0;
}
//wprintf(L"HandleName: %s\n", device_interface_detail_data->DevicePath);

// Open a handle to the device

cont:
// We no longer need the detail data. It can be freed
free(device_interface_detail_data);

device_index++;

}

// Close the device information handle.
SetupDiDestroyDeviceInfoList(device_info_set);

return finddevice;

}


GUID InterfaceClassGuid这个值是怎么来的?代表什么?[/quote]
关于GUID 了解一下 https://blog.csdn.net/eric422113/article/details/6607052
songyachao 2018-09-28
  • 打赏
  • 举报
回复

int CTestapiDlg::JudgeHid(void)
{
BOOL res;
int finddevice=0;
// Windows objects for interacting with the driver.
GUID InterfaceClassGuid = {0x4d1e55b2, 0xf16f, 0x11cf, {0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30} };
SP_DEVINFO_DATA devinfo_data;
SP_DEVICE_INTERFACE_DATA device_interface_data;
SP_DEVICE_INTERFACE_DETAIL_DATA_A *device_interface_detail_data = NULL;
HDEVINFO device_info_set = INVALID_HANDLE_VALUE;
int device_index = 0;

// Initialize the Windows objects.
devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

// Get information for all the devices belonging to the HID class.
device_info_set = SetupDiGetClassDevs(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

// Iterate over each device in the HID class, looking for the right one.

for (;;) {
HANDLE write_handle = INVALID_HANDLE_VALUE;
DWORD required_size = 0;
//HIDD_ATTRIBUTES attrib;

res = SetupDiEnumDeviceInterfaces(device_info_set,
NULL,
&InterfaceClassGuid,
device_index,
&device_interface_data);

if (!res) {
// A return of FALSE from this function means that
// there are no more devices.
break;
}

// Call with 0-sized detail size, and let the function
// tell us how long the detail struct needs to be. The
// size is put in &required_size.
res = SetupDiGetDeviceInterfaceDetail(device_info_set,
&device_interface_data,
NULL,
0,
&required_size,
NULL);

// Allocate a long enough structure for device_interface_detail_data.
device_interface_detail_data = (SP_DEVICE_INTERFACE_DETAIL_DATA_A*) malloc(required_size);
device_interface_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);

// Get the detailed data for this device. The detail data gives us
// the device path for this device, which is then passed into
// CreateFile() to get a handle to the device.
res = SetupDiGetDeviceInterfaceDetail(device_info_set,
&device_interface_data,
device_interface_detail_data,
required_size,
NULL,
NULL);

if (!res) {
//register_error(dev, "Unable to call SetupDiGetDeviceInterfaceDetail");
// Continue to the next device.
goto cont;
}

if (strstr(device_interface_detail_data->DevicePath,"vid_b111&pid_a111")!=NULL)
{
finddevice = 1;
//PID,VID添加到标签控件
SetDlgItemText(IDC_STATIC_PID, "a111");
SetDlgItemText(IDC_STATIC_VID, "b111");
free(device_interface_detail_data);
hidpos_connect_num ++;
break;
}
else if(strstr(device_interface_detail_data->DevicePath,"vid_c111&pid_d111")!=NULL)
{
finddevice = 1;
//PID,VID添加到标签控件
SetDlgItemText(IDC_STATIC_PID, "d111");
SetDlgItemText(IDC_STATIC_VID, "c111");
free(device_interface_detail_data);
hidpos_connect_num ++;
break;
}
else
{
SetDlgItemText(IDC_STATIC_PID, "");
SetDlgItemText(IDC_STATIC_VID, "");
hidpos_connect_num = 0;
}
//wprintf(L"HandleName: %s\n", device_interface_detail_data->DevicePath);

// Open a handle to the device

cont:
// We no longer need the detail data. It can be freed
free(device_interface_detail_data);

device_index++;

}

// Close the device information handle.
SetupDiDestroyDeviceInfoList(device_info_set);

return finddevice;

}
  • 打赏
  • 举报
回复
引用 10 楼 songyachao 的回复:
int CTestapiDlg::JudgeHid(void)
{
BOOL res;
int finddevice=0;
// Windows objects for interacting with the driver.
GUID InterfaceClassGuid = {0x4d1e55b2, 0xf16f, 0x11cf, {0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30} };
SP_DEVINFO_DATA devinfo_data;
SP_DEVICE_INTERFACE_DATA device_interface_data;
SP_DEVICE_INTERFACE_DETAIL_DATA_A *device_interface_detail_data = NULL;
HDEVINFO device_info_set = INVALID_HANDLE_VALUE;
int device_index = 0;

// Initialize the Windows objects.
devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

// Get information for all the devices belonging to the HID class.
device_info_set = SetupDiGetClassDevs(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

// Iterate over each device in the HID class, looking for the right one.

for (;;) {
HANDLE write_handle = INVALID_HANDLE_VALUE;
DWORD required_size = 0;
//HIDD_ATTRIBUTES attrib;

res = SetupDiEnumDeviceInterfaces(device_info_set,
NULL,
&InterfaceClassGuid,
device_index,
&device_interface_data);

if (!res) {
// A return of FALSE from this function means that
// there are no more devices.
break;
}

// Call with 0-sized detail size, and let the function
// tell us how long the detail struct needs to be. The
// size is put in &required_size.
res = SetupDiGetDeviceInterfaceDetail(device_info_set,
&device_interface_data,
NULL,
0,
&required_size,
NULL);

// Allocate a long enough structure for device_interface_detail_data.
device_interface_detail_data = (SP_DEVICE_INTERFACE_DETAIL_DATA_A*) malloc(required_size);
device_interface_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);

// Get the detailed data for this device. The detail data gives us
// the device path for this device, which is then passed into
// CreateFile() to get a handle to the device.
res = SetupDiGetDeviceInterfaceDetail(device_info_set,
&device_interface_data,
device_interface_detail_data,
required_size,
NULL,
NULL);

if (!res) {
//register_error(dev, "Unable to call SetupDiGetDeviceInterfaceDetail");
// Continue to the next device.
goto cont;
}

if (strstr(device_interface_detail_data->DevicePath,"vid_b111&pid_a111")!=NULL)
{
finddevice = 1;
//PID,VID添加到标签控件
SetDlgItemText(IDC_STATIC_PID, "a111");
SetDlgItemText(IDC_STATIC_VID, "b111");
free(device_interface_detail_data);
hidpos_connect_num ++;
break;
}
else if(strstr(device_interface_detail_data->DevicePath,"vid_c111&pid_d111")!=NULL)
{
finddevice = 1;
//PID,VID添加到标签控件
SetDlgItemText(IDC_STATIC_PID, "d111");
SetDlgItemText(IDC_STATIC_VID, "c111");
free(device_interface_detail_data);
hidpos_connect_num ++;
break;
}
else
{
SetDlgItemText(IDC_STATIC_PID, "");
SetDlgItemText(IDC_STATIC_VID, "");
hidpos_connect_num = 0;
}
//wprintf(L"HandleName: %s\n", device_interface_detail_data->DevicePath);

// Open a handle to the device

cont:
// We no longer need the detail data. It can be freed
free(device_interface_detail_data);

device_index++;

}

// Close the device information handle.
SetupDiDestroyDeviceInfoList(device_info_set);

return finddevice;

}


GUID InterfaceClassGuid这个值是怎么来的?代表什么?
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
学会使用devcon ?


我要使用MFC,要给用户提供界面。
  • 打赏
  • 举报
回复
引用 3 楼 kangji 的回复:
是笔记本电脑吧,鼠标键盘触摸板等等,有这么多HID设备很正常呀

是台式电脑,插的usb无线键鼠,如果我想跟非HID类的usb设备通讯我该怎么做?
赵4老师 2018-08-13
  • 打赏
  • 举报
回复
引用 6 楼 wangyiyu3 的回复:
[quote=引用 4 楼 zhao4zhong1 的回复:]
学会使用devcon ?


我要使用MFC,要给用户提供界面。[/quote]
看来你不会在MFC环境下使用
WinExec("cmd /c devcon.exe ...>devcon.txt",SW_HIDE);
//然后读文件devcon.txt的内容
这种方法调用devcon.exe的功能。
赵4老师 2018-08-07
  • 打赏
  • 举报
回复
学会使用devcon ?
kangji 2018-08-07
  • 打赏
  • 举报
回复
是笔记本电脑吧,鼠标键盘触摸板等等,有这么多HID设备很正常呀
  • 打赏
  • 举报
回复
我看了下这个枚举,跟我的并没有区别,我想知道为什么会出现一堆vid和pid完全一样的设备?这些设备在设备管理器中还都能找到?这是什么原理?

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧