Jump to content

DllCall - PARTITION_INFORMATION_EX


JFX
 Share

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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