Sign in to follow this  
Followers 0
dogsn0t

how can i identify if a usb stick has been plugged into my computer?

2 posts in this topic

simple question right? should be easy.. thanks

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You can do it by using WMI... Look at the forum.

#Include <GUIConstantsEx.au3>
#Include <WinAPIEx.au3>
#Include <WindowsConstants.au3>

Global Const $DBT_DEVICEARRIVAL = 0x00008000
Global Const $DBT_DEVICEREMOVECOMPLETE = 0x00008004
Global Const $DBT_DEVTYP_VOLUME = 0x00000002

Global Const $WM_DEVICECHANGE = 0x0219

$Dev = _GetDeviceList()

$hForm = GUICreate('')
GUIRegisterMsg($WM_DEVICECHANGE, 'WM_DEVICECHANGE')

While 1
    Sleep(1000)
WEnd

Func _ArrayDiff(ByRef $ArrayMin, ByRef $ArrayMax)
    For $i = $ArrayMax[0] To 1 Step -1
        For $j = $ArrayMin[0] To 1 Step -1
            If $ArrayMax[$i] = $ArrayMin[$j] Then
                ContinueLoop 2
            EndIf
        Next
        Return $ArrayMax[$i]
    Next
    Return 0
EndFunc   ;==>_ArrayDiff

Func _GetDeviceList()

    Local $List = DriveGetDrive('REMOVABLE')

    If Not IsArray($List) Then
        Dim $List[1] = [0]
    EndIf
    For $i = 1 To $List[0]
        $List[$i] = StringUpper($List[$i])
    Next
    Return $List
EndFunc   ;==>_GetDeviceList

Func WM_DEVICECHANGE($hWnd, $iMsg, $wParam, $lParam)
    Switch $hWnd
        Case $hForm
            Switch $wParam
                Case $DBT_DEVICEARRIVAL, $DBT_DEVICEREMOVECOMPLETE

                    Local $Type, $Letter, $List = _GetDeviceList()

                    Switch $wParam
                        Case $DBT_DEVICEARRIVAL

                            Local $tDEV_BROADCAST_VOLUME = DllStructCreate('int Size;int DeviceType;int Reserved;int Mask;ushort Flags', $lParam)

                            If DllStructGetData($tDEV_BROADCAST_VOLUME, 'DeviceType') = $DBT_DEVTYP_VOLUME Then
                                $Letter = _ArrayDiff($Dev, $List)
                                $Type = _WinAPI_GetDriveBusType($Letter)
                                Switch $Type
                                    Case $DRIVE_BUS_TYPE_USB
                                        ConsoleWrite('USB device (' & $Letter & ') is plug in.' & @CR)
;                                   Case $DRIVE_BUS_TYPE_...

                                EndSwitch
                            EndIf
                    EndSwitch
                    $Dev = $List
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_DEVICECHANGE

WinAPIEx.au3

Edited by Yashied

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0