CD Insertion

Is there a way I can tell when a CD is inserted into the CD drive without using a timer control to check every second?
rj_bAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LeXienCommented:
try sticking some sort of ShellHook into something - dunno, just an idea - this may require a day on MSDN.
0
mcriderCommented:
Here you go!  This example creates a windows hook on form1 and notifies you when a CD is mounted and unmounted.

1) Create a new project.

2) Add two commandbuttons to the form.  Name them "cmdHook" and "cmdUnHook".

3) Add the following code to the DECLARATIONS SECTION of Form1:

    Private Sub cmdHook_Click()
        Hook
    End Sub
    Private Sub cmdUnHook_Click()
        UnHook
    End Sub
    Private Sub Form_Load()
        gHW = Me.hWnd
    End Sub

4) Add the following code to a MODULE:

    Global lpPrevWndProc As Long
    Global gHW As Long
    Public Const GWL_WNDPROC = -4
    Public Const WM_DEVICECHANGE = 537
    Public Const DBT_DEVICEARRIVAL = 32768
    Public Const DBT_DEVICEREMOVECOMPLETE = 32772
    Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
           (ByVal lpPrevWndFunc As Long, _
            ByVal hWnd As Long, _
            ByVal Msg As Long, _
            ByVal wParam As Long, _
            ByVal lParam As Long) As Long
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
           (ByVal hWnd As Long, _
            ByVal nIndex As Long, _
            ByVal dwNewLong As Long) As Long
    Public Sub Hook()
        lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)
    End Sub
    Public Sub UnHook()
        Dim lngReturnValue As Long
        lngReturnValue = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
    End Sub
    Function WindowProc(ByVal hw As Long, _
                        ByVal uMsg As Long, _
                        ByVal wParam As Long, _
                        ByVal lParam As Long) As Long
        Select Case uMsg
            Case WM_DEVICECHANGE 'CHECK FOR THE DEVICE CHANGE MESSAGE
                Select Case wParam
                    Case DBT_DEVICEARRIVAL ' DISK MOUNTED
                        MsgBox "CD Inserted"
                    Case DBT_DEVICEREMOVECOMPLETE 'DISK UNMOUNTED
                        MsgBox "CD Removed"
                    Case Else
                        Debug.Print "wParam:", wParam
                End Select
            Case Else
                WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
        End Select
    End Function


5) Run the program and click the "cmdHook" Button.

6) Eject the CD.

7) Insert a CD.

8) Eject the CD.

9) Click the "cmdUnHook" button.

10) Stop the program.

YOU MUST UNHOOK THE WINDOWS HOOK BEFORE STOPPING THE PROGRAM, OTHERWISE IT WILL CRASH!


Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rj_bAuthor Commented:
Thanks much -- works perfect.
0
mcriderCommented:
Thanks for the points! Glad I could help!


Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.