使用Windows消息检测到介质插入驱动器 [英] Detect insertion of media into a drive using windows messages

查看:145
本文介绍了使用Windows消息检测到介质插入驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用WM_DEVICECHANGE在新的USB驱动器连接到计算机时收到通知。这对于像拇指驱动器的设备非常有用,其中一旦设备到达,它就准备好从其读取文件。对于像SD读卡器这样的设备,不是因为当设备连接时消息被发送一次,而当用户实际将卡插入设备时不发送消息。

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.

是否可以检测到将新介质插入现有USB设备,而无需使用轮询?

Is it possible to detect the insertion of new media into an existing USB device without having to use polling?

推荐答案

我刚才在几个星期前做了这个。从技术上讲,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.

这篇关于使用Windows消息检测到介质插入驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆