在Qt上检测到连接/断开的新USB设备

时间:2016-07-22 14:14:35

标签: windows qt events notifications usb

我希望在连接或断开新USB设备时收到通知(设备隐藏)。当有usb设备更改但我不知道设备是连接还是断开连接时,我已成功收到通知。 我收到的消息(当USB连接或分离时)是相同的: 消息:537(VM_DEVICECHANGE) wParam:7 lParam:0

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QAbstractEventDispatcher>
#include <QAbstractNativeEventFilter>
#include <QDebug>
#include <windows.h>
#include <dbt.h>
#include <QObject>

class MyNativeEventFilter : public QAbstractNativeEventFilter {
public :
    virtual bool nativeEventFilter( const QByteArray &eventType, void *message, long *result )
    Q_DECL_OVERRIDE
    {
        if (eventType == "windows_generic_MSG")
        {
          MSG *msg = static_cast<MSG *>(message);
          static int i = 0;

              msg = (MSG*)message;
                  qDebug() << "message: " << msg->message << " wParam: " << msg->wParam
                      << " lParam: " << msg->lParam;
              if (msg->message == WM_DEVICECHANGE)
              {
                  qDebug() << "WM_DEVICECHANGE";
              }
            }
        return false;
    }
};

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    MyNativeEventFilter myEventfilter;
    app.eventDispatcher()->installNativeEventFilter(&myEventfilter);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

2 个答案:

答案 0 :(得分:0)

WM_DEVICECHANGE message并不能真正告诉您设备是连接还是已断开连接。收到该消息后,您的应用程序应使用SetupAPI查看所有已连接的USB设备并采取适当的措施。

答案 1 :(得分:0)

以下是我在其中一个项目中使用以获取USB连接/断开连接通知的内容:

/** deviceeventfilter.h **/
class DeviceEventFilter: public QObject, public QAbstractNativeEventFilter
{
    Q_OBJECT
public:
    explicit DeviceEventFilter(QObject *parent = 0);
    virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result);

signals:
    void newUSBDevice();
    void lostUSBDevice();

public slots:
    void registerEvent(QWindow *window);
};

/** deviceeventfilter.cpp **/
DeviceEventFilter::DeviceEventFilter(QObject *parent) :
    QObject(parent)
{

}

bool DeviceEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{
    Q_UNUSED(eventType);
    Q_UNUSED(result);
#ifdef Q_OS_WIN32
    MSG *msg = (MSG *)message;
    if( WM_DEVICECHANGE == msg->message && DBT_DEVICEARRIVAL == msg->wParam )
    {
        qDebug("USB arrival detected!");
        emit newUSBDevice();
    }
    else if( WM_DEVICECHANGE == msg->message && DBT_DEVICEREMOVECOMPLETE == msg->wParam )
    {
        qDebug("USB departure detected!");
        emit lostUSBDevice();
    }
#endif
    // Return false so that the event is propagated
    return false;
}


void DeviceEventFilter::registerEvent(QWindow *window)
{
    if( window != nullptr )
    {
#ifdef Q_OS_WIN32
        GUID guid = ...;

        DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;

        ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
        NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
        NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
        NotificationFilter.dbcc_classguid = guid;
        HDEVNOTIFY hDevNotify = RegisterDeviceNotification((HANDLE)window->winId(), &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
        if( NULL == hDevNotify )
        {
            // Print error
        }
#endif
    }

}

/** main.cpp **/
int main()
{
    ...
    DeviceEventFilter event_filter;
    QWindow *w = qApp->allWindows().first();
    event_filter.registerEvent(w);
    app.installNativeEventFilter(&event_filter);
    ...
}