最近在做一个通用对话框,类似于windows的资源管理器,当然了没有windwos资源管理器那么强大。用户报了一个bug,说通用对话框打开之后不能实时监控U盘插入,随手在百度上搜索了一圈,这个问题还是挺多人在搞,都大同小异,基本都是监控windows的事件。下面说下我自己解决该问题的流程。
一、windows消息
WM_DEVICECHANGE:附上WM_DEVICECHANGE消息的链接,此消息意思是当计算机的硬件配置或者设备发生变化时通知应用程序,此消息下在wParam参数中包含了此事件的事件类型。英文:Notifies an application of a change to the hardware configuration of a device or the computer.
此消息类型下的事件类型有12个,如下表所示
|
A request to change the current configuration (dock or undock) has been canceled. |
更改当前配置的请求已被取消 |
|
The current configuration has changed, due to a dock or undock. |
由于插入或移除,当前的配置已经改变。 |
|
A custom event has occurred. |
定制事件 |
|
A device or piece of media has been inserted and is now available. |
插入新的设备 |
|
Permission is requested to remove a device or piece of media. Any application can deny this request and cancel the removal. |
请求移除设备,可以失败 |
|
A request to remove a device or piece of media has been canceled. |
去除中断 |
|
A device or piece of media has been removed. |
设备被移除 |
|
A device or piece of media is about to be removed. Cannot be denied. |
即将删除,仍然有效 |
|
A device-specific event has occurred. |
发生设备特定的事件 |
|
A device has been added to or removed from the system. |
设备被新增或者移除 |
|
Permission is requested to change the current configuration (dock or undock). |
请求权限来更改当前的配置 |
|
The meaning of this message is user-defined. |
此消息的含义是用户定义的 |
上表所示的消息中我们用到了其中3个事件,这三个事件分别在合适的时机来获取优盘事件;初次之外还可以监控DBT_DEVNODES_CHANGED事件,此事件在优盘插拔时不止一次的响应,如果用户需要区分插拔事件则此事件不可以。
DBT_DEVICEARRIVAL://检测到新设备
DBT_DEVICEREMOVECOMPLETE://设备被移除
DBT_CUSTOMEVENT://右键弹出设备时响应,此事件只有用户进行注册过后才会收到
二、注册设备通知
注册设备状态发生变化时通知,需要使用RegisterDeviceNotification接口,注册方法代码如下,关键注释都有,就不解释了。
bool diskoperate::registerDisk(const QString & cDiskName)
{
if (!IsDiskExist(cDiskName.at().toLatin1()))
{
return false;
} const UINT oldmode = ::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);//不弹出系统提示
HANDLE handle = CreateFile(
cDiskName.toStdWString().c_str() ,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_NORMAL,
);//拿到盘符句柄
if (handle == nullptr)
{
return false;
}
DEV_BROADCAST_HANDLE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof (NotificationFilter) );
NotificationFilter.dbch_size = sizeof (DEV_BROADCAST_HANDLE );
NotificationFilter.dbch_devicetype = DBT_DEVTYP_HANDLE;
NotificationFilter.dbch_handle = handle;
HDEVNOTIFY hDevNotify = RegisterDeviceNotification((HWND)this->winId()
, &NotificationFilter
, DEVICE_NOTIFY_WINDOW_HANDLE);//注册设备通知 CloseHandle(handle);//关闭盘符句柄
::SetErrorMode(oldmode);//恢复之前错误模式
if (!hDevNotify)
{
return false;
} m_lstMoveDrive[cDiskName.at()] = hDevNotify; return true;
}
上述方法中用了一个判断当前盘符是否存在的函数IsDiskExist,此函数也比较简单,用过GetLogicalDrives接口获取当前系统已有盘符,进行比较即可。
bool IsDiskExist(char cDiskName)
{
DWORD dwDrivers;
int i = toupper(cDiskName) - 'A'; //dwDrivers的每一个二进制位表示对应的驱动器是否存在。
dwDrivers = GetLogicalDrives();
//判断当前位是否有驱动器
if ((dwDrivers & ( << (i))) != )
{
return true;
}
return false;
}
三、获取系统事件
Qt给我们提供了一个QAbstractNativeEventFilter类,该类可以过滤应用程序的所有事件,因此我们的类需要继承并实现此类中的nativeEventFilter方法,在此方法中进行事件过滤,之前写过一个相关的文章qt捕获全局windows消息可以进行参考下,,nativeEventFilter方法想要进行事件过滤,我们必须使用qApp->installNativeEventFilter(this);方法进行注册我们自己写的类。
bool diskoperate::nativeEventFilter( const QByteArray &eventType, void *message, long *result )
{
if ("windows_dispatcher_MSG" == eventType
|| "windows_generic_MSG" == eventType)
{
MSG * msg = reinterpret_cast<MSG *>(message);
if(msg->message == WM_DEVICECHANGE)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
switch(msg->wParam)
{
case DBT_DEVICEARRIVAL://检测到新设备
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
qDebug() << "DBT_DEVICEARRIVAL";
updateMoveDrives();
}
break;
case DBT_DEVICEQUERYREMOVE://请求移除设备,可能失败 此时刷新不会让移动设备消失
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
qDebug() << "DBT_DEVICEQUERYREMOVE";
}
break;
case DBT_DEVICEQUERYREMOVEFAILED://去除中断
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
qDebug() << "DBT_DEVICEQUERYREMOVEFAILED";
}
break;
case DBT_DEVICEREMOVEPENDING://即将删除,仍然有效
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
qDebug() << "DBT_DEVICEREMOVEPENDING";
}
break;
case DBT_DEVICEREMOVECOMPLETE://设备不见了
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
qDebug() << "DBT_DEVICEREMOVECOMPLETE";
updateMoveDrives();
}
break;
case DBT_CUSTOMEVENT:
if (lpdb->dbch_devicetype == DBT_DEVTYP_HANDLE)
{
qDebug() << "DBT_CUSTOMEVENT";
updateMoveDrives();
}
break;
case DBT_DEVNODES_CHANGED:
qDebug() << "DBT_DEVNODES_CHANGED";
updateMoveDrives();
break;
default:
qDebug() << msg->wParam;
}
outputDrives();
}
} return __super::nativeEvent(eventType, message, result);
}
四、运行效果
如下图1所示,一次完整的优盘插入、弹出和删除,所触发的系统事件
图1 测试截图
五、下载连接
相关连接: