CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2009
    Posts
    1

    Help- USB & WM_DEVICECHANGE

    Hi

    I'm writing a small application to detect the insertion of USB memory sticks
    and prompt the user to accept the device or reject it. Rejecting it would
    prevent its being loaded by Windows.

    I have the code that detects insertion & removal working fine but have no
    idea how to cancel the insertion.
    I was thinking that I could intercept the DEVICEARRIVAL message and kill it
    off before its actioned by Windows but I am lost with this.
    Has anyone got and ideas on this. I'd really appreciate some sample vb.net
    code to show the way.

    Heres the main part of the code I have so far-

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    'This kills off any 'autoplay' capability in the stick
    If QueryCancelAutoPlay = 0 Then
    QueryCancelAutoPlay = RegisterWindowMessage("QueryCancelAutoPlay")

    End If

    If m.Msg = WM_DEVICECHANGE Then
    Select Case m.WParam
    Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL
    ProcessUSB()
    Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE
    lblMessage.Text = "USB Removed"
    End Select
    ElseIf m.Msg = QueryCancelAutoPlay Then
    m.Result = CType(1, IntPtr)
    Return
    End If
    'Presumably I can insert a different message to cancel here??
    MyBase.WndProc(m)
    End Sub

    Private Sub ProcessUSB()
    For Each ThisDrive As System.IO.DriveInfo In My.Computer.FileSystem.
    Drives
    If ThisDrive.DriveType.ToString = "Removable" Then
    'Prompt user for accept/reject etc
    lblMessage.Text = "USB Inserted"
    End If
    Next
    End Sub

    many thanks
    malcom

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Help- USB & WM_DEVICECHANGE

    Why do you want to cancel the USB device's insertion ¿

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured