使用 Windows 消息检测媒体插入驱动器的情况
我目前正在使用 WM_DEVICECHANGE 在新 USB 驱动器连接到计算机时收到通知。这对于拇指驱动器等设备非常有用,一旦设备到达,就可以从中读取文件。对于像 SD 卡读卡器这样的设备,则不会,因为当设备连接时消息会发送一次,但当用户实际将卡插入设备时不会发送消息。
是否可以在不使用轮询的情况下检测新媒体插入现有 USB 设备的情况?
I am currently using WM_DEVICECHANGE to be notified when new USB drives are connected to the computer. This works great for devices like thumb-drives where as soon as the device arrives it is ready to have files read from it. For devices like SD card readers it does not because the message is sent out once when the device is connected but no message is sent when a user actually inserts a card into the device.
Is it possible to detect the insertion of new media into an existing USB device without having to use polling?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我几周前刚做了这个。从技术上讲,RegisterDeviceNotification 路线是正确的方法,但它需要大量的工作才能正确。然而,Windows 资源管理器已经为您完成了所有艰苦的工作。只需将 SHChangeNotifyRegister 与 SHCNE_DRIVEADD / SHCNE_DRIVEREMOVED / SHCNE_MEDIAINSERTED / SHCNE_MEDIAREMOVED 结合使用即可。请注意,此方法取决于 Shell 硬件检测服务(或任何名称),但它比尝试自己重新实现该功能要容易得多。
I just did this a few weeks ago. Technically speaking the RegisterDeviceNotification route is the proper way to go, but it requires a decent amount of work to get right. However, Windows Explorer already does all of the hard work for you. Just use SHChangeNotifyRegister with SHCNE_DRIVEADD / SHCNE_DRIVEREMOVED / SHCNE_MEDIAINSERTED / SHCNE_MEDIAREMOVED. Note that this method depends on the Shell Hardware Detection service (or whatever it is called), but it's much easier than trying to re-implement the functionality yourself.
“Change Notify Watcher Sample”中有一个很好的使用
SHChangeNotifyRegister
的示例。从以下地址下载:
http://msdn.microsoft.com/en -us/library/dd940348.aspx#downloading
我在 Qt 代码中实现了 SHChangeNotifyRegister。
感谢卢克的出色回答!
There is a very good example of the usage of
SHChangeNotifyRegister
in the "Change Notify Watcher Sample".Download it from this address:
http://msdn.microsoft.com/en-us/library/dd940348.aspx#downloading
I implemented SHChangeNotifyRegister in my Qt code.
Thanks to Luke for his very good answer!
可能是 SD 读卡器驱动程序中的错误。 WM_DEVICECHANGE 中的描述为“DBT_DEVICEARRIVAL 0x8000 A设备或媒体已插入并且现在可用。”
因此,您应该收到媒体插入的通知。
您是否尝试使用RegisterDeviceNotification?默认情况下仅发送一些通知,媒体更改可能不是其中之一。
Could be a bug in the SD reader driver. The description in WM_DEVICECHANGE is "DBT_DEVICEARRIVAL 0x8000 A device or piece of media has been inserted and is now available."
Therefore, you're supposed to get a notification for media insertion.
Did you try using RegisterDeviceNotification? Only some notifications are sent by default, and media change might not be one of them.
也许这是设备驱动程序中的一个错误(我当然认为是),但它们似乎都以相同的方式工作......不是。因此,在这一点上,我认为您无法让任何设备驱动程序开发人员更改其设备特征,即使微软(可能是最大的开发人员)让您相信它应该以这种方式工作(不适用于我的 Windows7 微软驱动程序)。将 SD 卡插入 USB 设备不会触发 WM_DEVICECHANGE 消息。
对媒体类型设备使用 SHChangeNotifyRegister。它不需要注册设备通知。您可以通过 SHGetPathFromIDList 获取驱动器号。使用 SHCNE_MEDIAINSERTED 和 SHCNE_MEDIAREMOVED。
如果您正在寻找硬件设备而不是媒体设备,请使用设备 ONDeviceNotify,或者您可以编写自己的设备驱动程序。
请参阅此评论的代码 CodeProject - 捕获 Windows 消息
Perhaps it is a bug in the device drivers (I certainly think it is), but they all appear to work in the same way... NOT. So at this point I don't think you can get any device driver devlopers to change their device characteristices even if Microsoft (probably the largest developer) leads you to believe it should work this way (Not working for my Windows7 microsoft driver). The insertion of an SD card into a USB device simply does not trigger a WM_DEVICECHANGE message.
Use SHChangeNotifyRegister for media type devices. It does not require a register devicenotification. And you can get the drive letter via SHGetPathFromIDList. Use SHCNE_MEDIAINSERTED and SHCNE_MEDIAREMOVED.
Use device ONDeviceNotify if you are looking for a hardware device rather than a media device, or you might be able to write a device driver of your own.
See this comment's code CodeProject - Trapping windows messages