WM_DEVICECHANGE 在我的 Qt 应用程序中收到但未收到 DBT_DEVICEARRIVAL

WM_DEVICECHANGE received but not DBT_DEVICEARRIVAL in my Qt app

我正在按照一个示例来检测 Windows 中的 USB 闪存驱动器插件和拔出检测 7. 我确实收到了通知 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;
}

我解决了这个问题,如果其他人遇到类似问题,这里是解决方案。

问题出在下面的行 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}

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

错误代码:

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

我为 winId 的地址传递了错误的值,尽管检测到设备插入和移除,但我的 wparam 一直是 7。 在我将其更改为使用 window 句柄的正确地址后,我将其传递给 RegisterDeviceNotification 调用并且它起作用了。 正确代码:

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