#include-once #include ; #INDEX# ======================================================================================================================= ; Title .........: USBEvent ; AutoIt Version : 3.3.8.1 ; Description ...: Call a function on USB drives insertion/remove ; Author(s) .....: Matwachich ; =============================================================================================================================== ; #VARIABLES# =================================================================================================================== Global $__gUSBEvent_sCBFunc = "" Global $__gUSBEvent_aArray[1][3] = [[0, "", ""]] ; letter, serial, notified ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ;_USBEvent_Set ; =============================================================================================================================== ; #EXAMPLE# ===================================================================================================================== #cs #include Func _Callback($sDrive, $sSerial, $iFlag) Switch $iFlag Case 1 ConsoleWrite("> Inserted: " & $sDrive & "\ [" & DriveGetLabel($sDrive) & "] (N°" & $sSerial & ")" & @CRLF) Case 0 ConsoleWrite("! Removed: " & $sDrive & "\ [" & DriveGetLabel($sDrive) & "] (N°" & $sSerial & ")" & @CRLF) EndSwitch EndFunc _USBEvent_Set("_Callback", 1) Do Sleep(100) Until _IsPressed("1B") #ce ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name ..........: _USBEvent_Set ; Description ...: Setup an USB disk insertion/removal watch. ; Syntax ........: _USBEvent_Set($sCBFunc[, $iDelay = 500]) ; Parameters ....: $sCBFunc - The name of the callback function to call on USB disk event (insert/remove). ; |Pass an empty string to deactivate the USB disks watch. ; $iCallForPresents - [optional] If set to 1, then the insert callback will be called for already inserted ; |disks, otherwise, the callbacks will be called only on new events. Default is 0. ; $iDelay - [optional] Delay (im ms) between USB disks check. Default is 500. ; Return values .: 1 ; Author ........: Matwachich ; Remarks .......: The callback function must accept 3 parameters: ; |$sDrive - Will be the USB disk drive letter ; |$sSerial - Will be the USB disk serial number (DriveGetSerial()) ; |$iFlag - Will be 1 if the disk is inserted, 0 if the disk is removed ; =============================================================================================================================== Func _USBEvent_Set($sCBFunc, $iCallForPresents = 0, $iDelay = 500) If $sCBFunc Then $__gUSBEvent_sCBFunc = $sCBFunc __USBEvent_ArrayReset() If Not $iCallForPresents Then __USBEvent_ArrayAddFirst() AdlibRegister("__USBEvent_CBFunc", $iDelay) Else $__gUSBEvent_sCBFunc = "" AdlibUnRegister("__USBEvent_CBFunc") EndIf Return 1 EndFunc ; #INTERNAL_USE_ONLY# =========================================================================================================== Func __USBEvent_CBFunc() Local $aDrives = DriveGetDrive("REMOVABLE") If Not IsArray($aDrives) Then Return ; --- For $i = 1 To $aDrives[0] __USBEvent_ArrayAdd($aDrives[$i]) Next ; --- __USBEvent_CallInsert() __USBEvent_CallRemove($aDrives) EndFunc ; Ajoute les disques présents de tel manière que le callback ne soit pas appelé Func __USBEvent_ArrayAddFirst() Local $aDrives = DriveGetDrive("REMOVABLE") If Not IsArray($aDrives) Then Return ; --- For $i = 1 To $aDrives[0] __USBEvent_ArrayAdd($aDrives[$i], 1) Next EndFunc ; Appel la fonction callback d'insertion Func __USBEvent_CallInsert() For $i = 1 To $__gUSBEvent_aArray[0][0] If $__gUSBEvent_aArray[$i][2] = 0 Then Call($__gUSBEvent_sCBFunc, $__gUSBEvent_aArray[$i][0], $__gUSBEvent_aArray[$i][1], 1) $__gUSBEvent_aArray[$i][2] = 1 EndIf Next EndFunc ; Appel la fonction de retrait, et retire l'élément du array général Func __USBEvent_CallRemove($aDrives) For $i = $__gUSBEvent_aArray[0][0] To 1 Step -1 If Not __USBEvent_DriveExists($__gUSBEvent_aArray[$i][0], $__gUSBEvent_aArray[$i][1], $aDrives) Then Call($__gUSBEvent_sCBFunc, $__gUSBEvent_aArray[$i][0], $__gUSBEvent_aArray[$i][1], 0) _ArrayDelete($__gUSBEvent_aArray, $i) $__gUSBEvent_aArray[0][0] -= 1 EndIf Next EndFunc Func __USBEvent_ArrayReset() ReDim $__gUSBEvent_aArray[1][3] $__gUSBEvent_aArray[0][0] = 0 EndFunc ; Ajoute un disque dans le array général, s'il n'y existe pas déjà Func __USBEvent_ArrayAdd($sDrive, $iSetCalled = 0) Local $sSerial = DriveGetSerial($sDrive) If @error Then Return ; --- If Not __USBEvent_ArrayExists($sDrive, $sSerial) Then ReDim $__gUSBEvent_aArray[$__gUSBEvent_aArray[0][0] + 2][3] $__gUSBEvent_aArray[0][0] += 1 $__gUSBEvent_aArray[$__gUSBEvent_aArray[0][0]][0] = $sDrive $__gUSBEvent_aArray[$__gUSBEvent_aArray[0][0]][1] = $sSerial $__gUSBEvent_aArray[$__gUSBEvent_aArray[0][0]][2] = $iSetCalled EndIf EndFunc ; Test si un disque existe dans le array général Func __USBEvent_ArrayExists($sDrive, $sSerial) For $i = 1 To $__gUSBEvent_aArray[0][0] If $__gUSBEvent_aArray[$i][0] = $sDrive And _ $__gUSBEvent_aArray[$i][1] = $sSerial Then Return 1 Next Return 0 EndFunc ; Test si un disque est inséré Func __USBEvent_DriveExists($sDrive, $sSerial, $aDrives = Default) If $aDrives = Default Then $aDrives = DriveGetDrive("REMOVABLE") ; --- For $i = 1 To $aDrives[0] If $aDrives[$i] = $sDrive And _ DriveGetSerial($aDrives[$i]) = $sSerial Then Return 1 Next Return 0 EndFunc