r2du-soft Posted December 4, 2023 Share Posted December 4, 2023 hi I am using wmic to get the serial of the hard drive that windows is installed on But I want to do this through a method other than using wmic Is it possible to do this using DllStructCreate and DllStructGetPtr and DllCall? expandcollapse popup#include <MsgBoxConstants.au3> MsgBox($MB_ICONINFORMATION, "", _Home_DiskDrive_SerialNumber() & @CRLF) Func _Home_DiskDrive_SerialNumber() $Get_DiskDrive_SerialNumber = "" $obj_WMIService = ObjGet("winmgmts:\\" & "localhost" & "\root\cimv2") If Not IsObj($obj_WMIService) Then Return "Error 1" $colDiskDrives = $obj_WMIService.ExecQuery("SELECT * FROM Win32_DiskDrive") If Not IsObj($colDiskDrives) Then Return "Error 2" For $objDrive In $colDiskDrives $strDeviceID = StringReplace($objDrive.DeviceID, "\", "\\") $colPartitions = $obj_WMIService.ExecQuery _ ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _ $strDeviceID & """} WHERE AssocClass = " & _ "Win32_DiskDriveToDiskPartition") For $objPartition In $colPartitions $colLogicalDisks = $obj_WMIService.ExecQuery _ ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _ $objPartition.DeviceID & """} WHERE AssocClass = " & _ "Win32_LogicalDiskToPartition") For $objLogicalDisk In $colLogicalDisks If ($objLogicalDisk.DeviceID = @HomeDrive) Then $Get_DiskDrive_SerialNumber = $objDrive.SerialNumber ExitLoop 3 EndIf Next Next Next IF ($Get_DiskDrive_SerialNumber) Then Return $Get_DiskDrive_SerialNumber Else Return "Error 3" EndIF EndFunc Link to comment Share on other sites More sharing options...
RTFC Posted December 4, 2023 Share Posted December 4, 2023 (edited) WinAPI provides this: #include <WinAPIFiles.au3> Local $aData = _WinAPI_GetVolumeInformation() ; you can parse a root dir (as string, with trailing backslash) here ConsoleWrite('Serial number: ' & $aData[1] & @CRLF) Edited December 4, 2023 by RTFC r2du-soft 1 My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
Andreik Posted December 4, 2023 Share Posted December 4, 2023 What's wrong with native DriveGetSerial()? r2du-soft 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
r2du-soft Posted December 4, 2023 Author Share Posted December 4, 2023 (edited) Thank you friends But the two suggested ways do not display the hardware serial of the hard drive They only display the Volume Serial Number, I know that the volume serial number changes every time when the drive is formatted or partitioned But The presented method displays the serial written on the label on the hard Disk. But I don't want to get it using wmic and I need another way other than this method Edit: It is better to include the name of the hardware in the topic name 🙏 Edited December 4, 2023 by r2du-soft Link to comment Share on other sites More sharing options...
rsn Posted December 4, 2023 Share Posted December 4, 2023 Try this: $WMIObj = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2") If @error Then MsgBox ( 4096 , @ScriptName , "Error getting wmi : " & Hex(@error, 8)) Else $WMIObjItems = $WMIObj.ExecQuery("select * FROM Win32_DiskDrive" ) If IsObj($WMIObjItems) Then For $oItem In $WMIObjItems $sSerialNumber = $oItem.SerialNumber Next EndIf EndIf MsgBox ( 4096 , @ScriptName , $sSerialNumber ) r2du-soft 1 Link to comment Share on other sites More sharing options...
Andreik Posted December 4, 2023 Share Posted December 4, 2023 (edited) @rsn For some reason he doesn't want to use WMI. @r2du-soft You can get the serial using DeviceIoControl to get access to STORAGE_DEVICE_DESCRIPTOR and then use SerialNumberOffset member of the structure to get the serial number. Edited December 4, 2023 by Andreik r2du-soft 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
rsn Posted December 4, 2023 Share Posted December 4, 2023 (edited) @Andreik facepalm (thought he said no wmic) Edited December 4, 2023 by rsn Andreik and r2du-soft 1 1 Link to comment Share on other sites More sharing options...
Andreik Posted December 4, 2023 Share Posted December 4, 2023 Same s*** with and without an interface. r2du-soft 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
argumentum Posted December 4, 2023 Share Posted December 4, 2023 ...still. Say you'll RMA a drive. You'll need the serial number of the device that you can read off the label but the question is: how can I ask the OS for it. Storage Space knows it, so how can I get it too ? ( that is the OP question ) r2du-soft 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Andreik Posted December 4, 2023 Share Posted December 4, 2023 The answer is above, STORAGE_DEVICE_DESCRIPTOR. PS: what is RMA? r2du-soft 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
argumentum Posted December 4, 2023 Share Posted December 4, 2023 "RMA stands for Return Merchandise Authorization" r2du-soft and Andreik 1 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
argumentum Posted December 4, 2023 Share Posted December 4, 2023 52 minutes ago, Andreik said: You can get the serial using DeviceIoControl to get access to STORAGE_DEVICE_DESCRIPTOR and That "you can get" is not so. I can not either. Can you cook some code for us him. I'm clueless busy right now. So very busy r2du-soft 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Andreik Posted December 4, 2023 Share Posted December 4, 2023 1 minute ago, argumentum said: Can you cook some code for us him. I'm clueless busy right now. Mabye later when I get home. argumentum and r2du-soft 1 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Solution Andreik Posted December 4, 2023 Solution Share Posted December 4, 2023 (edited) The promised code: expandcollapse popup#RequireAdmin #include <WinAPIFiles.au3> #include <WinAPIHObj.au3> MsgBox(0, 'Serial', 'Disk serial: ' & GetDiskSerial(@HomeDrive)) Func GetDiskSerial($sDriveLetter) Local Const $IOCTL_STORAGE_QUERY_PROPERTY = 0x2D1400 Local $tagSTORAGE_PROPERTY_QUERY = 'int PropertyId; int QueryType; byte AdditionalParameters[1];' Local $tagSTORAGE_DESCRIPTOR_HEADER = 'dword Version;dword Size;' Local $tagSTORAGE_DEVICE_DESCRIPTOR = 'dword Version;dword Size; byte DeviceType;byte DeviceTypeModifier; boolean RemovableMedia;' & _ 'boolean CommandQueueing;dword VendorIdOffset;dword ProductIdOffset;dword ProductRevisionOffset;dword SerialNumberOffset;int BusType;' & _ 'dword RawPropertiesLength;byte RawDeviceProperties[1];' Local $aDevice = _WinAPI_GetDriveNumber ($sDriveLetter) If @error Then Return SetError(1, @error, Null) Local $hDevice = _WinAPI_CreateFile('\\.\PhysicalDrive' & $aDevice[1], 2, 2, 6) If @error Then Return SetError(2, @error, Null) Local $tSTORAGE_PROPERTY_QUERY = DllStructCreate($tagSTORAGE_PROPERTY_QUERY) $tSTORAGE_PROPERTY_QUERY.PropertyId = 0 ; StorageDeviceProperty $tSTORAGE_PROPERTY_QUERY.QueryType = 0 ; PropertyStandardQuery Local $tSTORAGE_DESCRIPTOR_HEADER = DllStructCreate($tagSTORAGE_DESCRIPTOR_HEADER) If Not _WinAPI_DeviceIoControl($hDevice, $IOCTL_STORAGE_QUERY_PROPERTY, _ DllStructGetPtr($tSTORAGE_PROPERTY_QUERY), DllStructGetSize($tSTORAGE_PROPERTY_QUERY), _ DllStructGetPtr($tSTORAGE_DESCRIPTOR_HEADER), DllStructGetSize($tSTORAGE_DESCRIPTOR_HEADER)) _ Then _WinAPI_CloseHandle($hDevice) Return SetError(3, 0, Null) EndIf Local $tBuffer = DllStructCreate('byte[' & $tSTORAGE_DESCRIPTOR_HEADER.Size & ']') If Not _WinAPI_DeviceIoControl($hDevice, $IOCTL_STORAGE_QUERY_PROPERTY, _ DllStructGetPtr($tSTORAGE_PROPERTY_QUERY), DllStructGetSize($tSTORAGE_PROPERTY_QUERY), _ DllStructGetPtr($tBuffer), DllStructGetSize($tBuffer)) _ Then _WinAPI_CloseHandle($hDevice) Return SetError(4, 0, Null) EndIf Local $tSTORAGE_DEVICE_DESCRIPTOR = DllStructCreate($tagSTORAGE_DEVICE_DESCRIPTOR, DllStructGetPtr($tBuffer)) If $tSTORAGE_DEVICE_DESCRIPTOR.SerialNumberOffset = 0 Then _WinAPI_CloseHandle($hDevice) Return SetError(5, 0, Null) EndIf Local $tSerial = DllStructCreate('char Serial[32]', DllStructGetPtr($tBuffer) + $tSTORAGE_DEVICE_DESCRIPTOR.SerialNumberOffset) _WinAPI_CloseHandle($hDevice) Return $tSerial.Serial EndFunc Edited December 5, 2023 by Andreik r2du-soft and argumentum 1 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
argumentum Posted December 5, 2023 Share Posted December 5, 2023 I was looking for something more like: Hard Disk Model ID: Samsung SSD 980 PRO 2TB Firmware Revision: 5B2QGXA7 Hard Disk Serial Number: S6B0NU0W123456P but I get something like 0025_3957_0240_ED41 Is there a way to get something like that ?, not the firmware revision or drive name but the actual hardware serial number ? r2du-soft 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Andreik Posted December 5, 2023 Share Posted December 5, 2023 How did you get these? WMI? r2du-soft 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
argumentum Posted December 5, 2023 Share Posted December 5, 2023 https://www.hdsentinel.com/ r2du-soft 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
argumentum Posted December 5, 2023 Share Posted December 5, 2023 its supposed to show something like: but maybe because there are NVMe drives shows these xxxx_xxxx_xxxx_xxxx outputs ? r2du-soft 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Andreik Posted December 5, 2023 Share Posted December 5, 2023 Well, there are all kinds of serials and product IDs but the above it's certainly the serial number of the disk. For example if I use ProductIdOffset member to obtain the vendor product ID for my disk I get something like this KINGSTON RBUSNS8180DS3256GJ. There is also a SerialNumber member in STORAGE_ADAPTER_SERIAL_NUMBER structure so pick what serial you need. r2du-soft 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Andreik Posted December 5, 2023 Share Posted December 5, 2023 6 minutes ago, argumentum said: its supposed to show something like: but maybe because there are NVMe drives shows these xxxx_xxxx_xxxx_xxxx outputs ? Looks fine for me. r2du-soft 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now