Jump to content

Get DiskName without DiskPart, WMI, WMIC


meoit
 Share

Recommended Posts

On 07/10/2016 at 2:14 PM, meoit said:

Thanks to MOD.

I do not know to build WinPE. I only need get Disk Name (Friendly Name) in the WinPE environment.

Also, you don't know how to get informations without Diskpart, WMI...
So, take the time to learn how to add the WMI feature to your WinPE, it will be easier and you will learn a useful thing.

 

Link to comment
Share on other sites

For SMF (see my sig) I developed this some time ago (I think with trancexx's help), though still in AutoIt 3.3.8.1 ;).

#include <array.au3>
#include <winapi.au3>

$a = _Drive_GetHardwareInfos("c")
_ArrayDisplay($a)

Func _Drive_GetHardwareInfos($DriveLetter)

    Local $a_Info_Drive_Hardware_temp[4], $ret, $tTemp, $ptrTemp

    $ret = DllCall("kernel32.dll", 'ptr', 'CreateFileW', _
            'wStr', '\\.\' & $DriveLetter & ':', _
            'dword', 0, _
            'dword', 7, _
            'ptr', 0, _
            'dword', 3, _
            'dword', 0, _
            'ptr', 0 _
            )

    If @error Then Return SetError(1, 0, $a_Info_Drive_Hardware_temp)
    If $ret[0] = 0xFFFFFFFF Then Return SetError(2, 0, $a_Info_Drive_Hardware_temp)

    Local $hDevice = $ret[0]

    Local Const $tagSTORAGE_PROPERTY_QUERY = 'ULONG_PTR PropertyId;ULONG_PTR QueryType;byte AdditionalParameters[4]'
    Local Const $tagSTORAGE_DESCRIPTOR_HEADER = 'dword Version;dword Size'

    Local $tSTORAGE_PROPERTY_QUERY = DllStructCreate($tagSTORAGE_PROPERTY_QUERY)
    DllStructSetData($tSTORAGE_PROPERTY_QUERY, 'PropertyId', 0)
    DllStructSetData($tSTORAGE_PROPERTY_QUERY, 'QueryType', 0)

    Local $tSTORAGE_DESCRIPTOR_HEADER = DllStructCreate($tagSTORAGE_DESCRIPTOR_HEADER)

    $ret = DllCall("kernel32.dll", 'int', 'DeviceIoControl', _
            'handle', $hDevice, _
            'dword', 0x002D1400, _
            'ptr', DllStructGetPtr($tSTORAGE_PROPERTY_QUERY), _
            'dword', DllStructGetSize($tSTORAGE_PROPERTY_QUERY), _
            'PTR', DllStructGetPtr($tSTORAGE_DESCRIPTOR_HEADER), _
            'dword', DllStructGetSize($tSTORAGE_DESCRIPTOR_HEADER), _
            'dword*', 0, _
            'ptr', 0 _
            )

    If DllStructGetData($tSTORAGE_DESCRIPTOR_HEADER, "Size") Then

        Local Const $tagSTORAGE_DEVICE_DESCRIPTOR = 'ulong Version;ulong Size;byte DeviceType;byte DeviceTypeModifier;byte RemovableMedia;byte CommandQueueing;ulong VendorIdOffset;ulong ProductIdOffset;ulong ProductRevisionOffset;ulong SerialNumberOffset;ulong BusType;ulong RawPropertiesLength;byte RawDeviceProperties[' & DllStructGetData($tSTORAGE_DESCRIPTOR_HEADER, "Size") & ']'
        Local $tSTORAGE_DEVICE_DESCRIPTOR = DllStructCreate($tagSTORAGE_DEVICE_DESCRIPTOR)

        $ret = DllCall("kernel32.dll", 'int', 'DeviceIoControl', _
                'handle', $hDevice, _
                'dword', 0x002D1400, _
                'PTR', DllStructGetPtr($tSTORAGE_PROPERTY_QUERY), _
                'dword', DllStructGetSize($tSTORAGE_PROPERTY_QUERY), _
                'PTR', DllStructGetPtr($tSTORAGE_DEVICE_DESCRIPTOR), _
                'dword', DllStructGetSize($tSTORAGE_DEVICE_DESCRIPTOR), _
                'dword*', 0, _
                'PTR', 0 _
                )

        If DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "VendorIdOffset") Then

            $ptrTemp = DllStructGetPtr($tSTORAGE_DEVICE_DESCRIPTOR) + DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "VendorIdOffset")

            $tTemp = DllStructCreate("char Temp[" & _WinAPI_StringLenA($ptrTemp) & "]", $ptrTemp)
            $a_Info_Drive_Hardware_temp[0] = DllStructGetData($tTemp, "Temp")

        EndIf

        If DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "ProductIdOffset") Then

            $ptrTemp = DllStructGetPtr($tSTORAGE_DEVICE_DESCRIPTOR) + DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "ProductIdOffset")

            $tTemp = DllStructCreate("char Temp[" & _WinAPI_StringLenA($ptrTemp) & "]", $ptrTemp)
            $a_Info_Drive_Hardware_temp[1] = DllStructGetData($tTemp, "Temp")

        EndIf

        If DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "ProductRevisionOffset") Then

            $ptrTemp = DllStructGetPtr($tSTORAGE_DEVICE_DESCRIPTOR) + DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "ProductRevisionOffset")

            $tTemp = DllStructCreate("char Temp[" & _WinAPI_StringLenA($ptrTemp) & "]", $ptrTemp)
            $a_Info_Drive_Hardware_temp[2] = DllStructGetData($tTemp, "Temp")

        EndIf

        If "0x" & Hex(DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "SerialNumberOffset")) <> "0xFFFFFFFF" Then

            $ptrTemp = DllStructGetPtr($tSTORAGE_DEVICE_DESCRIPTOR) + DllStructGetData($tSTORAGE_DEVICE_DESCRIPTOR, "SerialNumberOffset")

            $tTemp = DllStructCreate("char Temp[" & _WinAPI_StringLenA($ptrTemp) & "]", $ptrTemp)
            $a_Info_Drive_Hardware_temp[3] = DllStructGetData($tTemp, "Temp")

        EndIf

    EndIf

    $a_Info_Drive_Hardware_temp[0] = _StringToReadable($a_Info_Drive_Hardware_temp[0])
    $a_Info_Drive_Hardware_temp[1] = _StringToReadable($a_Info_Drive_Hardware_temp[1])
    $a_Info_Drive_Hardware_temp[2] = _StringToReadable($a_Info_Drive_Hardware_temp[2])
    $a_Info_Drive_Hardware_temp[3] = _SwapStringOrder(_StringToReadable($a_Info_Drive_Hardware_temp[3]))

    DllCall("kernel32.dll", 'int', 'CloseHandle', 'hwnd', $hDevice)
    Return $a_Info_Drive_Hardware_temp
EndFunc   ;==>_Drive_GetHardwareInfos

Func _StringToReadable($sString)
    Local $sString_Return, $iAscCode
    Local $aString = StringSplit($sString, "")
    For $i = 1 To $aString[0]
        $iAscCode = Asc($aString[$i])
        Switch $iAscCode
            ; Case 32 to 126
            Case 48 To 57, 65 To 90, 97 To 122
                $sString_Return &= $aString[$i]
        EndSwitch
    Next
    Return $sString_Return
EndFunc   ;==>_StringToReadable

Func _SwapStringOrder($sString)
    Local $sString_Return, $iAscCode
    ; ConsoleWrite($sString & @TAB & StringLen($sString) & @CRLF)
    Local $aString = StringSplit($sString, "")
    For $i = 1 To $aString[0] Step 2
        If $i + 1 > $aString[0] Then
            $sString_Return &= $aString[$i]
            ExitLoop
        EndIf
        $sString_Return &= $aString[$i + 1] & $aString[$i]
        ; ConsoleWrite($i & @TAB & $aString[0] & @CRLF)
    Next
    Return $sString_Return
EndFunc   ;==>_SwapStringOrder

 

Link to comment
Share on other sites

Hello. I did this.

#include <Array.au3>

Local $aDisk = _GetDiskName()

_ArrayDisplay($aDisk)
Func _GetDiskName()
    Local $iCount = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\disk\Enum", "Count")
    Local $sDiskKey = ""
    Local $sDiskName = ""
    Local $aDisk[$iCount]
    For $i = 1 To $iCount
        $sDiskKey = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\disk\Enum", String($i - 1))
        $sDiskName = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" & $sDiskKey, "FriendlyName")
        $aDisk[$i - 1] = $sDiskName
    Next
    Return $aDisk
EndFunc   ;==>_GetDiskName

Saludos

Edited by Danyfirex
Rename function
Link to comment
Share on other sites

Your code it has the same results as lines of code that I found in articles 15 and 20.

In Mini XP (Hiren'sBootCD), it is not working. :)

How do I upgrade it to make it compatible with Mini XP?.

Edited by meoit
Link to comment
Share on other sites

Hello. Does diskpart work in your MiniXP Os?

Let me know if this works

#include <Array.au3>

Local $aDisk = _GetDiskName()

_ArrayDisplay($aDisk)
Func _GetDiskName()
    Local $iCount = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\disk\Enum", "Count")
    Local $sDiskKey = ""
    Local $sDiskName = ""
    Local $aDisk[$iCount]
    For $i = 1 To $iCount
        $sDiskKey = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\disk\Enum", String($i - 1))
        $sDiskName = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" & $sDiskKey, "FriendlyName")
        If $sDiskName="" Then $sDiskName=RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" & $sDiskKey, "DeviceDesc")
        $aDisk[$i - 1] = $sDiskName
    Next
    Return $aDisk
EndFunc   ;==>_GetDiskName

Saludos

Link to comment
Share on other sites

@Danyfirex . GOOD NEWS :) . It is working fine!. Show full devices in MiniXP with Array.

You are master.

By the way, I want to ask. I have one ComboBox, I want to set the data of Disk name (FriendlyName) and its index.
Examples:
Disk 0 - My Disk0 Name
Disk 1 - My Disk1 Name
Disk 2 - My Disk2 Name
What should I do?.

Thanks you.

Edited by meoit
Link to comment
Share on other sites

Link to comment
Share on other sites

Hi all.

I want to get Disk Letters from ID Disk Number without using DiskPart, WMI, WMIC.

Example:

Disk 0 - C:\, D:\, E:\

Disk 1 - F:\

Disk 2 - G:\, H:\

Disk 0, Disk 1, Disk 2,... may be a Fixed Drive or Hard Drive

--------------------------------------------------------

I found an example about '_WinAPI_GetDriveNumber', it is can get Disk Letters from ID Disk Number. And now I want to remove all Disk Types: "Unknown", "Network", "CDROM", "RAMDisk" of result. I need only: "Removable", "Fixed".

#include <WinAPIFiles.au3>
#include <Array.au3>

Local $aData, $aDrive = DriveGetDrive('ALL')
Local $aList[10]

For $i = 0 To UBound($aDrive) - 1
    $aList[$i] = ''
Next
If IsArray($aDrive) Then
    For $i = 1 To $aDrive[0]
        $aData = _WinAPI_GetDriveNumber($aDrive[$i])
        If IsArray($aData) Then
            ;I want to check Disk types (using DriveGetType()) at here and remove all Disk Types: "Unknown", "Network", "CDROM", "RAMDisk" of the array results. I need only result have: "Removable" and "Fixed".
            $aList[$aData[1]] &= StringUpper($aDrive[$i]) & ' '
        EndIf
    Next
EndIf
For $i = 0 To UBound($aDrive) - 1
    If $aList[$i] Then
        ConsoleWrite('Drive' & $i & ' => ' & $aList[$i] & @CRLF)
    EndIf
Next

--------------------------------------------------------

Someone help me, please.

Thanks.

Edited by meoit
Link to comment
Share on other sites

can you try this one ?

#include <WinAPIFiles.au3>
#include <Array.au3>


Local $aDriveInfo, $iLastDevNumber = -1
Local $aFixed = DriveGetDrive('FIXED'), $aRemovable = DriveGetDrive('REMOVABLE')
Local $aDrives[  (IsArray($aFixed) ? $aFixed[0] : 0) + (IsArray($aRemovable) ? $aRemovable[0] : 0) ][3]

Local $iDrive = 0
For $i = 1 To UBound($aFixed) - 1
    $aDrives[$iDrive][0] = $aFixed[$i]
    $aDriveInfo = _WinAPI_GetDriveNumber($aFixed[$i])
    If Not @error Then
        $aDrives[$iDrive][1] = $aDriveInfo[1]
        $aDrives[$iDrive][2] = $aDriveInfo[2]
    EndIf
    $iDrive += 1
Next
For $i = 1 To UBound($aRemovable) - 1
    $aDrives[$iDrive][0] = $aRemovable[$i]
    $aDriveInfo = _WinAPI_GetDriveNumber($aRemovable[$i])
    If Not @error Then
        $aDrives[$iDrive][1] = $aDriveInfo[1]
        $aDrives[$iDrive][2] = $aDriveInfo[2]
    EndIf
    $iDrive += 1
Next

_ArraySort($aDrives, 0, 0, 0, 1)


Local $aDisks[1][2], $iDisk = 0
For $i = 0 To $iDrive - 1
    If Not IsNumber($aDrives[$i][1]) Then ContinueLoop
    If $aDrives[$i][1] <> $iLastDevNumber Then
        ReDim $aDisks[$iDisk + 1][2]
        $aDisks[$iDisk][0] = "Disk #"& $aDrives[$i][1] & " [" & _GetDiskNameByNumber($aDrives[$i][1]) & "]"
        $aDisks[$iDisk][1] = $aDrives[$i][0] & " (partition #" & $aDrives[$i][2] & ")"
        $iDisk += 1
        $iLastDevNumber = $aDrives[$i][1]
    Else
        $aDisks[$iDisk - 1][1] &= "; " & $aDrives[$i][0] & " (partition #" & $aDrives[$i][2] & ")"
    Endif
Next
_ArrayDisplay($aDisks, "Disks/paritions")



Func _GetDiskNameByNumber($iDiskNumber)
    Local $iCount = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\disk\Enum", "Count")
    Local $sDiskKey = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\disk\Enum", String($iDiskNumber))
    If @error Then Return SetError(1, 0, 0)

    Local $sDiskName = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" & $sDiskKey, "FriendlyName")
    If $sDiskName = "" Then $sDiskName = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" & $sDiskKey, "DeviceDesc")

    Return $sDiskName
EndFunc

 

Link to comment
Share on other sites

Thanks to @jguinch

I tested your code in Mini XP (Hiren'sBootCD). It's not get any Disks or USBs. (In the main my Windows 7 x64, it's working fine).

Test Image:

Mini_XP.jpg

I see the code at #30 of Danyfirex. It is working properly when it comes to get the Name of the Drives. We need to combine the two lines of code of the two friends to get the desired results.

Someone help me. :)

Edited by meoit
Link to comment
Share on other sites

I changed as you ask. Result:

---------------------------
AutoIt Error
---------------------------
Line 30  (File "C:\Users\Admin\Desktop\Disks_TEST.au3"):

_ArrayDisplay($aDisks, "Disks/paritions")
_ArrayDisplay(^ ERROR

Error: Variable used without being declared.
---------------------------
OK   
---------------------------
 

Link to comment
Share on other sites

In this other recent thread there was this code by trancexx working fine for me:

; https://www.autoitscript.com/forum/topic/91598-get-disk-number-from-drive-letter/?do=findComment&comment=659721
; by trancexx

; $sDDriveLetter = "C"

For $i = 97 To 122
    $sDDriveLetter = Chr($i)
    $iDiskNumber = _GetDiskNimberForDrive($sDDriveLetter)
Next

#cs
    If @error Then
    MsgBox(48, "Error", "Error Number " & @error & @CRLF)
    Else
    MsgBox(64, "_GetDiskNimberForDrive", "Drive " & StringUpper($sDDriveLetter) & " is on disk #" & $iDiskNumber)
    EndIf
#ce


Func _GetDiskNimberForDrive($sDriveLetter)

    Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFile", _
            "str", "\\.\" & $sDriveLetter & ":", _; logical drive
            "dword", 0, _
            "dword", 0, _
            "ptr", 0, _
            "dword", 3, _; OPEN_EXISTING
            "dword", 128, _; FILE_ATTRIBUTE_NORMAL
            "ptr", 0)

    If @error Then
        Return SetError(1, 0, -1); your system is very old. Do something.
    EndIf

    If $a_hCall[0] = -1 Then
        Return SetError(2, 0, -1); non-existing drive
    EndIf

    Local $hDevice = $a_hCall[0]

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

    Local $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)

    If @error Or Not $a_hCall[0] Then
        DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hDevice)
        Return SetError(3, 0, -1); DeviceIoControl failed for some reason
    EndIf

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

    ; will write some data
    ConsoleWrite("Drive " & StringUpper($sDriveLetter) & ": " & @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)
    ; end writing

    If DllStructGetData($tIOCTL_STORAGE_GET_DEVICE_NUMBER, "DeviceType") = 7 Then; FILE_DEVICE_DISK
        Return SetError(0, 0, DllStructGetData($tIOCTL_STORAGE_GET_DEVICE_NUMBER, "DeviceNumber"))
    EndIf

    Return SetError(4, 0, -1); not a disk partition

EndFunc   ;==>_GetDiskNimberForDrive
Edited by KaFu
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...