WM_DEVICECHANGE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Sks=?=

    WM_DEVICECHANGE

    I need to load the contents of a file from a USB flash drive when it
    inserted. Can someone please provide a sample of how this can be achieved?
  • Newbie Coder

    #2
    Re: WM_DEVICECHANGE

    I wrote this code a few weeks ago

    Imports System.Runtime. InteropServices

    #Region "Constants"

    Private Const WM_DEVICECHANGE As Integer = &H219
    Private Const DBT_DEVICEARRIV AL As Integer = &H8000
    Private Const DBT_DEVICEREMOV ECOMPLETE As Integer = &H8004
    Private Const DBT_DEVTYP_VOLU ME As Integer = &H2

    #End Region

    #Region "Structures "

    Private Structure DEV_BROADCAST_H DR
    Public dbch_size As Int32
    Public dbch_devicetype As Int32
    Public dbch_reserved As Int32
    End Structure

    Private Structure DEV_BROADCAST_V OLUME
    Public dbcv_size As Int32
    Public dbcv_devicetype As Int32
    Public dbcv_reserved As Int32
    Public dbcv_unitmask As Int32
    Public dbcv_flags As Int32
    End Structure

    #End Region

    #Region "WndProc (SUB)"

    Protected Overrides Sub WndProc(ByRef m As Message)
    ' Windows MESSAGE
    If m.Msg = WM_DEVICECHANGE Then
    ' Check to see if device was plugged in
    If m.WParam.ToInt3 2() = DBT_DEVICEARRIV AL Then
    Dim hdr As DEV_BROADCAST_H DR =
    CType(Marshal.P trToStructure(m .LParam, GetType(DEV_BRO ADCAST_HDR)),
    DEV_BROADCAST_H DR)
    ' See if the device is a USB Flash Drive etc.
    If hdr.dbch_device type = DBT_DEVTYP_VOLU ME Then
    ' Declare volume as structure
    Dim vol As DEV_BROADCAST_V OLUME =
    CType(Marshal.P trToStructure(m .LParam, GetType(DEV_BRO ADCAST_VOLUME)) ,
    DEV_BROADCAST_V OLUME)
    ' Get volume number
    Dim mask As Int32 = vol.dbcv_unitma sk
    ' Get the drive letter
    Dim chDriveLetter As Char() = GetDriveLetter( mask)
    ' Display the drive letter
    lblDrive.Text = String.Format(" USB Flash Drive Plugged
    Into {0}:", chDriveLetter(0 ).ToString().To Upper())
    End If
    ElseIf m.WParam.ToInt3 2() = DBT_DEVICEREMOV ECOMPLETE Then
    ' Flash Drive Unplugged
    lblDrive.Text = "USB Flash Drive Removed"
    End If
    End If
    MyBase.WndProc( m)
    End Sub

    #End Region

    #Region "Get Drive Letter (FUNCTION)"

    Private Function GetDriveLetter( ByVal iChar As Int32) As Char()
    Dim cnt As Integer = 0
    Dim intTemp As Int32 = iChar
    Dim i As Integer

    For i = 0 To 31
    If (intTemp And 1) = 1 Then
    cnt += 1
    End If
    intTemp = (intTemp >1)
    If intTemp = 0 Then
    Exit For
    End If
    Next

    Dim chResult(0) As Char
    Dim ii As Integer

    For ii = 0 To 31
    If (iChar And 1) = 1 Then
    chResult(0) = Chr(97 + ii)
    End If
    iChar = (iChar >1)
    If iChar = 0 Then
    Exit For
    End If
    Next
    Return chResult
    End Function

    #End Region


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: WM_DEVICECHANGE

      "JK" <JK@discussions .microsoft.coms chrieb:
      >I need to load the contents of a file from a USB flash drive when it
      inserted. Can someone please provide a sample of how this can be achieved?
      <URL:http://www.vb-tips.com/dbpages.aspx?IA =NG>

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • =?Utf-8?B?Sks=?=

        #4
        RE: WM_DEVICECHANGE

        Thank you both!

        "JK" wrote:
        I need to load the contents of a file from a USB flash drive when it
        inserted. Can someone please provide a sample of how this can be achieved?

        Comment

        Working...