• 0

How can i get a message when a USB device connect


Question

I use C# or VB.net

or you will give me a C++ codes also.

I Just want to get a message or a event or some changes i can detect it immediately in my code when a USB Device is connected.

Thanks.

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Taken right from an example on http://www.lvr.com/

   Public Const DBT_DEVICEARRIVAL As Integer = &H8000
    Public Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004
    Public Const DBT_DEVTYP_DEVICEINTERFACE As Integer = 5
    Public Const DBT_DEVTYP_HANDLE As Integer = 6
    Public Const DEVICE_NOTIFY_ALL_INTERFACE_CLASSES As Integer = 4
    Public Const DEVICE_NOTIFY_SERVICE_HANDLE As Integer = 1
    Public Const DEVICE_NOTIFY_WINDOW_HANDLE As Integer = 0
    Public Const WM_DEVICECHANGE As Integer = &H219

    Protected Overrides Sub WndProc(ByRef m As Message)

        'Purpose    : Overrides WndProc to enable checking for and handling
        '           : WM_DEVICECHANGE(messages)

        'Accepts    : m - a Windows Message                   

        Try
            'The OnDeviceChange routine processes WM_DEVICECHANGE messages.
            If m.Msg = WM_DEVICECHANGE Then
                OnDeviceChange(m)
            End If

            'Let the base form process the message.
            MyBase.WndProc(m)

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try

    End Sub

    Friend Sub OnDeviceChange(ByVal m As Message)

        'Purpose    : Called when a WM_DEVICECHANGE message has arrived,
        '           : indicating that a device has been attached or removed.

        'Accepts    : m - a message with information about the device

        Debug.WriteLine("WM_DEVICECHANGE")

        Try
            If (m.WParam.ToInt32 = DBT_DEVICEARRIVAL) Then

                'If WParam contains DBT_DEVICEARRIVAL, a device has been attached.
                Debug.WriteLine("A device has been attached.")

                'Find out if it's the device we're communicating with.
                If MyDeviceManagement.DeviceNameMatch(m, MyDevicePathName) Then
                    Debug.WriteLine("My device attached")
                    lstResults.Items.Add("My device attached.")
                End If

            ElseIf (m.WParam.ToInt32 = DBT_DEVICEREMOVECOMPLETE) Then

                'If WParam contains DBT_DEVICEREMOVAL, a device has been removed.
                Debug.WriteLine("A device has been removed.")

                'Find out if it's the device we're communicating with.
                If MyDeviceManagement.DeviceNameMatch(m, MyDevicePathName) Then

                    Debug.WriteLine("My device removed")
                    lstResults.Items.Add("My device removed.")

                    'Set MyDeviceDetected False so on the next data-transfer attempt,
                    'FindTheHid() will be called to look for the device 
                    'and get a new handle.
                    frmMy.MyDeviceDetected = False
                End If
            End If

            Call ScrollToBottomOfListBox()

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try
    End Sub

Link to comment
Share on other sites

  • 0

Thank you.

I don't know how to put my thankfulness in English.

Thank you very much!!

I am trying myself now.These are very usefull.

Time for bed now in China.

bye-bye

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.