USB eject notification
-
I have USB drive inserted in computer. My application should be notified when user click in eject button from system tray. Is there any way to get notification?
You can do that by handling the WM_DEVICECHANGE message (Windows)[^] in the main window of your application. See also Detecting Media Insertion or Removal (Windows)[^] and Hardware Change Detection[^] .
-
You can do that by handling the WM_DEVICECHANGE message (Windows)[^] in the main window of your application. See also Detecting Media Insertion or Removal (Windows)[^] and Hardware Change Detection[^] .
Very good reply! That is a little known and obscure part of the win API.
-
Very good reply! That is a little known and obscure part of the win API.
Hmmmm, Welcome to the C++ forum where every question has been asked hundreds of times repeatedly. :) Re: how can i make USB filter driver inf or regedit file? - Hardware & Devices Discussion Boards[^] Maybe we should create an autoresponder bot that answers every question with an automatic search of previous similar questions. Wait...hang on... maybe Jochen Arndt[^] is a bot? :omg:
-
Hmmmm, Welcome to the C++ forum where every question has been asked hundreds of times repeatedly. :) Re: how can i make USB filter driver inf or regedit file? - Hardware & Devices Discussion Boards[^] Maybe we should create an autoresponder bot that answers every question with an automatic search of previous similar questions. Wait...hang on... maybe Jochen Arndt[^] is a bot? :omg:
Randor wrote:
maybe Jochen Arndt[^] is a bot?
By his profile picture, he's a boot (or possibly a pair of them) :)
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill
-
Hmmmm, Welcome to the C++ forum where every question has been asked hundreds of times repeatedly. :) Re: how can i make USB filter driver inf or regedit file? - Hardware & Devices Discussion Boards[^] Maybe we should create an autoresponder bot that answers every question with an automatic search of previous similar questions. Wait...hang on... maybe Jochen Arndt[^] is a bot? :omg:
It is still a little known and obscure part of the API. :)
-
You can do that by handling the WM_DEVICECHANGE message (Windows)[^] in the main window of your application. See also Detecting Media Insertion or Removal (Windows)[^] and Hardware Change Detection[^] .
Thanks Jochen, WM_DEVICECHANGE message is to handle device removal and insertion event. But my requirement is something different. I want to handle a notification when user click on "eject" option from system tray. This event should occur before WM_DEVICECHANGE I think. Please help me to do so.
-
Thanks Jochen, WM_DEVICECHANGE message is to handle device removal and insertion event. But my requirement is something different. I want to handle a notification when user click on "eject" option from system tray. This event should occur before WM_DEVICECHANGE I think. Please help me to do so.
It can be also received by the
WM_DEVICECHANGE
handler asDBT_DEVICEQUERYREMOVE
,DBT_DEVICEREMOVEPENDING
, orDBT_CUSTOMEVENT
. This requires callingRegisterDeviceNotification
with appropriate settings. But that will not support volumes (DBT_DEVTYP_VOLUME
) because arrival and removal evens are send by default. The only solution I know is opening a device handle for the USB drive and using that to register. Than you will get informed about the eject request and can reject the request or accept it after closing the device handle. See Device Events (Windows)[^]. -
It can be also received by the
WM_DEVICECHANGE
handler asDBT_DEVICEQUERYREMOVE
,DBT_DEVICEREMOVEPENDING
, orDBT_CUSTOMEVENT
. This requires callingRegisterDeviceNotification
with appropriate settings. But that will not support volumes (DBT_DEVTYP_VOLUME
) because arrival and removal evens are send by default. The only solution I know is opening a device handle for the USB drive and using that to register. Than you will get informed about the eject request and can reject the request or accept it after closing the device handle. See Device Events (Windows)[^]. -
Thanks Jochen, Your approach worked well with MFC application but same approach not working with windows service. Any clue?
See RegisterDeviceNotification function (Windows)[^]:
Quote:
Services can use the RegisterDeviceNotification function to register to receive device notifications. If a service specifies a window handle in the hRecipient parameter, the notifications are sent to the window procedure. If hRecipient is a service status handle, SERVICE_CONTROL_DEVICEEVENT notifications are sent to the service control handler. For more information about the service control handler, see HandlerEx.
-
Thanks Jochen, Your approach worked well with MFC application but same approach not working with windows service. Any clue?
Hi, There is an amazing amount of high quality information right here on codeproject dating back almost 18 years. Much of it is still relevant. Just use the search box up at the top of the page. Receiving Device Event Notification in Windows Service[^] Best Wishes, -David Delaune