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?
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With