From: malcomm on
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 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
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

From: Alex Clark on
Hi Malcomm,

I doubt that intercepting & swalling the DEVICEARRIVAL message would do you
any good, as it's basically a courtesy message broadcast by Windows so that
apps can respond to it. At this stage the device has already been connected
& recognised.

What you could do at this point is react by checking the USB memory stick
and if it violates your business-logic rule, perhaps use a WMI command to
"eject" it. A quick Google for "wmi eject usb" throws up loads of useful
looking results, some of which are C# based and could likely be converted
easily.

HTH,
Alex

"malcomm" <u54785(a)uwe> wrote in message news:9c2280f5224eb(a)uwe...
> 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 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
> 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
>


From: malcomm on
Thanks Alex
I'll delve into that and see what trouble I can get into.

Cheers
malcom