1,需求:监听Windows下USB设备拔插事件,并将指定vid,pid的USB设备过滤出来。
QAbstractNativeEventFilter类:主要作用是捕获全局系统消息
怎么用?先来看下文档。
QAbstractNativeEventFilter::nativeEventFilter简介:
Linux example:
// Linux example
class MyXcbEventFilter : public QAbstractNativeEventFilter
{
public:
bool nativeEventFilter(const QByteArray &eventType, void *message, long *) override
{
if (eventType == "xcb_generic_event_t") {
xcb_generic_event_t* ev = static_cast<xcb_generic_event_t *>(message);
// ...
}
return false;
}
};
2,文档告诉我们,需要创建一个类去继承QAbstractNativeEventFilter,并重写nativeEventFilter方法。
直接上代码:
USBDeviceManager.h
#pragma once
#include <QObject>
#include <QDebug>
#include <QList>
#include <QApplication>
#include <QAbstractNativeEventFilter>
#ifdef WIN32
#include <windowsx.h>
#include <dwmapi.h>
#include <dbt.h>
#include <stdio.h>
#pragma comment(lib, "Dwmapi.lib")
#endif // WIN32
static const QList<GUID> GuidList = {
// GUID_DEVINTERFACE_USB_DEVICE
{ 0xA5DCBF10, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } },
// KSCATEGORY_CAPTURE
{ 0x65E8773D, 0x8F56, 0x11D0, { 0xA3, 0xB9, 0x00, 0xA0, 0xC9, 0x22, 0x31, 0x96 } },
//// GUID_DEVINTERFACE_DISK
//{ 0x53f56307, 0xb6bf, 0x11d0, { 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b } },
// GUID_DEVINTERFACE_HID,
{ 0x4D1E55B2, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } },
//// GUID_NDIS_LAN_CLASS
//{ 0xad498944, 0x762f, 0x11d0, { 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c } },
//// GUID_DEVINTERFACE_COMPORT
//{ 0x86e0d1e0, 0x8089, 0x11d0, { 0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73 } },
//// GUID_DEVINTERFACE_SERENUM_BUS_ENUMERATOR
//{ 0x4D36E978, 0xE325, 0x11CE, { 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 } },
//// GUID_DEVINTERFACE_PARALLEL
//{ 0x97F76EF0, 0xF883, 0x11D0, { 0xAF, 0x1F, 0x00, 0x00, 0xF8, 0x00, 0x84, 0x5C } },
//// GUID_DEVINTERFACE_PARCLASS
//{ 0x811FC6A5, 0xF728, 0x11D0, { 0xA5, 0x37, 0x00, 0x00, 0xF8, 0x75, 0x3E, 0xD1 } }
};
class USBDeviceManager : public QObject, public QAbstractNativeEventFilter {
Q_OBJECT
public:
USBDeviceManager();
~USBDeviceManager() = default;
USBDeviceManager(const USBDeviceManager&) = delete;
USBDeviceManager* operator=(const USBDeviceManager&) = delete;
public:
static USBDeviceManager& getInstance(); // 单例
void registerSystemMessage(HANDLE);
QString Wchat_tToQString(wchar_t*);
void usbDeviceFilter(bool, bool, QString);
protected:
bool nativeEventFilter(const QByteArray&, void*, long*) override;
};
把USBDeviceManager设计成一个单例,多继承于 QAbstractNativeEventFilter,QObject,方便使用Qt的信号与槽机制。
USBDeviceManager.cpp
#include "USBDeviceManager.h"
USBDeviceManager::USBDeviceManager() : QObject(nullptr), QAbstractNativeEventFilter() { }
USBDeviceManager& USBDeviceManager::getInstance() {
static USBDeviceManagerinstance;
return instance;
}
void USBDeviceManager::registerSystemMessage(HANDLE window) {
constexpr uint32_t Interface_Size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
for (const auto& guid : GuidList) {
DEV_BROADCAST_DEVICEINTERFACE device_interface;
ZeroMemory(&device_interface, Interface_Size);
device_interface.dbcc_size = Interface_Size;
device_interface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
device_interface.dbcc_classguid = guid;
if (!RegisterDeviceNotification(window, &device_interface, DEVICE_NOTIFY_WINDOW_HANDLE)) {
int Err = GetLastError();
qDebug() << "注册失败";
}
}
}
QString USBDeviceManager::Wchat_tToQString(wchar_t* name) {
QString ret = QString((QChar*)name, wcslen(name));
return ret;
}
bool USBDeviceManager::nativeEventFilter(const QByteArray& eventType, void* message, long* result) {
Q_UNUSED(eventType);
MSG* pMsg = reinterpret_cast<MSG*>(message);
if (pMsg->message == WM_DEVICECHANGE) {
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)pMsg->lParam;
// 过滤接入/拔出以外的消息
bool isDeviceArrival = pMsg->wParam == DBT_DEVICEARRIVAL;
bool isDeviceRemoved = pMsg->wParam == DBT_DEVICEREMOVECOMPLETE;
if (!isDeviceArrival && !isDeviceRemoved) { return false; }
// 过滤 device interface class 以外类型的消息
auto* broadcast = (DEV_BROADCAST_HDR*)pMsg->lParam;
if (!broadcast || broadcast->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE) { return false; }
// 过滤不监听的设备类型
auto* device_interface = (DEV_BROADCAST_DEVICEINTERFACE*)broadcast;
if (!device_interface || !GuidList.contains(device_interface->dbcc_classguid)) { return false; }
// 处理消息
QString path = Wchat_tToQString(device_interface->dbcc_name).toLower();
// 设备过滤
usbDeviceFilter(isDeviceArrival, isDeviceRemoved, path);
if (path.isEmpty() || !(path.contains("hid") || path.contains("usb"))) { return false; }
}
return false;
};
void USBDeviceManager::usbDeviceFilter(bool isDeviceArrival, bool isDeviceRemoved, QString path) {
if (path.contains("usb#vid_xxxxf&pid_xxxx")) {
if (isDeviceArrival) {
qDebug() << "isDeviceArrival";
}
if (isDeviceRemoved) {
qDebug() << "isDeviceRemoved";
}
}
}
注意:必须调用registerSystemMessage,否则很多USB设备会监听不到。
在主窗口调用方法:
USBDeviceManager::getInstance().registerSystemMessage(reinterpret_cast<HWND>(window->winId()));