Jump to content

How to get the number of the partition?


Recommended Posts

AZJIO,

Does this help?

$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

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

AZJIO,

thread has the following code which seems to be what you are looking for (note the comment by trancexx concerning the difference between what this return and what WMI reports).

Local $aCall = DllCall("kernel32.dll", "dword", "GetLogicalDrives")
Local $iMask = $aCall[0]
Local $tIOCTL_STORAGE_GET_DEVICE_NUMBER = DllStructCreate("dword DeviceType;" & _
"dword DeviceNumber;" & _
"int PartitionNumber")
Local $hDevice
Local $a_hCall, $a_iCall
For $i = 0 To 25; alphabet (A = 0)
If BitAND(BitShift($iMask, $i), 1) Then; drive exists
$a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFile", _
    "str", "[url="file://\."]\\.\[/url]" & Chr(65 + $i) & ":", _; want handle to the logical drive
    "dword", 0, _
    "dword", 0, _
    "ptr", 0, _
    "dword", 3, _; OPEN_EXISTING
    "dword", 128, _; FILE_ATTRIBUTE_NORMAL
    "ptr", 0)
$hDevice = $a_hCall[0]
$a_iCall = DllCall("kernel32.dll", "int", "DeviceIoControl", _
    "hwnd", $hDevice, _
    "dword", 0x2D1080, _; IOCTL_STORAGE_GET_DEVICE_NUMBER
    "ptr", 0, _
    "dword", 0, _
    "ptr", DllStructGetPtr($tIOCTL_STORAGE_GET_DEVICE_NUMBER), _
    "dword", DllStructGetSize($tIOCTL_STORAGE_GET_DEVICE_NUMBER), _
    "dword*", 0, _
    "ptr", 0)
DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hDevice)
ConsoleWrite("Drive " & Chr(65 + $i) & ": " & @CRLF)
ConsoleWrite(@TAB & "DeviceType: " & DllStructGetData($tIOCTL_STORAGE_GET_DEVICE_NUMBER, "DeviceType") & @CRLF)
ConsoleWrite(@TAB & "DeviceNumber: " & DllStructGetData($tIOCTL_STORAGE_GET_DEVICE_NUMBER, "DeviceNumber") & @CRLF)
ConsoleWrite(@TAB & "PartitionNumber: " & DllStructGetData($tIOCTL_STORAGE_GET_DEVICE_NUMBER, "PartitionNumber") & @CRLF)
ConsoleWrite(@CRLF)
EndIf
Next

kylomas

edit: code is by trancexx

edit2: incidentally, trancexx's code (above) agrees with the code that KaFu pointed you to and they are both different from what WMI reports as partition numbers

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

F.W.I.W. - Listview's of various ways of reporting disk properties

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#include <Array.au3>
#include <WinAPIEx.au3>
#include <GUIListView.au3>

#AutoIt3Wrapper_Add_Constants=n

local $gui010   =   guicreate('',300,850)
local $aSize    =   wingetclientsize($gui010)
                    guictrlcreatelabel('Using _WINAPI_GetDriveNumber',10,20,300)
                    guictrlsetfont(-1,9,800,default,'lucinda console')
local $lsv010   =   guictrlcreatelistview('Disk|Partition|Logical Drive|Type',10,40,$aSize[0]-20,200)
                    guictrlcreatelabel('Using DeviceIoControl method',10,270,300)
                    guictrlsetfont(-1,9,800,default,'lucinda console')
local $lsv020   =   guictrlcreatelistview('Disk|Partition|Logical Drive|Type',10,290,$aSize[0]-20,200)
                    guictrlcreatelabel('Using WMI method',10,520,300)
                    guictrlsetfont(-1,9,800,default,'lucinda console')
local $lsv030   =   guictrlcreatelistview('Disk|Partition|Logical Drive|Type',10,540,$aSize[0]-20,200)

;=======================================================================================================================================
;  populate listview using _winapi method
;=======================================================================================================================================

local $aDRV = drivegetdrive('all')

For $1 = 1 to $aDRV[0]
    $aDRVSTAT = _WinAPI_GetDriveNumber($aDRV[$1])
    If Not @error Then
        if $aDRVSTAT[2] > 10  then $aDRVSTAT[2] = 'N/A'
        guictrlcreatelistviewitem( $aDRVSTAT[1] & '|' & $aDRVSTAT[2] & '|' & stringupper($aDRV[$1]) & '|' & $aDRVSTAT[0], $lsv010)
    endif
Next

;=======================================================================================================================================
;  populate listview using DeviceIoControl method
;=======================================================================================================================================

Local $aCall = DllCall("kernel32.dll", "dword", "GetLogicalDrives")

Local $iMask = $aCall[0]

Local $tIOCTL_STORAGE_GET_DEVICE_NUMBER = DllStructCreate("dword DeviceType;" & _
        "dword DeviceNumber;" & _
        "int PartitionNumber")

Local $hDevice
Local $a_hCall, $a_iCall

For $i = 0 To 25; alphabet (A = 0)

    If BitAND(BitShift($iMask, $i), 1) Then; drive exists

        $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFile", _
                "str", "\\.\" & Chr(65 + $i) & ":", _; want handle to the logical drive
                "dword", 0, _
                "dword", 0, _
                "ptr", 0, _
                "dword", 3, _; OPEN_EXISTING
                "dword", 128, _; FILE_ATTRIBUTE_NORMAL
                "ptr", 0)

        $hDevice = $a_hCall[0]

        $a_iCall = DllCall("kernel32.dll", "int", "DeviceIoControl", _
                "hwnd", $hDevice, _
                "dword", 0x2D1080, _; IOCTL_STORAGE_GET_DEVICE_NUMBER
                "ptr", 0, _
                "dword", 0, _
                "ptr", DllStructGetPtr($tIOCTL_STORAGE_GET_DEVICE_NUMBER), _
                "dword", DllStructGetSize($tIOCTL_STORAGE_GET_DEVICE_NUMBER), _
                "dword*", 0, _
                "ptr", 0)

        DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hDevice)

        guictrlcreatelistviewitem(  DllStructGetData($tIOCTL_STORAGE_GET_DEVICE_NUMBER, "DeviceNumber") & '|' & _
                                    DllStructGetData($tIOCTL_STORAGE_GET_DEVICE_NUMBER, "PartitionNumber") & '|' & _
                                    Chr(65 + $i) & '|' & _
                                    DllStructGetData($tIOCTL_STORAGE_GET_DEVICE_NUMBER, "DeviceType"),$lsv020 )

    EndIf

Next

;=======================================================================================================================================
;  populate listview using WMI method
;=======================================================================================================================================

local $drv, $part

$oWMISvc = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
$colDiskDrives = $oWMISvc.ExecQuery("SELECT * FROM Win32_DiskDrive")
For $oDiskDrive In $colDiskDrives
    $drv = $oDiskDrive.DeviceId
    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
        $part = $oPartition.DeviceId
        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
            if GUICtrlCreateListViewItem($drv & '|' & $part & '|' & $oLogicalDisk.DeviceId , $lsv030) = 0 then ConsoleWrite('LV create failed' & @LF)
            ConsoleWrite(@TAB & @TAB & "LogicalDisk = " & $oLogicalDisk.DeviceId & @LF)
        Next
    Next
Next

;=======================================================================================================================================
;  adjust column widths and finish off gui shit
;=======================================================================================================================================

for $1 = 0 to _GUICtrlListView_GetColumnCount($lsv010) + 1
    _GUICtrlListView_SetColumnWidth($lsv010, $1, $aSize[0]/_GUICtrlListView_GetColumnCount($lsv010)-6)
next
for $1 = 0 to _GUICtrlListView_GetColumnCount($lsv020) + 1
    _GUICtrlListView_SetColumnWidth($lsv020, $1, $aSize[0]/_GUICtrlListView_GetColumnCount($lsv020)-6)
next

guisetstate()

while 1
    switch guigetmsg()
        case $gui_event_close
            Exit
    EndSwitch
WEnd

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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...