Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect insertion of media into a drive using windows messages

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?

like image 874
rjschnorenberg Avatar asked Mar 10 '10 20:03

rjschnorenberg


2 Answers

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.

like image 94
Luke Avatar answered Oct 21 '22 14:10

Luke


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!

like image 2
caesarvanou Avatar answered Oct 21 '22 13:10

caesarvanou