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
Bookmarks