VC 识别USB HID设备

遇到问题:和http://bbs.csdn.net/topics/190065205论坛中一样,不过前辈是2007年遇到的。。。

有一个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);//进行注册。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值