Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the <a href="http://msdn.microsoft.com/en-us/library/aa394582%28v=vs.85%29.aspx" rel="noreferrer"><code>WMI (Windows Management Instrumentation)</code></a> to get information related to windows hardware. </p> <p>Exist two wmi classes wich exposes a property called <code>SerialNumber</code> which store <code>the Number allocated by the manufacturer to identify the physical media.</code> these classes are <a href="http://www.google.com/search?q=Win32_DiskDrive&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;client=firefox-a&amp;rlz=1R1GGLL_en___CL418" rel="noreferrer"><code>Win32_DiskDrive</code></a> and <a href="http://msdn.microsoft.com/en-us/library/aa394346%28v=vs.85%29.aspx" rel="noreferrer"><code>Win32_PhysicalMedia</code></a>.to access the <code>SerialNumber</code> property of these classes you must know the <code>DeviceId</code> of the Disk which is something like this <code>\\.\PHYSICALDRIVE0</code>. Another way is use a association class which link the Physical drive with the logical drive (C,D,E) </p> <p>so you must find this link previous to obtain the serial number. the sequence to find this association is like this.</p> <p><a href="http://msdn.microsoft.com/en-us/library/aa394135%28v=vs.85%29.aspx" rel="noreferrer"><code>Win32_DiskPartition</code></a> -> <a href="http://msdn.microsoft.com/en-us/library/aa394175%28v=vs.85%29.aspx" rel="noreferrer"><code>Win32_LogicalDiskToPartition</code></a> -> <a href="http://msdn.microsoft.com/en-us/library/aa394132%28v=vs.85%29.aspx" rel="noreferrer"><code>Win32_DiskDrive</code></a></p> <p><strong>Note 1</strong> : The <code>SerialNumber</code> property for the <code>Win32_DiskDrive</code> class does not exist in Windows Server 2003, Windows XP, Windows 2000, and Windows NT 4.0, so how you are talking about use Windows Vista or Windows 7, will work ok for you.</p> <p><strong>Note 2</strong> : The code does not require a administrator account to run.</p> <p>check this code</p> <pre><code>{$APPTYPE CONSOLE} uses SysUtils, ActiveX, ComObj, Variants; function GetDiskSerial(const Drive:AnsiChar):string; var FSWbemLocator : OLEVariant; objWMIService : OLEVariant; colDiskDrives : OLEVariant; colLogicalDisks: OLEVariant; colPartitions : OLEVariant; objDiskDrive : OLEVariant; objPartition : OLEVariant; objLogicalDisk : OLEVariant; oEnumDiskDrive : IEnumvariant; oEnumPartition : IEnumvariant; oEnumLogical : IEnumvariant; iValue : LongWord; DeviceID : string; begin; Result:=''; FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); objWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); //Connect to the WMI colDiskDrives := objWMIService.ExecQuery('SELECT * FROM Win32_DiskDrive','WQL',0); oEnumDiskDrive:= IUnknown(colDiskDrives._NewEnum) as IEnumVariant; while oEnumDiskDrive.Next(1, objDiskDrive, iValue) = 0 do begin DeviceID := StringReplace(objDiskDrive.DeviceID,'\','\\',[rfReplaceAll]); //Escape the `\` chars in the DeviceID value because the '\' is a reserved character in WMI. colPartitions := objWMIService.ExecQuery(Format('ASSOCIATORS OF {Win32_DiskDrive.DeviceID="%s"} WHERE AssocClass = Win32_DiskDriveToDiskPartition',[DeviceID]));//link the Win32_DiskDrive class with the Win32_DiskDriveToDiskPartition class oEnumPartition := IUnknown(colPartitions._NewEnum) as IEnumVariant; while oEnumPartition.Next(1, objPartition, iValue) = 0 do begin colLogicalDisks := objWMIService.ExecQuery('ASSOCIATORS OF {Win32_DiskPartition.DeviceID="'+objPartition.DeviceID+'"} WHERE AssocClass = Win32_LogicalDiskToPartition'); //link the Win32_DiskPartition class with theWin32_LogicalDiskToPartition class. oEnumLogical := IUnknown(colLogicalDisks._NewEnum) as IEnumVariant; while oEnumLogical.Next(1, objLogicalDisk, iValue) = 0 do begin if objLogicalDisk.DeviceID=(Drive+':') then //compare the device id begin Result:=objDiskDrive.SerialNumber; Exit; end; objLogicalDisk:=Unassigned; end; objPartition:=Unassigned; end; end; end; begin try CoInitialize(nil); try Writeln(GetDiskSerial('C')); Readln; finally CoUninitialize; end; except on E:Exception do begin Writeln(E.Classname, ':', E.Message); Readln; end; end; end. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload