Please i want the code of the function which could get the serial number of the Hard-disk of any computer

Please Can anyone help ?

Dani AI

Generated

's Delphi snippet is a useful starting point, but it returns the filesystem's volume serial (the ID created when a volume is formatted). That is different from the drive's manufacturer serial number. Which one you need changes the method you should use and the reliability of the result.

If a Windows-level approach is acceptable, try WMI first. Common WMI queries that are useful (run from PowerShell or any language that can query WMI/CIM) are:

SELECT VolumeSerialNumber FROM Win32_LogicalDisk WHERE DeviceID='C:'
SELECT SerialNumber FROM Win32_PhysicalMedia
SELECT DeviceID, PNPDeviceID, SerialNumber FROM Win32_DiskDrive

In PowerShell you can also run:

Get-CimInstance Win32_PhysicalMedia | Select-Object Tag,SerialNumber
Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object VolumeSerialNumber

Win32_PhysicalMedia and Win32_DiskDrive are documented by Microsoft and are the usual first choices for a hardware serial (, Win32_DiskDrive).

If WMI returns nothing or the value is blank (common on USB enclosures, some RAID controllers, or virtual machines), the next step is a lower-level query via DeviceIoControl (IOCTL_STORAGE_QUERY_PROPERTY) to read the STORAGE_DEVICE_DESCRIPTOR, or sending an ATA IDENTIFY command / SCSI inquiry for the physical device. The STORAGE_DEVICE_DESCRIPTOR API is documented here: STORAGE_DEVICE_DESCRIPTOR.

Caveats and troubleshooting notes: administrator rights are sometimes required; virtual/USB/RAID devices may hide or change serials; serial strings frequently contain padding or nonprintable characters; using any serial for licensing is brittle (users can reformat, replace hardware, or spoof values). Recommended workflow: decide if you need volume or hardware serial, try WMI first, fall back to DeviceIoControl/ATA pass-through if necessary, and if reliability matters, combine multiple identifiers or use a generated machine ID instead.

Here is a little Delphi code that gives you just a hint:

// get the hard drive serial number (Delphi Pascal)
procedure TForm1.SerialNumberButtonClick(Sender: TObject);
var
  VolumeSerialNumber : DWORD;
  MaximumComponentLength : DWORD;
  FileSystemFlags : DWORD;
  SerialNumber : string;
begin
  GetVolumeInformation('c:\',
                       nil,
                       0,
                       @VolumeSerialNumber,
                       MaximumComponentLength,
                       FileSystemFlags,
                       nil,
                       0);
  SerialNumber := IntToHex(HiWord(VolumeSerialNumber), 4)
                  + '-' +
                  IntToHex(LoWord(VolumeSerialNumber), 4);
  Memo1.Lines.Add(SerialNumber);
end;

It uses the WinApi function:

BOOL GetVolumeInformation(
    LPCTSTR lpRootPathName,	// address of root directory of the file system 
    LPTSTR lpVolumeNameBuffer,	// address of name of the volume 
    DWORD nVolumeNameSize,	// length of lpVolumeNameBuffer 
    LPDWORD lpVolumeSerialNumber,	// address of volume serial number 
    LPDWORD lpMaximumComponentLength,	// address of system's maximum filename length
    LPDWORD lpFileSystemFlags,	// address of file system flags 
    LPTSTR lpFileSystemNameBuffer,	// address of name of file system 
    DWORD nFileSystemNameSize 	// length of lpFileSystemNameBuffer 
);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.