Ich vermute, dass man die WMI-Abfrage nicht während des Events aufrufen darf.
Setze nur ein Flag und rufe es dann im While-Loop auf:
AutoIt
#Region ;************ Includes ************
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <WinAPIFiles.au3>
#EndRegion ;************ Includes ************
GUICreate("Test", -1, -1, 0, 0)
;Deklarierung MY_WM_DEVICECHANGE (Laufwerk hinzu oder entfernt)
Global Const $DBT_DEVICEARRIVAL = 0x8000 ; A device or piece of media has been inserted and is now available.
Global Const $DBT_DEVICEREMOVECOMPLETE = 0x8004 ; A device or piece of media has been removed.
GUIRegisterMsg($WM_DEVICECHANGE, 'MY_WM_DEVICECHANGE') ;Laufwerk hinzu oder entfernt
Global $new = False
_test()
Func MY_WM_DEVICECHANGE($hWnd, $Msg, $wParam, $lParam)
#forceref $hWnd, $Msg, $lParam
Switch $wParam
Case $DBT_DEVICEARRIVAL ;Laufwerk hinzu
$new = True
Case $DBT_DEVICEREMOVECOMPLETE ;Laufwerk entfernt
$new = True
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>MY_WM_DEVICECHANGE
Func _test()
$new = False
$sDrive = 'g:'
$Ret = _GetDriveInfo($sDrive)
If IsArray($Ret) Then
_ArrayDisplay($Ret, 'Informationen über Laufwerk "' & $sDrive & '"')
Else
MsgBox(0, 'Fehler', $Ret)
EndIf
EndFunc ;==>_test
While 1
Sleep(10)
If $new Then _test()
WEnd
;===============================================================================
; Function Name: _GetDriveInfo($sDrive)
; Description:: Erweiterte Informationen zu einer Festplatte/USB-Stick
; Parameter(s): $sDrive = Laufwerksbuchstabe
; Requirement(s): ---
; Return Value(s): 2D-Array mit Bezeichnung und Wert (siehe Beispiel)
; Author(s): Oscar (http://www.autoit.de)
;===============================================================================
Func _GetDriveInfo($sDrive)
Local Const $aBusType = StringSplit('UNKNOWN,SCSI,ATAPI,ATA,1394,SSA,FIBRE,USB,RAID,ISCSI,SAS,SATA,SD,MMC,Virtual,FileBakVirtual,Spaces,NVMe,SCM,UFS,MAX,MAX Res', ',', $STR_NOCOUNT)
$sDrive = StringRegExpReplace($sDrive, '(?i)(.*)([a-z]{1}:)(.*)', '$2')
Local $sDriveType = DriveGetType($sDrive)
If $sDriveType <> 'Fixed' And $sDriveType <> 'Removable' Then SetError(1, 0, 1)
If Not FileExists($sDrive) Then Return SetError(1, 0, 1)
Local $wbemFlagReturnImmediately = 0x10
Local $wbemFlagForwardOnly = 0x20
Local $colItems = ''
Local $aPartition, $aPhysicalDrive, $aOut[14][2]
$objWMIService = ObjGet('winmgmts:\\localhost\root\CIMV2')
If Not IsObj($objWMIService) Then Return SetError(2, 0, 2)
$colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_LogicalDiskToPartition', 'WQL', $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) Then
For $objItem In $colItems
If StringInStr($objItem.Dependent, '"' & $sDrive & '"') Then
$aPartition = StringRegExp($objItem.Antecedent, 'DeviceID="(.*)"', 3)
EndIf
Next
EndIf
If Not IsArray($aPartition) Then Return SetError(3, 0, 3)
$colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_DiskDriveToDiskPartition', 'WQL', $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) Then
For $objItem In $colItems
If StringInStr($objItem.Dependent, '"' & $aPartition[0] & '"') Then
$aPhysicalDrive = StringRegExp($objItem.Antecedent, 'DeviceID="(.*)"', 3)
EndIf
Next
EndIf
If Not IsArray($aPhysicalDrive) Then Return SetError(4, 0, 4)
Local $iBus = _WinAPI_GetDriveBusType($sDrive)
$aPhysicalDrive[0] = StringReplace($aPhysicalDrive[0], '\\', '\')
$colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_DiskDrive', 'WQL', $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) Then
For $objItem In $colItems
If StringInStr($objItem.DeviceID, $aPhysicalDrive[0]) Then
$aOut[0][0] = 'Volume-Label'
$aOut[0][1] = DriveGetLabel($sDrive)
$aOut[1][0] = 'Serial-Nr. (Volume)'
$aOut[1][1] = DriveGetSerial($sDrive)
$aOut[2][0] = 'Model'
$aOut[2][1] = $objItem.Model
$aOut[3][0] = 'Serial-Nr. (Drive)'
$aOut[3][1] = $objItem.SerialNumber
$aOut[4][0] = 'Interface'
$aOut[4][1] = $aBusType[$iBus]
$aOut[5][0] = 'Size'
$aOut[5][1] = _WinAPI_StrFormatByteSizeEx($objItem.Size)
$aOut[6][0] = 'Media Type'
$aOut[6][1] = StringRegExpReplace($objItem.MediaType, '[^[:print:]]', ' ')
$aOut[7][0] = 'Total Cylinders'
$aOut[7][1] = $objItem.TotalCylinders
$aOut[8][0] = 'Total Heads'
$aOut[8][1] = $objItem.TotalHeads
$aOut[9][0] = 'Total Tracks'
$aOut[9][1] = $objItem.TotalTracks
$aOut[10][0] = 'Total Sectors'
$aOut[10][1] = $objItem.TotalSectors
$aOut[11][0] = 'Tracks Per Cylinder'
$aOut[11][1] = $objItem.TracksPerCylinder
$aOut[12][0] = 'Sectors Per Track'
$aOut[12][1] = $objItem.SectorsPerTrack
$aOut[13][0] = 'Bytes Per Sector'
$aOut[13][1] = $objItem.BytesPerSector
EndIf
Next
EndIf
$objWMIService = ''
Return $aOut
EndFunc ;==>_GetDriveInfo
Alles anzeigen