views:

385

answers:

3

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?

A: 

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.

Ben Voigt
AS it happens, new volume notifications are sent by default.
MSalters
Right, but the drive letter (volume) is assigned when a card reader is inserted. The media change notification might require RegisterDeviceNotification.
Ben Voigt
+1  A: 

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.

Luke
Good call. The library that implements that is present on every system. But does the DisableAutorunDriveType registry setting affect that?
Ben Voigt
I did not test that, but I wouldn't think the two are connected. This mechanism is used by the "Remove Hardware Safely" tray app; even if autorun is disabled, that tray app still needs to receive arrival / removal notifications.
Luke
The SHCNE_MEDIAINSERTED message is being sent even if the DriveTypeAutoRun registry setting is set to disable all devices.
rschnorenberg
A: 

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 [http://www.codeproject.com/KB/dotnet/devicevolumemonitor.aspx?msg=2897423#xx2897423xx]

Quin Arnold