VC 识别USB HID设备
2017-03-23 13:27
405 查看
遇到问题:和http://bbs.csdn.net/topics/190065205论坛中一样,不过前辈是2007年遇到的。。。
有一个USB设备,在设备管理器中显示为一个磁盘驱动器,但是它是没有盘符的, 在OnDeviceChange中,当这个设备插入或者拔插的时候产生多个DBT_DEVNODES_CHANGED, 而DBT_DEVICEARRIVAL, DBT_DEVICEREMOVECOMPLETE之类的却截获不到,如何才能截获到DBT_DEVICEARRIVAL, DBT_DEVICEREMOVECOMPLETE?(U盘之类的USB设备可以截获的到)
解决代码如下:
如题主要是添加了:将USB设备用
有一个USB设备,在设备管理器中显示为一个磁盘驱动器,但是它是没有盘符的, 在OnDeviceChange中,当这个设备插入或者拔插的时候产生多个DBT_DEVNODES_CHANGED, 而DBT_DEVICEARRIVAL, DBT_DEVICEREMOVECOMPLETE之类的却截获不到,如何才能截获到DBT_DEVICEARRIVAL, DBT_DEVICEREMOVECOMPLETE?(U盘之类的USB设备可以截获的到)
解决代码如下:
#include <tchar.h> #include <string> #include <iostream> #include <Windows.h> #include <dbt.h> GUID HIDClassGuid = {0x4D1E55B2,0xF16F,0x11CF,{0x88,0xCB,0x00,0x11,0x11,0x00,0x00,0X30}}; void RegisterForDeviceNotifications(HWND m_hWnd) { // Request to receive messages when a device is attached or removed. // Also see WM_DEVICECHANGE in BEGIN_MESSAGE_MAP(CUsbhidiocDlg, CDialog). DEV_BROADCAST_DEVICEINTERFACE DevBroadcastDeviceInterface; HDEVNOTIFY DeviceNotificationHandle; DevBroadcastDeviceInterface.dbcc_size = sizeof(DevBroadcastDeviceInterface); DevBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; DevBroadcastDeviceInterface.dbcc_classguid = HIDClassGuid; DeviceNotificationHandle = RegisterDeviceNotification(m_hWnd, &DevBroadcastDeviceInterface, DEVICE_NOTIFY_WINDOW_HANDLE); } LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam; PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb; TCHAR szMsg[80]; char driveName; switch(uMsg) { case WM_DEVICECHANGE: switch(wParam ) { case DBT_DEVNODES_CHANGED: printf("USB Drive: has inserted.\r\n"); MessageBox(hWnd, "USB Drive: has inserted 0x0007.", TEXT("WM_DEVICECHANGE"), MB_OK); //ShellExecute(0, "open", "C:\\Program Files\\Internet Explorer\\iexplore.exe", "http://www.baidu.com", NULL, SW_SHOW); break; case DBT_DEVICEARRIVAL: printf("USB Drive: has inserted.\r\n"); MessageBox(hWnd, "USB Drive: has inserted 0x8000.", TEXT("DBT_DEVICEARRIVAL"), MB_OK); break; case DBT_DEVICEREMOVECOMPLETE: printf("USB Drive: has gone.\r\n"); MessageBox(hWnd, "USB Drive: has gone 0x8004.", TEXT("DBT_DEVICEREMOVECOMPLETE"), MB_OK); break; default: ; } break; default: ; } return DefWindowProc(hWnd, uMsg, wParam, lParam); } int main(int argc, char *argv[]) { TCHAR szClassName[] = _T("MyUsbCheck"); WNDCLASS wndcls = {0}; wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndcls.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW); wndcls.hIcon = (HICON)LoadIcon(NULL, IDI_APPLICATION); wndcls.lpfnWndProc = WndProc; wndcls.lpszClassName = szClassName; if(!RegisterClass(&wndcls)) { printf("RegisterClass Failed!\r\n"); return 0; } HWND hWnd = CreateWindow(szClassName, szClassName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL); if(NULL == hWnd) { printf("CreateWindow Failed!\r\n"); return 0; } ShowWindow(hWnd, SW_HIDE); UpdateWindow(hWnd); RegisterForDeviceNotifications(hWnd); MSG msg; while(GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; }
如题主要是添加了:将USB设备用
RegisterForDeviceNotifications(hWnd);//进行注册。
相关文章推荐
- VC识别有USB存储设备插入
- vc中如何可以识别到插入的设备在哪个USB端口
- stm32 USB hid设备与PC进行双向数据传输时PC不识别USB设备
- Windows主机端与自定义USB HID设备通信详解
- 《Linux总线、设备与驱动》USBHID设备驱动
- vc检测usb设备的拔插状态
- USB设备无法识别
- USB设备驱动6:usb HID 报告描述符
- VMware识别不到Usb设备
- 4 种方法识别Linux系统 USB 设备
- 破解USB HID 设备接口协议
- STM32 USB HID 自定义设备 bulk 传输
- 移动设备无法识别的USB设备处理方法
- 驱动中识别Usb设备插入拔出消息
- C# 访问USB(HID)设备
- Android 设备USB识别
- vitualbox中redhat识别usb设备
- USB 设备无法识别故障的排除
- CentOS6.5菜鸟之旅:VirtualBox4.3识别USB设备
- 使用pdiusbd12制作包含HID接口的多接口USB设备