Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. USB eject notification

USB eject notification

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J john5632

    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?

    J Offline
    J Offline
    Jochen Arndt
    wrote on last edited by
    #2

    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[^] .

    M J 2 Replies Last reply
    0
    • J Jochen Arndt

      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[^] .

      M Offline
      M Offline
      Munchies_Matt
      wrote on last edited by
      #3

      Very good reply! That is a little known and obscure part of the win API.

      L 1 Reply Last reply
      0
      • M Munchies_Matt

        Very good reply! That is a little known and obscure part of the win API.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        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:

        D M 2 Replies Last reply
        0
        • L Lost User

          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:

          D Offline
          D Offline
          Daniel Pfeffer
          wrote on last edited by
          #5

          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

          1 Reply Last reply
          0
          • L Lost User

            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:

            M Offline
            M Offline
            Munchies_Matt
            wrote on last edited by
            #6

            It is still a little known and obscure part of the API. :)

            1 Reply Last reply
            0
            • J Jochen Arndt

              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[^] .

              J Offline
              J Offline
              john5632
              wrote on last edited by
              #7

              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.

              J 1 Reply Last reply
              0
              • J john5632

                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.

                J Offline
                J Offline
                Jochen Arndt
                wrote on last edited by
                #8

                It can be also received by the WM_DEVICECHANGE handler as DBT_DEVICEQUERYREMOVE, DBT_DEVICEREMOVEPENDING, or DBT_CUSTOMEVENT. This requires calling RegisterDeviceNotification 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)[^].

                J 1 Reply Last reply
                0
                • J Jochen Arndt

                  It can be also received by the WM_DEVICECHANGE handler as DBT_DEVICEQUERYREMOVE, DBT_DEVICEREMOVEPENDING, or DBT_CUSTOMEVENT. This requires calling RegisterDeviceNotification 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)[^].

                  J Offline
                  J Offline
                  john5632
                  wrote on last edited by
                  #9

                  Thanks Jochen, Your approach worked well with MFC application but same approach not working with windows service. Any clue?

                  J L 2 Replies Last reply
                  0
                  • J john5632

                    Thanks Jochen, Your approach worked well with MFC application but same approach not working with windows service. Any clue?

                    J Offline
                    J Offline
                    Jochen Arndt
                    wrote on last edited by
                    #10

                    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.

                    1 Reply Last reply
                    0
                    • J john5632

                      Thanks Jochen, Your approach worked well with MFC application but same approach not working with windows service. Any clue?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #11

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups