找回密码
 加入
搜索
查看: 2989|回复: 10

[AU3基础] 有没有能够获取磁盘名称以及其他扩展信息的API方法或是思路?

[复制链接]
发表于 2020-3-30 17:34:08 | 显示全部楼层 |阅读模式
Disk  Partitions  Cylinders  Heads  Sectors  Mbytes  Model
  1        3        121601     255      63  953869.8  ST1000DM 003-1SB102 CC63
上面是GDisk的输出结果,请教各位大神如何用纯AU3获得,不使用WMI(某些pe不能用)
发表于 2020-3-30 21:06:38 | 显示全部楼层
读取扇区信息,或者用磁盘相关api,据我所知,有好几种方法。
发表于 2020-3-30 23:19:19 | 显示全部楼层
api DeviceIoControl
 楼主| 发表于 2020-3-31 22:14:21 | 显示全部楼层
afan 发表于 2020-3-30 23:19
api DeviceIoControl

可否给个调用示例,文档看不懂
 楼主| 发表于 2020-3-31 22:14:53 | 显示全部楼层
haijie1223 发表于 2020-3-30 21:06
读取扇区信息,或者用磁盘相关api,据我所知,有好几种方法。

大佬讲讲   
 楼主| 发表于 2020-3-31 22:17:29 | 显示全部楼层
afan 发表于 2020-3-30 23:19
api DeviceIoControl


#include <WinAPI.au3>

$_PARTITION_INFORMATION_MBR = 'byte PartitionType;' & _
                'byte BootIndicator;' & _
                'byte RecognizedPartition;' & _
                'dword HiddenSectors;'

$_PARTITION_INFORMATION_GPT = 'byte TypeGuid[16];' & _
                'byte IDGuid[16];' & _
                'int64 Attributes;' & _
                'wchar Name[36];'

$_PARTITION_INFORMATION_EX = 'int PartitionStyle;' & _
                'int64 StartingOffset;' & _
                'int64 PartitionLength;' & _
                'dword PartitionNumber;' & _
                'byte RewritePartition;' & _
                'byte pad[3];' & _
                $_PARTITION_INFORMATION_MBR & _
                $_PARTITION_INFORMATION_GPT


$_PARTITION_INFORMATION_EX_2 = 'int PartitionStyle;' & _
                'int64 StartingOffset;' & _
                'int64 PartitionLength;' & _
                'dword PartitionNumber;' & _
                'byte RewritePartition;' & _
                'byte pad[3];' & _
                $_PARTITION_INFORMATION_GPT



$DRIVE_LAYOUT_INFORMATION_MBR = 'ULONG Signature;'

$DRIVE_LAYOUT_INFORMATION_GPT = 'byte Guid[16];' & _
                'int64 StartingUsableOffset;' & _
                'int64 UsableLength;' & _
                'ulong MaxPartitionCount;'

$_DRIVE_LAYOUT = DllStructCreate('dword PartitionStyle;' & _
                'dword PartitionCount;' & _
                'byte union[40];' & _
                'byte PartitionEntry[8192]')



$hDrive = _WinAPI_CreateFile('.PHYSICALDRIVE0', 2, 0)
Local $Ret = DllCall("kernel32.dll", "int", "DeviceIoControl", _
                "hwnd", $hDrive, _
                "dword", 0x00070050, _
                "ptr", 0, _
                "dword", 0, _
                "ptr", DllStructGetPtr($_DRIVE_LAYOUT), _
                "dword", DllStructGetSize($_DRIVE_LAYOUT), _
                "dword*", 0, _
                "ptr", 0)



Switch DllStructGetData($_DRIVE_LAYOUT, "PartitionStyle")
        Case 0 ;MBR

                $data = DllStructGetData($_DRIVE_LAYOUT, "union")
                $binaryStruct = DllStructCreate('byte[8192]')
                DllStructSetData($binaryStruct, 1, $data)
                $DriveMBRInfo = DllStructCreate($DRIVE_LAYOUT_INFORMATION_MBR, DllStructGetPtr($binaryStruct))
                ConsoleWrite('MBR Signature: ' & Hex(DllStructGetData($DriveMBRInfo, "Signature"), 8) & @CRLF & @CRLF)


                $PartitionCount = DllStructGetData($_DRIVE_LAYOUT, "PartitionCount")

                $PartitionEntry = 'byte[144];'
                For $x = 2 To $PartitionCount
                        $PartitionEntry &= 'byte[144];'
                Next
                $PartitionEntry &= 'byte[8192]'


                $data = DllStructGetData($_DRIVE_LAYOUT, "PartitionEntry")
                $binaryStruct = DllStructCreate('byte[8192]')
                DllStructSetData($binaryStruct, 1, $data)
                $PartitionEntry = DllStructCreate($PartitionEntry, DllStructGetPtr($binaryStruct))


                For $x = 1 To $PartitionCount
                        $Partion = DllStructCreate($_PARTITION_INFORMATION_EX, DllStructGetPtr($PartitionEntry, $x))
                        If Not DllStructGetData($Partion, 'PartitionNumber') Then ContinueLoop
                        ConsoleWrite('PartitionNumber: ' & DllStructGetData($Partion, 'PartitionNumber') & @CRLF)
                        ConsoleWrite('PartitionStyle: ' & DllStructGetData($Partion, 'PartitionStyle') & @CRLF)
                        ConsoleWrite('StartingOffset: ' & DllStructGetData($Partion, 'StartingOffset') & @CRLF)
                        ConsoleWrite('PartitionLength: ' & DllStructGetData($Partion, 'PartitionLength') & @CRLF)
                        ConsoleWrite('RecognizedPartition: ' & DllStructGetData($Partion, 'RecognizedPartition') & @CRLF)
                        ConsoleWrite('PartitionType: ' & DllStructGetData($Partion, 'PartitionType') & @CRLF)
                        ConsoleWrite('BootIndicator: ' & DllStructGetData($Partion, 'BootIndicator') & @CRLF)
                        ConsoleWrite('HiddenSectors: ' & DllStructGetData($Partion, 'HiddenSectors') & @CRLF & @CRLF)
                Next

        Case 1 ;GPT
                ;GPT stuff here ...

                $data = DllStructGetData($_DRIVE_LAYOUT, "union")
                $binaryStruct = DllStructCreate('byte[8192]')
                DllStructSetData($binaryStruct, 1, $data)
                $DriveGPTInfo = DllStructCreate($DRIVE_LAYOUT_INFORMATION_GPT, DllStructGetPtr($binaryStruct))
                ConsoleWrite('Guid: ' & _BeautyGUID(DllStructGetData($DriveGPTInfo, "Guid")) & @CRLF & @CRLF)
                ConsoleWrite('StartingUsableOffset: ' & Int(DllStructGetData($DriveGPTInfo, "StartingUsableOffset")) & @CRLF)
                ConsoleWrite('UsableLength: ' & Int(DllStructGetData($DriveGPTInfo, "UsableLength")) & @CRLF)
                ConsoleWrite('MaxPartitionCount: ' & Int(DllStructGetData($DriveGPTInfo, "MaxPartitionCount")) & @CRLF & @CRLF)

                $PartitionCount = DllStructGetData($_DRIVE_LAYOUT, "PartitionCount")

                $PartitionEntry = 'byte[144];'
                For $x = 2 To $PartitionCount
                        $PartitionEntry &= 'byte[144];'
                Next
                $PartitionEntry &= 'byte[8192]'

                $data = DllStructGetData($_DRIVE_LAYOUT, "PartitionEntry")
                $binaryStruct = DllStructCreate('byte[8192]')
                DllStructSetData($binaryStruct, 1, $data)
                $PartitionEntry = DllStructCreate($PartitionEntry, DllStructGetPtr($binaryStruct))


                For $x = 1 To $PartitionCount
                        $Partion = DllStructCreate($_PARTITION_INFORMATION_EX_2, DllStructGetPtr($PartitionEntry, $x))
                        If Not DllStructGetData($Partion, 'PartitionNumber') Then ContinueLoop
                        ConsoleWrite('Name: ' & DllStructGetData($Partion, 'Name') & @CRLF)
                        ConsoleWrite('PartitionNumber: ' & DllStructGetData($Partion, 'PartitionNumber') & @CRLF)
                        ConsoleWrite('PartitionStyle: ' & DllStructGetData($Partion, 'PartitionStyle') & @CRLF)
                        ConsoleWrite('StartingOffset: ' & DllStructGetData($Partion, 'StartingOffset') & @CRLF)
                        ConsoleWrite('PartitionLength: ' & DllStructGetData($Partion, 'PartitionLength') & @CRLF)
                        ConsoleWrite('TypeGuid: ' & _BeautyGUID(DllStructGetData($Partion, 'TypeGuid')) & @CRLF)
                        ConsoleWrite('IDGuid: ' & _BeautyGUID(DllStructGetData($Partion, 'IDGuid')) & @CRLF)
                        ConsoleWrite('Attributes: ' & Int(DllStructGetData($Partion, 'Attributes')) & @CRLF & @CRLF)
                Next

        Case 2 ; RAW
                ConsoleWrite('RAW Disk - no information available' & @CRLF)

EndSwitch



Func _BeautyGUID($GUID)
        $GUID = StringLower($GUID)
        If StringLeft($GUID, 2) = '0x' Then $GUID = StringTrimLeft($GUID, 2)
        Return '{' & StringLeft($GUID, 8) & '-' & StringMid($GUID, 9, 4) & '-' & StringMid($GUID, 13, 4) & '-' & StringMid($GUID, 17, 4) & '-' & StringRight($GUID, 8) & '}'
EndFunc   ;==>_BeautyGUID


上面这段代码执行后无效果
发表于 2020-4-5 13:16:36 | 显示全部楼层
;获取硬盘信息(序列号SerialNumber、序号DeviceID、品牌型号Model,其它信息可以搜索Win32_diskdrive获取相关命令)
Local $Win32_diskdrive = Run(@SystemDir & '\WindowsPowerShell\v1.0\powershell.exe Get-WmiObject -Class Win32_diskdrive SerialNumber,DeviceID,Model', '', @SW_HIDE, $STDERR_MERGED)
ProcessWaitClose($Win32_diskdrive, 5)
Local $etxt = StdoutRead($Win32_diskdrive)
MsgBox($MB_SYSTEMMODAL, '', $etxt)
发表于 2020-4-5 13:28:11 | 显示全部楼层
itsky2 发表于 2020-3-31 22:17
#include

$_PARTITION_INFORMATION_MBR = 'byte PartitionType;' & _

.PhysicalDrive0  改为 \\.\PhysicalDrive0
 楼主| 发表于 2020-4-5 17:40:16 | 显示全部楼层

谢谢大佬哈,不用WMI的
 楼主| 发表于 2020-4-5 17:43:20 | 显示全部楼层
afan 发表于 2020-4-5 13:28
.PhysicalDrive0  改为 \\.\PhysicalDrive0

成功运行,非常感谢!
还是不知道磁盘名怎么获取..
发表于 2020-4-5 20:24:42 | 显示全部楼层
https://www.autoitx.com/forum.ph ... ;highlight=%B4%BFAU

P版的代码能帮到你吧,不过在我这儿运行不了?
提示:
"Z:\GetDiskSerialNumber.au3"(4,80) : 错误: $IOCTL_STORAGE_QUERY_PROPERTY 先前声明为 'Const' 常量.
Const $IOCTL_STORAGE_QUERY_PROPERTY = _MakeCtrlCode($IOCTL_STORAGE_BASE, 0x500)
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-3-29 23:06 , Processed in 0.078164 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表