How can I retrieve the manufacturer serial number of an USB flash drive in Delphi ?
I have tried this:
function GetDiskVolSerialID(ADriveName: Char): Cardinal;
var
DiskDrive: string;
FileSystemFlags: DWORD;
VolumeSerialNumber: DWORD;
MaximumComponentLength: DWORD;
begin
DiskDrive := ADriveName + ':\';
GetVolumeInformation(PChar(DiskDrive),
nil,
0,
@VolumeSerialNumber,
MaximumComponentLength,
FileSystemFlags,
nil,
0);
Result := VolumeSerialNumber;
end;
But it doesn't return correct result!
You can try the component TDiskInfo from GLib to Get the SerialNumber.
It not use WMI, but in some system (disk types) not retrieve the number.
Try it. It's free.
Regards.
opc0de, according to your comments i will give you a sample using the WMI.
First, the code which you posted (using the
GetVolumeInformation
function) return the serial number assigned by windows when you format a disk.The good news are which exist two wmi classes wich exposes a property called
SerialNumber
which storethe Number allocated by the manufacturer to identify the physical media.
these classes areWin32_DiskDrive
andWin32_PhysicalMedia
.Now the bad news, unfortunately this classes is not associated directly with the letter (C,D,E,F...) of the logical disk, because that you must call to another wmi classes to find the link between the logical driver letter and the Physical drive.
so you must find this link previous to obtain the serial number. the sequence to find this association is like this.
Win32_DiskPartition
->Win32_LogicalDiskToPartition
->Win32_DiskDrive
this is the code to obtain the serial number of a usb using the
Win32_DiskDrive
class.By the way some time ago i wrote an application called WMI Delphi Code Creator which can help you to generate delphi code to access the system info using the WMI.
UPDATE
Some drivers of the USB disks does not expose the manufacturer serial number on the Win32_DiskDrive.SerialNumber property, so on this cases you can extract the serial number from the
PnPDeviceID
property.Check this sample code.