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

相关推荐
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页