Windows 卷设备检测失败,直到重新启动。 以前从未失败过
我有代码可以检测 USB 闪存驱动器作为卷的连接。 该代码已经工作了一段时间,但最近一位工程师同事的机器开始出现故障,直到重新启动后才再次正常工作。
该项目使用 Qt 4.5.0,但这应该与这个问题不太相关。
我按如下方式注册通知,
// Register for device connect notification
DEV_BROADCAST_DEVICEINTERFACE devInt;
ZeroMemory( &devInt, sizeof(devInt) );
devInt.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
devInt.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
devInt.dbcc_classguid = GUID_DEVINTERFACE_VOLUME;
m_hDeviceNotify =
RegisterDeviceNotification( winId(), &devInt, DEVICE_NOTIFY_WINDOW_HANDLE );
然后处理程序过滤 WM_DEVICECHANGE 消息。
if (message->message == WM_DEVICECHANGE)
{
switch (message->wParam)
{
case DBT_DEVICEARRIVAL:
HandleVolumeArrival( message );
break;
case DBT_DEVICEREMOVECOMPLETE:
HandleVolumeRemoval( message );
break;
default:
break;
}
*result = TRUE;
} // end if
然后,到达消息处理程序按如下方式处理消息:
void HandleVolumeArrival( MSG *message ) { if(消息->lParam == 0) { qDebug() << “设备到达时 lParam 为 0”; 返回; 卷类型的
PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR) message->lParam;
if(pHdr->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME pVol = (PDEV_BROADCAST_VOLUME) pHdr;
// Handling of the volume is performed here
} // end if
检查
设备类型时出现问题:
pHdr->dbch_devicetype == DBT_DEVTYP_VOLUME
失败时,设备类型被报告为 DBT_DEVTYP_DEVICEINTERFACE。
尝试了多个 USB 驱动器,所有 USB 驱动器都存在相同的问题。
以前有人见过这样的事情吗? 您是否知道可能导致此问题的原因是什么,或者为什么问题会在系统重新启动后消失?
I have code to detect the connection of USB Flash Drives as volumes. The code has been working very well for awhile, but recently a fellow engineer's machine started to fail and didn't work right again until it was restarted.
The project uses Qt 4.5.0, but that shouldn't be very relevant to this question.
I register for the notification as follows
// Register for device connect notification
DEV_BROADCAST_DEVICEINTERFACE devInt;
ZeroMemory( &devInt, sizeof(devInt) );
devInt.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
devInt.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
devInt.dbcc_classguid = GUID_DEVINTERFACE_VOLUME;
m_hDeviceNotify =
RegisterDeviceNotification( winId(), &devInt, DEVICE_NOTIFY_WINDOW_HANDLE );
The handler then filters for the WM_DEVICECHANGE messages.
if (message->message == WM_DEVICECHANGE)
{
switch (message->wParam)
{
case DBT_DEVICEARRIVAL:
HandleVolumeArrival( message );
break;
case DBT_DEVICEREMOVECOMPLETE:
HandleVolumeRemoval( message );
break;
default:
break;
}
*result = TRUE;
} // end if
The arrival message handler then handles the message as such:
void HandleVolumeArrival( MSG *message )
{
if(message->lParam == 0)
{
qDebug() << "lParam is 0 on Device Arrival";
return;
} // end if
PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR) message->lParam;
if(pHdr->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME pVol = (PDEV_BROADCAST_VOLUME) pHdr;
// Handling of the volume is performed here
} // end if
} // end HandleVolumeArrival
The problem came when checking the device type for a volume type:
pHdr->dbch_devicetype == DBT_DEVTYP_VOLUME
When it was failing, the device type was being reported as DBT_DEVTYP_DEVICEINTERFACE
.
Multiple USB drives were tried and all had the same problem.
Has anyone seen anything like this before? Do you know what could cause it or why the problem would go away on a system restart?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,无论如何您都会正常看到
DBT_DEVTYP_DEVICEINTERFACE
。 USB 设备是自描述的。 USB 设备可以具有任何“接口”,其中每个接口都是该设备的一个功能。 我的猜测是,当连接 USB 时,每个 USB 设备接口都会得到一个“DBT_DEVTYP_DEVICEINTERFAC
E”,以便 USB 设备驱动程序可以说它们可以处理该 USB 接口。 我假设 USB 大容量存储驱动程序的 USB 设备驱动程序将通过安装卷来处理此消息,然后您将收到DBT_DEVTYP_VOLUME
消息。我猜测大容量存储接口驱动程序无法正常工作(或已崩溃)并且未处理
DBT_DEVTYP_DEVICEINTERFACE
。 除非你开始经常看到这种情况,否则我认为你不应该费心去处理这种情况。My guess would be that you would see the
DBT_DEVTYP_DEVICEINTERFACE
normally anyway. USB devices are self-describing. A USB device can have any "interfaces" where each interface is a feature of the device. My guess is that when a USB is connected you get a "DBT_DEVTYP_DEVICEINTERFAC
E" per USB device interface so that a USB device driver can say that they can handle that USB interface. I would assume that the USB device driver for USB mass storage driver would would handle this messages by mounting the volume and then you would get theDBT_DEVTYP_VOLUME
message.I would guess that the mass storage interface driver is not working correctly (or has crashed) and is not handling the
DBT_DEVTYP_DEVICEINTERFACE
. Unless you starting seeing it a lot I don't think it is a situation that you should bother to handle.