在我的Qt应用程序中收到了WM_DEVICECHANGE但没有收到DBT_DEVICEARRIVAL

时间:2015-05-22 14:58:09

标签: c++ qt

我正在按照一个示例来检测Windows 7中的USB闪存驱动器插件和插件检测。我确实收到了通知WM_DEVICECHANGE但未收到DBT_DEVICEARRIVAL这是插入USB设备的时间。我的代码是下面:

/*******************************************
*             WINDOWS EVENTS
********************************************/
/*We use the first WM_PAINT event to get the handle of main window
  and pass it to RegisterDeviceNotification function.
  It not possible to do this in the contructor because the
  main window does not exist yet.
  WM_DEVICECHANGE event notify us that a device is attached or detached */
bool USBexample::nativeEvent(const QByteArray & eventType, void * message, long * result)
{
    MSG * msg = static_cast< MSG * > (message);
    int msgType = msg->message;
    if(msgType == WM_PAINT)
    {
        if(!msgp)   //Only the first WM_PAINT
        {
            GUID InterfaceClassGuid = HID_CLASSGUID;
            DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
            ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
            NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
            NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
            NotificationFilter.dbcc_classguid = InterfaceClassGuid;
            HWND hw = (HWND) this->effectiveWinId();   //Main window handle
            hDevNotify = RegisterDeviceNotification(hw,&NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
            msgp = true;
        }
    }
    if(msgType == WM_DEVICECHANGE)
    {
       qDebug() << "WM_DEVICECHANGE recieved";
       PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
       switch(msg->wParam)
       {
             case DBT_DEVICEARRIVAL: // never comes here!
                if (lpdb -> dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
                {

                    qDebug() << "DBT_DEVICEARRIVAL case";

                    PDEV_BROADCAST_DEVICEINTERFACE lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
                    int i = 0;
                    QString s;
                    //to find a better way for this...
                    while(lpdbv->dbcc_name[i] != 0)
                    {
                        s.append(lpdbv->dbcc_name[i]);
                        i++;
                    }
                    s = s.toUpper();
                    if(s.contains(MY_DEVICE_VIDPID))
                        emit USB_Arrived();
                }
            break;
            case DBT_DEVICEREMOVECOMPLETE:
                if (lpdb -> dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
                {
                    qDebug() << "DBT_DEVICEREMOVECOMPLETE case";

                    PDEV_BROADCAST_DEVICEINTERFACE lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
                    int i = 0;
                    QString s;
                    //to find a better way for this...
                    while(lpdbv->dbcc_name[i] != 0)
                    {
                        s.append(lpdbv->dbcc_name[i]);
                        i++;
                    }
                    s = s.toUpper();
                    if(s.contains(MY_DEVICE_VIDPID))
                        emit USB_Removed();
                }
            break;
       case DBT_DEVICEREMOVEPENDING :
       {
           qDebug() << "DBT_DEVICEREMOVEPENDING case";
       }
       break;
       default:
       {
           qDebug() << "Went to Default case";
       }


       }
    }
    return false;
}

2 个答案:

答案 0 :(得分:0)

我想出来了,如果有其他人遇到类似的问题,这里就是解决方案。

问题是InterfaceClassGuid在下面一行。

GUID InterfaceClassGuid = HID_CLASSGUID;

HID_CLASSGUID在我的代码中设置为以下内容:

#define HID_CLASSGUID {0x4d1e55b2, 0xf16f, 0x11cf,{ 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}}

这是错的,我从例子中选择了这个并且从未意识到我需要改变它。注册不同类型的通知有不同的值,在这种情况下帮助系统没有帮助,但这里是有效GUID values的列表。

我将其更改为以下内容,现在我收到了所需的通知。

#define HID_CLASSGUID  {0x745a17a0,0x74d3, 0x11d0, 0xb6fe, 0x00a0c90f57da}

答案 1 :(得分:0)

此问题的另一个可能原因可能是传递给RegisterDeviceNotification(...)调用的窗口的句柄不正确。

错误代码:

  QMainWindow w;
  ...
  HANDLE windowId = w.window()->winId();
  RegisterDeviceNotification(&windowId,&NotificationFilter,DEVICE_NOTIFY_WINDOW_HANDLE);

我为winId的地址传递了错误的值,而wparam却一直得到7,尽管已检测到设备插入和移除。 在将其更改为使用窗口句柄的正确地址后,将其传递给RegisterDeviceNotification调用,它可以正常工作。 正确的代码:

 QMainWindow w;
 ...
 HANDLE *windowId = (HANDLE *)w.window()->winId();
 RegisterDeviceNotification(windowId,&NotificationFilter,DEVICE_NOTIFY_WINDOW_HANDLE);