No announcement yet.

Detect CD eject and insert

  • Filter
  • Time
  • Show
Clear All
new posts

  • Detect CD eject and insert

    I’m trying to monitor a cdrom drive to detect when certain cds are present. Using the built-in Drive actions, GetType, GetSize, and GetInformation, I can find the drive, get the size and label if a disk is present. By themselves, these actions work whether a disk is in the drive or not, and even if the tray is ejected. When I start adding more complexity to the code, Windows takes over and displays a message stating there is no disk is in the drive.
    Is there a better way to monitor the status of a cd drive, so I would only use GetInformation if I know a disk is inserted?
    Thanks, craig

  • #2
    Note: If the target drive is a CD-ROM drive or floppy drive, there must be a disk in the drive for this action to work. If there is no disk in the drive, nil is returned.

    From Drive.GetInformation, so you can test if a drive has media in it by testing for nil and to monitor a drive start a timer and then constantly check.

    As far as I can see the following Volume Management functions are available:

    If you want to look through and see if there is anything better then I'll help you code the function.


    • #3
      I was testing for media by using a timer and GetSize, which returns -1 if no disk is present even if the tray is ejected. This seems to work until a disk is inserted and then ejected, then the errors start popping up.
      From what I’ve read, there is an event that monitors changes in hardware, including cd inserts and ejects. WM_DEVICECHANGE.
      Notifies an application of a change to the hardware configuration of a device or the computer.

      here’s a couple more articles.

      Any ideas how to use this in AMS?


      • #4
        Yes, use the MemoryEx plugin then subclass the window returned from Application.GetWindowHandle() and you will receive that message.


        • #5
            -- Global
            WM_DEVICECHANGE = 0x0219;
            -- Page OnShow
            local s = Subclass.Create(Application.GetWndHandle(),function(hWnd,uMsg,wParam,lParam)
            if(uMsg == 0x0002)then
             return 0
            if(uMsg == WM_DEVICECHANGE)then
             -- Your code here for handling the message
             -- wParam = event
             -- lParam = pointer to a defined structure
            return Subclass.OldWinProc(hWnd,uMsg,wParam,lParam)
            -- On Exit


          • #6
            Thanks, Shrek. It works slick! I’ve been too intimidated to use MemoryEx until now. Had to read through the Help and refer to the MS documentation several times before making sense of defined structures, but I finally figured it out. Your sample code was very helpful and got me going in the right direction. Thank you!


            • #7
              You know for an AutoPlay IDE one really would have thought WM_DEVICECHANGE would have been one of the events available.