Drydo's Blog

Teenager of the Internet

This blog hosted by:
http://blogs.vbcity.com
  Home :: Syndication  :: Login   Community Forums   :: vbCity.com   :: DevCity.NET  

Just an old code snippet that I've dug out for a new project from an old VB6 project and thought it would be beneficial to post up but I start doing my VooDoo. In short, the following will detect the insertion and detection of USB key devices, CDs and DVDs. It doesn't detect the insertion of specific USB devices (at least not the devices arrayed in front of me) - but feel free to play with the code and the Hdr.devicetype parameter.

Anyhoo, create a new form and paste the following in...

Private Const WM_DEVICECHANGE As Integer = &H219
' Media Insertion parameters
Private
Const DBT_DEVICEARRIVAL As Integer = &H8000
Private
Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004&
Private
Const DBT_DEVTYP_VOLUME As Integer = 2
' Media Insertion structures
<StructLayout(LayoutKind.Sequential)> _
Private Structure DEV_BROADCAST_HDR
Public size As Integer
Public
devicetype As Integer
Public
reserved As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private
Structure DEV_BROADCAST_VOLUME
Public Header As DEV_BROADCAST_HDR
Public
UnitMask As Integer
Public
Flags As Short
End Structure
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
' Capture the appropriate messages
Select
Case m.Msg
Case WM_DEVICECHANGE
Dim Hdr As DEV_BROADCAST_HDR
Dim
Volume As DEV_BROADCAST_VOLUME
' Perform conversion
Try
' Convert into the struct
Hdr = DirectCast( _
Marshal.PtrToStructure(m.LParam, Hdr.GetType), _
DEV_BROADCAST_HDR)
' If its an appropriate device, convert the volume type
If
Hdr.devicetype = DBT_DEVTYP_VOLUME Then
Volume = DirectCast( _
   Marshal.PtrToStructure(m.LParam, Volume.GetType), _
   DEV_BROADCAST_VOLUME)
End If
Catch ex As Exception
Exit Sub
End Try
' Determine the event
Select
Case m.WParam.ToInt32
Case DBT_DEVICEARRIVAL
MessageBox.Show("Inserted and ready on drive: " & GetVolumeLetter(Volume.UnitMask))
Case DBT_DEVICEREMOVECOMPLETE
MessageBox.Show("Removed from drive: " & GetVolumeLetter(Volume.UnitMask))
End Select
End Select
' Call the base routine
MyBase
.WndProc(m)
End Sub
Private Function GetVolumeLetter(ByVal Mask As Integer) As Char
' This function takes the integer value passed through and determine the volume
' type that has been inserted/removed
Dim CharCode As Integer = 65
Do
Until (Mask And 1) = 1
' Used instead of c# syntax of >>=
Mask = CType(Fix(CType(Mask, Double) / 2), Integer)
CharCode += 1
Loop
' Return the char val

Return
Chr(CharCode)
End Function

This code comes without warranty so if it blows up, wipes your machine or becomes the key node in the birth of SkyNet - feel free to post a comment so I can laugh like a drain. Note: it does work but remember to include it within your testing phase...

posted on Wednesday, October 10, 2007 4:28 PM

Feedback

No comments posted yet.

Post Feedback

Title:
Name:
Url:
Comments: 
Protected by Clearscreen.SharpHIPEnter the code you see: