====== Identifier de manière unique un disque dur ====== Pour l'instant, sur cette page, ce ne sont que des pistes ===== Préambule ===== Le but est d'identifié de manière unique un disque dur physique, quelque soit son contenu ou formatage et quelque soit la machine sur laquelle il est connecté. J'entends par disque dur, un DiskDrive, un disque SATA, SSD une clé USB.\\ Tout ce qui peut etre accessible via un ''\\PHYSICALDRIVEx\'' ===== Approche WMI ===== En WMI il y a les classes ''Win32_DiskDrive'', ''MSFT_PhysicalDisk'' et ''MSFT_Disk'' (et peut etre d'autres) qui décrivent les disks ==== Win32_DiskDrive ==== Les propriétées interessantes sont : * ''Caption'' et ''Model'' = ST2000DM001-1ER164 (et commun avec d'autres modèles) * ''SerialNumber'' = W4Z3A36Q ( Certains disque du meme modéle ont le méme SerialNumber, j'ai vérifié cela sur certaines clé USB ) * ''Signature'' = 1431655765 (Est-ce que la signature du disque est unique tout au long de la vie du disque ???) Classe WMI Win32_DiskDrive { "__PATH" : "\\\\PC-THIERRY\\root\\CIMV2:Win32_DiskDrive.DeviceID=\"\\\\\\\\.\\\\PHYSICALDRIVE0\"", "__NAMESPACE" : "root\\CIMV2", "__SERVER" : "PC-THIERRY", "__DERIVATION" : [ "CIM_DiskDrive" ], "__PROPERTY_COUNT" : "51", "__RELPATH" : "Win32_DiskDrive.DeviceID=\"\\\\\\\\.\\\\PHYSICALDRIVE0\"", "__DYNASTY" : "CIM_ManagedSystemElement", "__SUPERCLASS" : "CIM_DiskDrive", "__CLASS" : "Win32_DiskDrive", "__GENUS" : "2", "BytesPerSector" : "512", "Capabilities" : [ "3" ], "CapabilityDescriptions" : [ "Random Access" ], "Caption" : "ST2000DM001-1ER164", "ConfigManagerErrorCode" : "0", "ConfigManagerUserConfig" : "False", "CreationClassName" : "Win32_DiskDrive", "Description" : "Lecteur de disque", "DeviceID" : "\\\\.\\PHYSICALDRIVE0", "FirmwareRevision" : "CC61", "Index" : "0", "InterfaceType" : "IDE", "Manufacturer" : "(Lecteurs de disque standard)", "MediaLoaded" : "True", "MediaType" : "Fixed hard disk media", "Model" : "ST2000DM001-1ER164", "Name" : "\\\\.\\PHYSICALDRIVE0", "Partitions" : "1", "PNPDeviceID" : "SCSI\\DISK&VEN_ST2000DM&PROD_001-1ER164\\4&61B3D44&0&000000", "SCSIBus" : "0", "SCSILogicalUnit" : "0", "SCSIPort" : "0", "SCSITargetId" : "0", "SectorsPerTrack" : "63", "SerialNumber" : " W4Z3A36Q", "Signature" : "1431655765", "Size" : "2000396321280", "Status" : "OK", "SystemCreationClassName" : "Win32_ComputerSystem", "SystemName" : "PC-THIERRY", "TotalCylinders" : "243201", "TotalHeads" : "255", "TotalSectors" : "3907024065", "TotalTracks" : "62016255", "TracksPerCylinder" : "255" }, ==== MSFT_PhysicalDisk ==== Les propriétées interessantes sont : * "FriendlyName" : "ST2000DM001-1ER164", * "Model" : "ST2000DM001-1ER164", * "SerialNumber" : "W4Z3A36Q", * "Size" : "2000398934016", * "UniqueId" : "5000C5008A988D69", * "UniqueIdFormat" : "3", === UniqueId === Certains disque retournent des UniqueId sous cette forme : "UniqueId" : "USBSTOR\\DISK&VEN_KINGSTON&PROD_DATATRAVELER_3.0&REV_\\60A44C3FAFD5F37129401064&0:PC-THIERRY", "UniqueIdFormat" : "0" On voit donc que certains UniqueId sont lié au PC (''PC-THIERRY''). Est-ce utilisable ??? Classe WMI : MSFT_PhysicalDisk { "__PATH" : "\\\\PC-THIERRY\\root\\Microsoft\\Windows\\Storage:MSFT_PhysicalDisk.ObjectId=\"{1}\\\\\\\\PC-THIERRY\\\\root/Microsoft/Windows/Storage/Providers_v2\\\\SPACES_PhysicalDisk.ObjectId=\\\"{6e6eee87-74ee-11e9-8b27-806e6f6e6963}:PD:{fd43b0d7-925e-cab8-0064-a31028c2c230}\\\"\"", "__NAMESPACE" : "root\\Microsoft\\Windows\\Storage", "__SERVER" : "PC-THIERRY", "__DERIVATION" : [ "MSFT_StorageFaultDomain" ], "__PROPERTY_COUNT" : "39", "__RELPATH" : "MSFT_PhysicalDisk.ObjectId=\"{1}\\\\\\\\PC-THIERRY\\\\root/Microsoft/Windows/Storage/Providers_v2\\\\SPACES_PhysicalDisk.ObjectId=\\\"{6e6eee87-74ee-11e9-8b27-806e6f6e6963}:PD:{fd43b0d7-925e-cab8-0064-a31028c2c230}\\\"\"", "__DYNASTY" : "MSFT_StorageObject", "__SUPERCLASS" : "MSFT_StorageFaultDomain", "__CLASS" : "MSFT_PhysicalDisk", "__GENUS" : "2", "AllocatedSize" : "2000398934016", "BusType" : "11", "CannotPoolReason" : [ "7" ], "CanPool" : "False", "DeviceId" : "0", "FirmwareVersion" : "CC61", "FriendlyName" : "ST2000DM001-1ER164", "HealthStatus" : "0", "IsPartial" : "True", "LogicalSectorSize" : "512", "MediaType" : "3", "Model" : "ST2000DM001-1ER164", "ObjectId" : "{1}\\\\PC-THIERRY\\root/Microsoft/Windows/Storage/Providers_v2\\SPACES_PhysicalDisk.ObjectId=\"{6e6eee87-74ee-11e9-8b27-806e6f6e6963}:PD:{fd43b0d7-925e-cab8-0064-a31028c2c230}\"", "OperationalStatus" : [ "2" ], "PhysicalLocation" : "Integrated : Bus 0 : Device 23 : Function 0 : Adapter 0 : Port 0", "PhysicalSectorSize" : "4096", "SerialNumber" : "W4Z3A36Q", "Size" : "2000398934016", "SpindleSpeed" : "-1", "SupportedUsages" : [ "1" ], "UniqueId" : "5000C5008A988D69", "UniqueIdFormat" : "3", "Usage" : "1", "VirtualDiskFootprint" : "0" }, ==== MSFT_Disk ==== Les propriétés intéressantes sont : * "FriendlyName" : "ST2000DM001-1ER164", * "Model" : "ST2000DM001-1ER164", * "SerialNumber" : " W4Z3A36Q", * "Signature" : "1431655765", * "Size" : "2000398934016", * "UniqueId" : "5000C5008A988D69", * "UniqueIdFormat" : "3" Classe WMI : MSFT_Disk { "__PATH" : "\\\\PC-THIERRY\\root\\Microsoft\\Windows\\Storage:MSFT_Disk.ObjectId=\"{1}\\\\\\\\PC-THIERRY\\\\root/Microsoft/Windows/Storage/Providers_v2\\\\WSP_Disk.ObjectId=\\\"{6e6eee87-74ee-11e9-8b27-806e6f6e6963}:DI:\\\\\\\\?\\\\scsi#disk&ven_st2000dm&prod_001-1er164#4&61b3d44&0&000000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}\\\"\"", "__NAMESPACE" : "root\\Microsoft\\Windows\\Storage", "__SERVER" : "PC-THIERRY", "__DERIVATION" : [ "MSFT_StorageObject" ], "__PROPERTY_COUNT" : "38", "__RELPATH" : "MSFT_Disk.ObjectId=\"{1}\\\\\\\\PC-THIERRY\\\\root/Microsoft/Windows/Storage/Providers_v2\\\\WSP_Disk.ObjectId=\\\"{6e6eee87-74ee-11e9-8b27-806e6f6e6963}:DI:\\\\\\\\?\\\\scsi#disk&ven_st2000dm&prod_001-1er164#4&61b3d44&0&000000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}\\\"\"", "__DYNASTY" : "MSFT_StorageObject", "__SUPERCLASS" : "MSFT_StorageObject", "__CLASS" : "MSFT_Disk", "__GENUS" : "2", "AllocatedSize" : "2000398934016", "BootFromDisk" : "False", "BusType" : "11", "FirmwareVersion" : "CC61", "FriendlyName" : "ST2000DM001-1ER164", "HealthStatus" : "0", "IsBoot" : "False", "IsClustered" : "False", "IsHighlyAvailable" : "False", "IsOffline" : "False", "IsReadOnly" : "False", "IsScaleOut" : "False", "IsSystem" : "False", "LargestFreeExtent" : "0", "Location" : "Integrated : Bus 0 : Device 23 : Function 0 : Adapter 0 : Port 0", "LogicalSectorSize" : "512", "Model" : "ST2000DM001-1ER164", "Number" : "0", "NumberOfPartitions" : "1", "ObjectId" : "{1}\\\\PC-THIERRY\\root/Microsoft/Windows/Storage/Providers_v2\\WSP_Disk.ObjectId=\"{6e6eee87-74ee-11e9-8b27-806e6f6e6963}:DI:\\\\?\\scsi#disk&ven_st2000dm&prod_001-1er164#4&61b3d44&0&000000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}\"", "OfflineReason" : "0", "OperationalStatus" : [ "53264" ], "PartitionStyle" : "1", "Path" : "\\\\?\\scsi#disk&ven_st2000dm&prod_001-1er164#4&61b3d44&0&000000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}", "PhysicalSectorSize" : "4096", "ProvisioningType" : "2", "SerialNumber" : " W4Z3A36Q", "Signature" : "1431655765", "Size" : "2000398934016", "UniqueId" : "5000C5008A988D69", "UniqueIdFormat" : "3" } ===== STORAGE_DEVICE_UNIQUE_IDENTIFIER ===== source : [[https://learn.microsoft.com/fr-fr/windows-hardware/drivers/ddi/storduid/ns-storduid-_storage_device_unique_identifier]] ChatGPT me sort un programme : program project1; {$mode objfpc}{$H+} uses {$IFDEF UNIX} cthreads, {$ENDIF} Classes, crt, SysUtils, Windows; { you can add units after this } const IOCTL_STORAGE_QUERY_PROPERTY = $002D1400; type TStoragePropertyQuery = record PropertyId: DWORD; QueryType: DWORD; AdditionalParameters: array [0..15] of byte; end; TStorageDeviceIDDescriptor = record Version: DWORD; Size: DWORD; Identifiers: array [0..0] of byte; end; var hDevice: THandle; query: TStoragePropertyQuery; descriptor: TStorageDeviceIDDescriptor; bytesReturned: DWORD; ch: char; begin // Ouvrir le périphérique de stockage hDevice := CreateFile(pchar('\\.\PhysicalDrive0'), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0); if hDevice = INVALID_HANDLE_VALUE then begin Writeln('Impossible d''ouvrir le périphérique de stockage.'); Exit; end; try // Préparer la requête pour la propriété STORAGE_DEVICE_UNIQUE_IDENTIFIER query.PropertyId := 0 {StorageDeviceProperty}; query.QueryType := 0 {PropertyStandardQuery}; // Obtenir la propriété STORAGE_DEVICE_UNIQUE_IDENTIFIER if not DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, @query, SizeOf(query), @descriptor, SizeOf(descriptor), bytesReturned, nil) then begin Writeln('Erreur lors de la récupération de la propriété STORAGE_DEVICE_UNIQUE_IDENTIFIER.'); Exit; end; // Afficher l'identifiant unique du périphérique de stockage Writeln('Identifiant unique du périphérique de stockage:'); Writeln(Format('%s', [PChar(@descriptor.Identifiers)])); //Writeln(Format('%.*s', [descriptor.Size, @descriptor.Identifiers])); finally // Fermer le périphérique de stockage CloseHandle(hDevice); end; ch:=ReadKey; end. Mais il y a un problème a l'affichage... j'ai pas le temps de voir pourquoi ! Il faut exécuter le programme en mode admin. ====== Sources & Ressources ====== * [[https://learn.microsoft.com/fr-fr/windows-hardware/drivers/ddi/storduid/ns-storduid-_storage_device_unique_identifier]] * [[https://learn.microsoft.com/fr-fr/windows-hardware/drivers/storage/device-unique-identifiers--duids--for-storage-devices]]