Jump to content

Counts physical disks. Without letter and WMI


TestPc
 Share

Recommended Posts

;Count PHYSICAL Disk
;Physical disks, not drive letters
Local $Cnt = 0, $iDrvNo = 0, $sPdisk = '\\.\PHYSICALDRIVE0'
While 1
    Local $sPdisk = '\\.\PHYSICALDRIVE' & $Cnt
    Local $iDrvNo = __WinAPI_GetDriveNumber($sPdisk)
    If $iDrvNo < $Cnt Then ExitLoop
    ConsoleWrite('Disk No: ' & $iDrvNo & @CRLF)
    $Cnt = $Cnt + 1
WEnd

ConsoleWrite('PHYSICAL Disk Total: ' & $Cnt & @CRLF)

Func __WinAPI_GetDriveNumber($sDrive)
    Local $hFile = ___CreateFile_OPEN_EXISTING($sDrive)
    If @error Then Return SetError(@error + 20, @extended, $hFile)

    Local $tSDN = DllStructCreate('dword;ulong;ulong')
    Local $aRet = DllCall('kernel32.dll', 'bool', 'DeviceIoControl', 'handle', $hFile, 'dword', 0x002D1080, 'ptr', 0, _
            'dword', 0, 'struct*', $tSDN, 'dword', DllStructGetSize($tSDN), 'dword*', 0, 'ptr', 0)
    If __CheckErrorCloseHandle($aRet, $hFile) Then Return SetError(@error, @extended, 0)

    Local $sResult = DllStructGetData($tSDN, 2)
    Return $sResult
EndFunc   ;==>__WinAPI_GetDriveNumber

Func ___CreateFile_OPEN_EXISTING($sDrive)
    Local $hFile = DllCall("kernel32.dll", "handle", "CreateFileW", "wstr", $sDrive, "dword", 0, "dword", 0, "ptr", 0, _
            "dword", 3, _ ; OPEN_EXISTING
            "dword", 0, "ptr", 0)
    If @error Or $hFile[0] = Ptr(-1) Then Return SetError(@error, @extended, 0) ; INVALID_HANDLE_VALUE
    Return $hFile[0]
EndFunc

Func __CheckErrorCloseHandle($aCall, $hFile, $bLastError = False, $iCurErr = @error, $iCurExt = @extended)
    If Not $iCurErr And Not $aCall[0] Then $iCurErr = 10
    Local $aLastError = DllCall("kernel32.dll", "dword", "GetLastError") ; _WinAPI_GetLastError()
    DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $hFile)
    If $iCurErr Then DllCall("kernel32.dll", "none", "SetLastError", "dword", $aLastError[0]) ; _WinAPI_SetLastError($iLastError)
    If $bLastError Then $iCurExt = $aLastError[0]
    Return SetError($iCurErr, $iCurExt, $iCurErr)
EndFunc   ;==>__CheckErrorCloseHandle

Counts physical disks. Without letter and WMI

Edited by TestPc
added codebox
Link to comment
Share on other sites

Or :

Local $objWMIService = ObjGet("winmgmts:\\.\root\CIMV2")
Local $colItems = $objWMIService.ExecQuery('SELECT Caption FROM Win32_DiskDrive')
If Not IsObj($colItems) Then Exit MsgBox(0, "", "Not an object")
ConsoleWrite ("Count = " & $colItems.count & @CRLF)
For $item in $colItems
  ConsoleWrite ("Caption = " & $item.caption & @CRLF)
Next

 

Link to comment
Share on other sites

5 hours ago, TestPc said:

Counts physical disks, not drive letters.

No, it only provides errors ;):

(9,21) : error: ListPart(): undefined function.
(18,52) : error: _WinAPI_CreateFile(): undefined function.
==> #include <WinAPI.au3> is missing
(24,45) : error: __CheckErrorCloseHandle(): undefined function.

It seems, that you copied your code from a larger script and thereby missed important parts.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

People who are interested in this topic might find the following thread helpful :

get-disk-number-from-drive-letter (from @PsaltyDS)

$oWMISvc = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
$colDiskDrives = $oWMISvc.ExecQuery("SELECT * FROM Win32_DiskDrive")
For $oDiskDrive In $colDiskDrives
    ConsoleWrite("DiskDrive = " & $oDiskDrive.DeviceId & "  Caption = " & $oDiskDrive.Caption & @LF)

    $sQuery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $oDiskDrive.DeviceId & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
    $colPartitions = $oWMISvc.ExecQuery($sQuery)

    For $oPartition In $colPartitions
        ConsoleWrite(@TAB & "Partition = " & $oPartition.DeviceId & @LF)

        $sQuery = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $oPartition.DeviceId & "'} WHERE AssocClass = Win32_LogicalDiskToPartition"
        $colLogicalDisks = $oWMISvc.ExecQuery($sQuery)

        For $oLogicalDisk In $colLogicalDisks
            ConsoleWrite(@TAB & @TAB & "LogicalDisk = " & $oLogicalDisk.DeviceId & @LF)
        Next
    Next
Next

Also have a look at the subsequent contributions from @trancexx .

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

; by AutoIt native function
MsgBox(0,'Num of Drives', DriveGetDrive('ALL')[0])

; by FileSystemObject
; https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/filesystemobject-object
MsgBox(0,'Num of Drives',ObjCreate("Scripting.FileSystemObject").Drives.Count)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

12 hours ago, Musashi said:

No, it only provides errors ;):

(9,21) : error: ListPart(): undefined function.
(18,52) : error: _WinAPI_CreateFile(): undefined function.
==> #include <WinAPI.au3> is missing
(24,45) : error: __CheckErrorCloseHandle(): undefined function.

It seems, that you copied your code from a larger script and thereby missed important parts.

ok. Modification complete.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...