Problem detecting WM_DEVICEREMOVALCOMPLETE event

shafi

Member
Joined
Feb 1, 2007
Messages
9
Programming Experience
Beginner
Hello,

I am using windows broadcast message to detect when USB device (EZ web cam) is connected and when it is removed.

I have a code that override the WinProc fucntion which gets called once when I connect USB device, this is probably for the WM_DEVICEARRIVAL event.

The same function is called two times when I disconnect the USB device. This is probably for WM_DEVICEREMOVAL and WM_DEVICEREMOVALCOMPLETE events.

For some reason when I set breakpoint in this function then I see same value of &H7 for m.WParam.ToInt32() for all these three events. Is there anything I am missing which causes same value for m.WParam member?

Below is my code snippet. Thanks.
++++++++++++++++++
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Dim index As Int32

If m.Msg = &H219 Then ' WM_DEVICECHANGE

If (m.WParam.ToInt32() = &H8000) Then
MsgBox("Device Arrival!")
End If

If (m.WParam.ToInt32() = &H8004) Then
MsgBox("Device Removed!")
End If

End If

MyBase.WndProc(m)

End Sub
+++++++++++++++++++++++++++++++++++++++++
 
WParam value &H7 for WM_DeviceChange message is the DBT_DEVNODES_CHANGED notification. Why is that a problem for you? DBT_DEVICEREMOVECOMPLETE and DBT_DEVICEARRIVAL only reports once.
 
I might be wrong, but based on my understanding: If I want to detect a USB device connection, then I have to look for DBT_DEVICEARRIVAL and in order to detect remove I have to look for DBT_DEVICEREMOVECOMPLETE. This is what I would liek to achieve. Am I doing anything incorrect or is it not possible to accomplish this using WM_DEVICECHANGE event?

Thanks.

Shafi.
 
The code in your first post works just fine for what you want. I don't understand why you find DBT_DEVNODES_CHANGED notices a problem?
 
Though my code seems correct it does not work. When I connect a USB device I expect see the messages. But I do not see any messages show up. So I put breakpoint and saw that the code does come to WinProc() func, but the value of m.WParam.ToInt32() is always &H07. As a result the msgbox() is never executed. Following are soem extra declarations relevant to this in my code:

++++++++++++++++++++++++
<DllImport("User32")> Private Shared Function ShowWindow(ByVal hWnd As Long, ByVal cmd As Integer) As Integer
End Function
<DllImport("User32")> Private Shared Function FindWindowEx(ByVal hwnd As IntPtr, ByVal hwnd2 As Long, ByVal winName1 As String, ByVal winName2 As String) As IntPtr
End Function
<DllImport("User32")> Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal hwnd2 As Long, ByVal winName1 As String, ByVal winName2 As String) As Int32
End Function
++++++++++++++++++++++++++++++++
Thanks.
 
When I add/remove USB devices that code works just fine, I get messagebox for both add/remove. Don't know any more about it.

Those Longs are wrong, they are not 64 bit integers, but 32 bit.
 
Back
Top