In the last post I was implemented the USB detection, but the problem become more complex, because my USB stick contains some Autorun.inf file and when you plug the stick, it start executing some program, otherwise you need to cancel it explicity. The client need to avoid this. So I have to find some solution, where, I need to disable the autorun, when my program is running. Luckly, on the first search in google only, I got the solution.
Here is the implementation.
'API
Public QueryCancelAutoPlay As Integer = 0
Public Shared Function RegisterWindowMessage(ByVal strMessage As String) As Integer
'No implementation required.
End Function
'Modified implementation of WndProc() method
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
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
lblMessage.Text = "USB Inserted"
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
I implemented it in C#, and I re-wrote it in VB.Net, and I didn’t tested it
If you are facing any issues, let me know.
Hi,
I am very new to this windows application and have absolutely no knowledge about the registry and programming windows application.
In my application it is required to disable autoplay of a particular usb drive and i am not able to do that.Is there any way that i can achieve this?
I also tried using the above code but i get CType is not defined can i get that complete implementation of this particular piece and i am trying put this in C# is there any reference that i have to include ? and what should be there in this WndProc() method??
can you please help me out in this?
Hi shruthi
Yes, you can disable autorun of USB disk, but I didn’t understand the particular USB drive. In the above example I am using VB.Net, because of that CType is undefined. I implemented the same code in C# and it is working fine. There is no managed code available to do this stuff(my understanding), so it is using WIN32 API calls. You don’t need any registry knowledge for this. Any issues let me know I am ready to help you. Sorry for the late reply I was on vacation. Sorry
You can mail me : anuraj dot p at live dot com