- Сообщения
- 5,379
- Репутация
- 2,724
Для запуска понадобится незаменимая библиотека WinAPIEx.au3.
Код:
#Include <WinAPIEx.au3>
Opt('MustDeclareVars', 1)
Opt('TrayAutoPause', 0)
Global Const $DBT_DEVICEARRIVAL = 0x00008000
Global Const $DBT_DEVICEREMOVECOMPLETE = 0x00008004
Global Const $DBT_DEVTYP_VOLUME = 0x00000002
Global Const $WM_DEVICECHANGE = 0x0219
Global $hForm = GUICreate('')
Global $Drives, $Volume = _WinAPI_GetLogicalDrives()
Global $Device = 1
GUIRegisterMsg($WM_DEVICECHANGE, 'WM_DEVICECHANGE')
While 1
Sleep(100)
If $Device Then
$Drives = _GetDriveLetter($Volume)
$Device = 0
$Volume = 0
For $j = 1 To $Drives[0]
_USBCheck($Drives[$j])
Next
EndIf
WEnd
Func _GetDriveLetter($iMask)
Local $Drive[27] = [0]
If $iMask Then
For $i = 0 To 25
If BitAND(BitShift($iMask, $i), 1) Then
$Drive[$Drive[0] + 1] = Chr(65 + $i) & ':'
$Drive[0] += 1
EndIf
Next
EndIf
ReDim $Drive[$Drive[0] + 1]
Return $Drive
EndFunc ;==>_GetDriveLetter
Func _USBCheck($sVolume)
Local $hDrive, $tData, $iBus, $iRes, $Fs
$iBus = _WinAPI_GetDriveBusType($sVolume)
Switch $iBus
Case $DRIVE_BUS_TYPE_USB
$hDrive = _WinAPI_CreateFileEx('\\.\' & $sVolume, 3, 0, 0)
If Not $hDrive Then
Return
EndIf
$tData = DllStructCreate('dword DeviceType;ulong DeviceNumber;ulong PartitionNumber')
$iRes = _WinAPI_DeviceIoControl($hDrive, $IOCTL_STORAGE_GET_DEVICE_NUMBER, 0, 0, DllStructGetPtr($tData), DllStructGetSize($tData))
_WinAPI_CloseHandle($hDrive)
If (Not $iRes) Or (Not (DllStructGetData($tData, 'DeviceType') = 7)) Then
Return
EndIf
;~ $Fs = DriveGetFileSystem($sVolume)
;~ Switch $Fs
;~ Case 'FAT32', 'NTFS'
;~
;~ Case Else
;~ Return
;~ EndSwitch
Case Else
Return
EndSwitch
ConsoleWrite('USB device found: ' & $sVolume & @CR)
EndFunc ;==>_USBCheck
Func WM_DEVICECHANGE($hWnd, $iMsg, $wParam, $lParam)
Switch $hWnd
Case $hForm
Switch $wParam
Case $DBT_DEVICEARRIVAL, $DBT_DEVICEREMOVECOMPLETE
Local $tDBV = DllStructCreate('dword Size;dword DeviceType;dword Reserved;dword Mask;ushort Flags', $lParam)
Local $Type = DllStructGetData($tDBV, 'DeviceType')
Local $Item
Switch $Type
Case $DBT_DEVTYP_VOLUME
If Not DllStructGetData($tDBV, 'Flags') Then
Switch $wParam
Case $DBT_DEVICEARRIVAL
$Volume = BitOR($Volume, DllStructGetData($tDBV, 'Mask'))
$Device = 1
Case Else
EndSwitch
EndIf
Case Else
EndSwitch
EndSwitch
EndSwitch
Return 'GUI_RUNDEFMSG'
EndFunc ;==>WM_DEVICECHANGE