Jump to content
Sign in to follow this  
JFX

DllCall - PARTITION_INFORMATION_EX

Recommended Posts

JFX

Hi! I'm, trying to get the this API PARTITION_INFORMATION_EX running in a dllcall.

Thought I could use it the same way like in this topic Get disk number from drive letter but i guess it doesn't work so

Please can anyone help me with this?

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

Local $iMask = $aCall[0]

Local $tIOCTL_DISK_GET_PARTITION_INFO_EX = DllStructCreate("long_ptr StartingOffset;" & _
        "long_ptr PartitionLength;" & _
        "ulong 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", 0x70048, _; IOCTL_DISK_GET_PARTITION_INFO_EX
                "ptr", 0, _
                "dword", 0, _
                "ulong_ptr", DllStructGetPtr($tIOCTL_DISK_GET_PARTITION_INFO_EX), _
                "dword", DllStructGetSize($tIOCTL_DISK_GET_PARTITION_INFO_EX), _
                "dword*", 0, _
                "ptr", 0)

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

        ConsoleWrite("Drive " & Chr(65 + $i) & ": " & @CRLF)
        ConsoleWrite(@TAB & "StartingOffset:  " & DllStructGetData($tIOCTL_DISK_GET_PARTITION_INFO_EX, "StartingOffset") & @CRLF)
        ConsoleWrite(@TAB & "PartitionLength: " & DllStructGetData($tIOCTL_DISK_GET_PARTITION_INFO_EX, "PartitionLength") & @CRLF)
        ConsoleWrite(@TAB & "PartitionNumber: " & DllStructGetData($tIOCTL_DISK_GET_PARTITION_INFO_EX, "PartitionNumber") & @CRLF)
        ConsoleWrite(@CRLF)
    EndIf
Next

Share this post


Link to post
Share on other sites
PsaltyDS

I ran it this way and suspiciously got the exact same handle for every drive. I don't think it's normal to get the same handle repeatedly like that.

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

Local $iMask = $aCall[0]

Local $tIOCTL_DISK_GET_PARTITION_INFO_EX = DllStructCreate("long_ptr StartingOffset;" & _
        "long_ptr PartitionLength;" & _
        "ulong PartitionNumber;")

Local $hDevice
Local $a_hCall, $a_iCall, $sDevice

For $i = 0 To 25 ; alphabet (A = 0)
    If BitAND(BitShift($iMask, $i), 1) Then ; drive exists
        $sDevice = "\\.\" & Chr(65 + $i) & ":"
        ConsoleWrite($i & ": $sDevice = " & $sDevice & @LF)
        $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFile", _
                "str", $sDevice, _ ; 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]
        ConsoleWrite($i & ": $hDevice = " & $hDevice & @LF)
        If $hDevice = -1 Then ContinueLoop ; Test for INVALID_HANDLE_VALUE

        $a_iCall = DllCall("kernel32.dll", "int", "DeviceIoControl", _
                "hwnd", $hDevice, _
                "dword", 0x70048, _ ; IOCTL_DISK_GET_PARTITION_INFO_EX
                "ptr", 0, _
                "dword", 0, _
                "ulong_ptr", DllStructGetPtr($tIOCTL_DISK_GET_PARTITION_INFO_EX), _
                "dword", DllStructGetSize($tIOCTL_DISK_GET_PARTITION_INFO_EX), _
                "dword*", 0, _
                "ptr", 0)

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

        ConsoleWrite("Drive " & Chr(65 + $i) & ": " & @CRLF)
        ConsoleWrite(@TAB & "StartingOffset:  " & DllStructGetData($tIOCTL_DISK_GET_PARTITION_INFO_EX, "StartingOffset") & @CRLF)
        ConsoleWrite(@TAB & "PartitionLength: " & DllStructGetData($tIOCTL_DISK_GET_PARTITION_INFO_EX, "PartitionLength") & @CRLF)
        ConsoleWrite(@TAB & "PartitionNumber: " & DllStructGetData($tIOCTL_DISK_GET_PARTITION_INFO_EX, "PartitionNumber") & @CRLF)
        ConsoleWrite(@CRLF)
    EndIf
Next

By the way, you should be testing if $hDevice = -1, which is INVALID_HANDLE_VALUE, so you don't continue with an invalid handle.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Authenticity

Local Const $tagGUID1 = "uint Data1;ushort Data2;ushort Data3;ubyte Data4[8];"
Local Const $tagPARTITION_INFORMATION_MBR = "ubyte PartitionType;ubyte BootIndicator;ubyte RecognizedPartition;uint HiddenSectors;"
Local Const $tagPARTITION_INFORMATION_GPT = $tagGUID1 & $tagGUID1 & "uint64 Attributes;wchar Name[36];"

Local Const $tagPARTITION_INFORMATION_EX_GPT = _
  "int PartitionStyle;" & _
  "int64 StartingOffset;" & _
  "int64 PartitionLength;" & _
  "uint PartitionNumber;" & _
  "ubyte RewritePartition;" & _
   $tagPARTITION_INFORMATION_GPT
   
Local Const $tagPARTITION_INFORMATION_EX_MBR = _
  "int PartitionStyle;" & _
  "int64 StartingOffset;" & _
  "int64 PartitionLength;" & _
  "uint PartitionNumber;" & _
  "ubyte RewritePartition;" & _
   $tagPARTITION_INFORMATION_MBR & "dword[26];"
   

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

Local $iMask = $aCall[0]

Local $tPIX = DllStructCreate($tagPARTITION_INFORMATION_EX_GPT)

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", 0x70048, _; IOCTL_DISK_GET_PARTITION_INFO_EX
                "ptr", 0, _
                "dword", 0, _
                "ptr", DllStructGetPtr($tPIX), _
                "dword", DllStructGetSize($tPIX), _
                "dword*", 0, _
                "ptr", 0)

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

        ConsoleWrite("Drive " & Chr(65 + $i) & ": " & @CRLF)
        ConsoleWrite(@TAB & "StartingOffset:  " & DllStructGetData($tPIX, "StartingOffset") & @CRLF)
        ConsoleWrite(@TAB & "PartitionLength: " & DllStructGetData($tPIX, "PartitionLength") & @CRLF)
        ConsoleWrite(@TAB & "PartitionNumber: " & DllStructGetData($tPIX, "PartitionNumber") & @CRLF)
        ConsoleWrite(@CRLF)
    EndIf
Next

Share this post


Link to post
Share on other sites
PsaltyDS

Well, I was obviously wrong (again) about the handle repeating. Adding this to Authenticity's code shows the exact same handle for every drive, yet the handle obviously works:

$hDevice = $a_hCall[0]
        ConsoleWrite($i & ": $hDevice = " & $hDevice & @LF)

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
JFX

@PsaltyDS

Thanks for pointing out INVALID_HANDLE_VALUE, guess I always forgot such a check.

@Authenticity

Works perfect,thanks a lot.

Regards,

JFX

Share this post


Link to post
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
Sign in to follow this  

×