WMI und USB-Festplatten

  • Hi,

    kann mir einer der Autoit/WMI-Spezies mal erläutern, warum diese WMI-Abfrage (so ähnlich aus Forum) so lange dauert bzw.obs was anderes gibt??
    Pro Festplatte bzw. Aufruf der Func USBPL() dauert es ca. 2-4 Sekunden

    Hintergrund ist der, dass ich prüfen will, welche Festplatte eine USB-Platte ist, das unten aber Notifications "auswertet" und eher ungeeignet ist.

    [autoit]


    Func USBPL($sfpl)

    [/autoit][autoit][/autoit][autoit]

    Local $strComputer = "."
    Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

    [/autoit][autoit][/autoit][autoit]

    $colEvents = $objWMIService.ExecNotificationQuery("Select * From __InstanceOperationEvent Within 5 Where " & "TargetInstance isa 'Win32_LogicalDisk'")

    [/autoit][autoit][/autoit][autoit]

    $objEvent = $colEvents.NextEvent
    If $objEvent.TargetInstance.DriveType = 2 Then
    Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & " is a removable Harddrive." & @CR)
    Return True
    EndIf
    Return False
    EndFunc

    [/autoit]

    Diese Funktion müsste ich mehrmals aufrufen können, was ja aber zu lange dauert.

    Aufrufen tue ich die in einer Schleife:

    [autoit]


    ...
    Switch DriveGetType($aLW[$i])
    Case 'Fixed'
    If USBPL($aDrives[$i]) = True Then
    $iIcon = 4
    Else
    $iIcon= 5
    EndIf
    ...

    [/autoit]

    Ich hab mir auf MSDN den Wolf gesucht..

    Einmal editiert, zuletzt von Torni (15. November 2012 um 10:16)

  • Nimm dazu:

    [autoit]

    DriveGetDrive

    [/autoit]

    Andy hat mir ein Schnitzel gebacken aber da war ein Raupi drauf und bevor Oscar das Bugfixen konnte kam Alina und gab mir ein AspirinJunkie.

    • Offizieller Beitrag

    Vielleicht reicht Dir das:

    Spoiler anzeigen
    [autoit]


    #include <Array.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $aDrives = _CI_GetHarddrives()
    _ArrayDisplay($aDrives)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _CI_GetHarddrives($strComputer = '.')
    Local $aReturn[1][7] = [[ _
    'Bezeichnung:', 'Größe:', 'Anschluss:', 'Bytes/Sektor:', 'Anzahl der Sektoren:', _
    'PNPDeviceID:', 'Status:']]
    Local $x = 0, $objWMIService, $colItems
    $objWMIService = ObjGet('winmgmts:\\' & $strComputer & '\root\cimv2')
    If Not IsObj($objWMIService) Then Return SetError(1, 0, 0)
    $colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_DiskDrive', 'WQL', 0x30)
    If IsObj($colItems) Then
    For $objItem In $colItems
    $x += 1
    ReDim $aReturn[$x + 1][7]
    $aReturn[$x][0] = $objItem.Model
    $aReturn[$x][1] = _ByteAutoSize($objItem.Size)
    $aReturn[$x][2] = $objItem.InterfaceType
    $aReturn[$x][3] = $objItem.BytesPerSector
    $aReturn[$x][4] = $objItem.TotalSectors
    $aReturn[$x][5] = $objItem.PNPDeviceID
    $aReturn[$x][6] = $objItem.Status
    Next
    EndIf
    Return $aReturn
    EndFunc ;==>_CI_GetHarddrives

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Function Name: _ByteAutoSize($iSize[, $iRound][, $iFormat][, $bThousands])
    ; Description:: Gibt einen Bytewert in einer bestimmten Einheit zurück
    ; Parameter(s): $iSize = Größe in Byte übergeben
    ; $iRound = Anzahl der Nachkommastellen (0...8)
    ; $iFormat = bestimmt den Rückgabewert
    ; 0 = Automatisch (je nach übergebenen Wert)
    ; 1 = in Byte
    ; 2 = in KByte
    ; 3 = in MByte
    ; 4 = in GByte
    ; $bThousands = Rückgabe mit Tausendertrennzeichen (True/False)
    ; Requirement(s): #include <String.au3>
    ; Author(s): Oscar (http://www.autoit.de)
    ;===============================================================================
    Func _ByteAutoSize($iSize, $iRound = 2, $iFormat = 0, $bThousands = True)
    Local $aSize[4] = [' Byte', ' KByte', ' MByte', ' GByte'], $sReturn
    If $iFormat < 0 Or $iFormat > 4 Then $iFormat = 0
    If $iRound < 0 Or $iRound > 8 Then $iRound = 2
    If Not IsBool($bThousands) Then $bThousands = False
    $iSize = Abs($iSize)
    If $iFormat = 0 Then
    For $i = 30 To 0 Step -10
    If $iSize > (2 ^ $i) Then
    $iFormat = $i / 10 + 1
    ExitLoop
    EndIf
    Next
    EndIf
    If $iFormat = 0 Then $iFormat = 1
    $sReturn = StringFormat('%.' & $iRound & 'f', Round($iSize / (2 ^ (($iFormat - 1) * 10)), $iRound))
    If $bThousands Then $sReturn = _StringAddThousandsSep($sReturn, '.', ',')
    Return $sReturn & $aSize[$iFormat - 1]
    EndFunc ;==>_ByteAutoSize

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _StringAddThousandsSep
    ; Description ...: Returns the original numbered string with the Thousands delimiter inserted.
    ; Syntax.........: _StringAddThousandsSep($sString[, $sThousands = -1[, $sDecimal = -1]])
    ; Parameters ....: $sString - The string to be converted.
    ; $sThousands - Optional: The Thousands delimiter
    ; $sDecimal - Optional: The decimal delimiter
    ; Return values .: Success - The string with Thousands delimiter added.
    ; Author ........: SmOke_N (orignal _StringAddComma
    ; Modified.......: Valik (complete re-write, new function name)
    ; ===============================================================================================================================
    Func _StringAddThousandsSep($sString, $sThousands = -1, $sDecimal = -1)
    Local $sResult = "" ; Force string
    Local $rKey = "HKCU\Control Panel\International"
    If $sDecimal = -1 Then $sDecimal = RegRead($rKey, "sDecimal")
    If $sThousands = -1 Then $sThousands = RegRead($rKey, "sThousand")
    Local $aNumber = StringRegExp($sString, "(\D?\d+)\D?(\d*)", 1) ; This one works for negatives.
    If UBound($aNumber) = 2 Then
    Local $sLeft = $aNumber[0]
    While StringLen($sLeft)
    $sResult = $sThousands & StringRight($sLeft, 3) & $sResult
    $sLeft = StringTrimRight($sLeft, 3)
    WEnd
    $sResult = StringTrimLeft($sResult, StringLen($sThousands)) ; Strip leading thousands separator
    If $aNumber[1] <> "" Then $sResult &= $sDecimal & $aNumber[1]
    EndIf
    Return $sResult
    EndFunc ;==>_StringAddThousandsSep

    [/autoit]