I'm writing an application that detects when a certain USB Mass Storage Device is plugged-in, and when it is unplugged - by listening for WM_DEVICECHANGE messages.
I've also registered my application to listen for WM_DEVICECHANGE messages for DBT_DEVTYP_DEVICEINTERFACE (using the RegisterDeviceNotification API call) and I get both the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE messages when a USB Mass Storage Device is plugged-in or unplugged.
Now, the problem occurs when a USB device that has multiple volumes is plugged-in, and then unplugged.
I get the following messages when the device is plugged in:
WM_DEVICECHANGE(DBT_DEVICEARRIVALof typeDBT_DEVTYP_DEVICEINTERFACE)WM_DEVICECHANGE(DBT_DEVICEARRIVALof typeDBT_DEVTYP_VOLUME)WM_DEVICECHANGE(DBT_DEVICEARRIVALof typeDBT_DEVTYP_VOLUME)
And the following messages when it is plugged out:
WM_DEVICECHANGE(DBT_DEVICEREMOVECOMPLETEof typeDBT_DEVTYP_VOLUME)WM_DEVICECHANGE(DBT_DEVICEREMOVECOMPLETEof typeDBT_DEVTYP_DEVICEINTERFACE)
So, only one remove message even though there are two volumes. Why??
I have two questions:
- How can I correlate
DBT_DEVTYP_DEVICEINTERFACEmessages withDBT_DEVTYP_VOLUMEmessages (essentially, how do I know which VOLUME message corresponds to which DEVICEINTERFACE message - since I get them both for the device)? - Is there a way to make Windows notify me of both volume removals?
Ok, so I was able to answer one of my own questions: Is there a way to make Windows notify me of both volume removals?
Yes - even though windows sends only one
DBT_DEVTYP_VOLUMEWM_DEVICECHANGEmessage, you actually do get notified of both volume removals - but, as always, the answer lies deep down buried in MSDN:So, all I had to do was ignore the example function that Microsoft gives in one of their samples,
And replace it with a function that interprets the mask for all drives affected. So the one message I was getting was indeed for both volumes, and both volume drive letters were available in the mask.
I still have the other question to answer though - how can the two messages (
DBT_DEVTYP_DEVICEINTERFACEandDBT_DEVTYP_VOLUME) be correlated?