- Offizieller Beitrag
Damit das eigene Script mitbekommt, wenn sich an der Anzahl der Laufwerke etwas ändert, kann man GUIRegisterMsg mit $WM_DEVICECHANGE verwenden.
Ich habe jetzt rausgefunden, dass man nicht nur rausbekommt welcher Laufwerksbuchstabe sich geändert hat, man bekommt auch raus, wenn ein Medium in ein Laufwerk gelegt wird bzw. daraus entfernt wird (z.B. bei CD-ROM Laufwerken).
Das hat mir bei der neuen Version des FileCommander sehr geholfen. Vielleicht könnt ihr das ja auch gebrauchen. Ich habe mal ein kleines Beispielscript erstellt:
AutoIt
#include <Date.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StringConstants.au3>
#include <WindowsConstants.au3>
Global Const $tagDEV_BROADCAST_HDR = 'dword dbch_size; dword dbch_devicetype; dword dbch_reserved'
Global Const $tagDEV_BROADCAST_VOLUME = 'dword dbcv_size; dword dbcv_devicetype; dword dbcv_reserved; dword dbcv_unitmask; dword dbcv_flags'
Global Const $DBT_DEVICEARRIVAL = 0x8000
Global Const $DBT_DEVICEREMOVECOMPLETE = 0x8004
Global Const $DBT_DEVTYP_VOLUME = 0x00000002
Global Const $DBTF_MEDIA = 0x0001
Global Const $DBTF_NET = 0x0002
Global $hGui = GUICreate('Device change example', 640, 400)
Global $idListview = GUICtrlCreateListView('Time|Device|Event', 5, 5, 630, 390)
GUICtrlSetFont(-1, 11, 400, 0, 'Verdana')
GUICtrlSendMsg($idListview, $LVM_SETCOLUMNWIDTH, 0, 100)
GUICtrlSendMsg($idListview, $LVM_SETCOLUMNWIDTH, 1, 70)
GUICtrlSendMsg($idListview, $LVM_SETCOLUMNWIDTH, 2, 400)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_DEVICECHANGE, '_WM_DEVICECHANGE')
While True
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
Func _WM_DEVICECHANGE($hWnd, $Msg, $wParam, $lParam)
; Remarks:
; Although the dbcv_unitmask member may specify more than one volume in any message,
; this does not guarantee that only one message is generated for a specified event.
; Multiple system features may independently generate messages for logical volumes at the same time.
;
; Messages for media arrival and removal are sent only for media in devices that support a soft-eject mechanism.
; For example, applications will not see media-related volume messages for floppy disks.
;
; Messages for network drive arrival and removal are not sent whenever network commands are issued,
; but rather when network connections will disappear as the result of a hardware event.
#forceref $hWnd, $Msg
Local $tDEV_BROADCAST_HDR, $tDEV_BROADCAST_HANDLE, $tDEV_BROADCAST_VOLUME
Local $iBitmask, $iFlags, $aDrives
Switch $wParam
Case $DBT_DEVICEARRIVAL
$tDEV_BROADCAST_HDR = DllStructCreate($tagDEV_BROADCAST_HDR, $lParam)
Switch DllStructGetData($tDEV_BROADCAST_HDR, 'dbch_devicetype')
Case $DBT_DEVTYP_VOLUME
$tDEV_BROADCAST_VOLUME = DllStructCreate($tagDEV_BROADCAST_VOLUME, $lParam)
$iBitmask = DllStructGetData($tDEV_BROADCAST_VOLUME, 'dbcv_unitmask')
$iFlags = DllStructGetData($tDEV_BROADCAST_VOLUME, 'dbcv_flags')
$aDrives = _BitmaskToDriveletterArray($iBitmask)
If Not @error Then
For $sDrive In $aDrives
Switch $iFlags
Case $DBTF_MEDIA ; Change affects media in drive.
GUICtrlCreateListViewItem(StringFormat('%s|%s|A media has been inserted', _NowTime(), $sDrive), $idListview)
Case $DBTF_NET ; Indicated logical volume is a network volume.
Case Else ; Change affects physical device or drive.
GUICtrlCreateListViewItem(StringFormat('%s|%s|A device has been inserted', _NowTime(), $sDrive), $idListview)
EndSwitch
Next
EndIf
EndSwitch
Case $DBT_DEVICEREMOVECOMPLETE
$tDEV_BROADCAST_HDR = DllStructCreate($tagDEV_BROADCAST_HDR, $lParam)
Switch DllStructGetData($tDEV_BROADCAST_HDR, 'dbch_devicetype')
Case $DBT_DEVTYP_VOLUME
$tDEV_BROADCAST_VOLUME = DllStructCreate($tagDEV_BROADCAST_VOLUME, $lParam)
$iBitmask = DllStructGetData($tDEV_BROADCAST_VOLUME, 'dbcv_unitmask')
$iFlags = DllStructGetData($tDEV_BROADCAST_VOLUME, 'dbcv_flags')
$aDrives = _BitmaskToDriveletterArray($iBitmask)
If Not @error Then
For $sDrive In $aDrives
Switch $iFlags
Case $DBTF_MEDIA ; Change affects media in drive.
GUICtrlCreateListViewItem(StringFormat('%s|%s|A media has been removed', _NowTime(), $sDrive), $idListview)
Case $DBTF_NET ; Indicated logical volume is a network volume.
Case Else ; Change affects physical device or drive.
GUICtrlCreateListViewItem(StringFormat('%s|%s|A device has been removed', _NowTime(), $sDrive), $idListview)
EndSwitch
Next
EndIf
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_DEVICECHANGE
Func _BitmaskToDriveletterArray(ByRef $iBitmask)
Local $sDriveLetter = ''
If $iBitmask = 0 Then Return SetError(1, 0, '')
For $i = 0 To 25
If BitAND($iBitmask, 2 ^ $i) Then $sDriveLetter &= Chr(65 + $i) & ':,'
Next
Return StringSplit(StringTrimRight($sDriveLetter, 1), ',', $STR_NOCOUNT)
EndFunc ;==>_BitmaskToDriveletterArray
Alles anzeigen