Main Site Documentation

Detecting 'eject media' from the PC


The only state I have not been able to detect with the mass storage class is when you ‘eject’ the device on the PC. The USB client connection does not change of course because the USB connection is still intact. Any ideas on detecting an ‘eject media’ event from the PC?


Just an idea : you could try to monitor the USB stream for a specific sequence on an endpoint, then when or if you have it, just create a thread that is checking for this particular sequence ?
This thread would be running as soon as the media is ready, for example.


I have been using the ‘EZ’ methods provided in the new USBClientController.StandardDevices class. It looks like in order to have access to the underlying USB bits you would have to create a custom USB device descriptor and then use USBClientController.Start to start it; that way you have access to the underlying device?


Hmmm… :confused: Looks like lots of pain for not much result, in this case.

Also, I think at something : is the “Eject media” a real USB command ? I mean, isn’t it only a flag for Windows internal use ? In which case it can’t be detected as an event. I didn’t search for this, so I may be wrong.


Well I have seen conflicting information on if the host must notify the client of an ‘eject’ being done. ‘Eject’, meaning to tell your PC to ‘Safely eject Media’. Some posts I have read said that a host is not required to do anything except complete what it is doing and stop sending packets.

I found this document: that specifies a PREVENT-ALLOW MEDIUM REMOVAL Command, with which the host can tell the client that is should prevent or allow media removal. It is not clear if this must be sent after an eject though.

For my uses I can live with just looking for the USB cable to be unplugged, but I am curious how you might be able to sense when the host has done an ‘eject’.


If the host sends such a command, then an USB sniffer will see it.
The hard part is to find out what the command is in the continuous flow of data :confused:

Hopefully, if the device is idle, then the data will always be the same. So a different one will show that something happened. If that “something” is you (the host) asking for “Eject media”, then you just got it :wink:

I’ve used this method to detect the sequence that gets the Phidgets interface kit serial number.


“Eject media” might mean something to Windows to flush all data, but there is no specific USB command…
Windows might power down the device when ejected, check USB Port State and see if it is running or not.