Get Disk Serial Number (A classic)

Lord Soth 1 Tallied Votes 1K Views Share

ShowDriveSerialNo shows it with ShowMessage GetDriveSerialNo returns a string. You don't need both.

AKJo commented: It is useful +2
Procedure ShowDriveSerialNo(Drive : String); // Drive as 'c:' or 'd:', ...
var VolSerNum: DWORD;
    Dummy1, Dummy2: DWORD; 
begin 
  if GetVolumeInformation(drive+'\', NIL, 0, @VolSerNum, Dummy1, Dummy2, NIL, 0) then ShowMessage(Format('%.4x:%.4x', [HiWord(VolSerNum), LoWord(VolSerNum)]));
End;

Function GetDriveSerialNo(Drive : String) : String; // Drive as 'x:' ...
var VolSerNum: DWORD;
    Dummy1, Dummy2: DWORD; 
begin 
  if GetVolumeInformation(drive+'\', NIL, 0, @VolSerNum, Dummy1, Dummy2, NIL, 0) then Result := Format('%.4x:%.4x', [HiWord(VolSerNum), LoWord(VolSerNum)]);
End;
AKJo 0 Light Poster

Sounds nice, but I don't understand the difference between
Drive as 'c:'
and
Drive as x:'

Please explain.

Arne

123ofOliveTree4 0 Newbie Poster

Hi AKJo,

And what about you LEARN programming?

Computers are machines that are useful not only for GAMES, MUSIC and VIDEOS, but also for USEFUL TASKS.

AKJo commented: Nothing in this post adds any knowledge +0
AKJo 0 Light Poster

Thank you 123ofOliveTree4 for your good advice. It made me look closer to the code and discovered that the difference in drives were not the importent difference between line 1-6 and 8-13, but the first one describes "Show" and the second part describes "Get". I didn't discover that before, but only saw the different drives.

Thanks again 123ofOliveTree4 for your good advice. I have only tried learning programming since 1968. Obviously I have failed. It would be nice if you are kind to teach me some.

finalist 1 Junior Poster in Training

The next code works for all HDD Volumes in my Computer:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


function GetVolumeID(DriveChar: Char): string;
var
 MaxFileNameLength, VolFlags, SerNum: DWord;
begin
  if GetVolumeInformation(PAnsiChar(DriveChar + ':\'), nil, 0, @SerNum, MaxFileNameLength, VolFlags, nil, 0)
  then
  begin
    Result := IntToHex(SerNum,8);
    Insert('-', Result, 5);
  end
  else
    Result := '';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  C:      Char;
  S:      string;
begin
  Memo1.Lines.Clear;
  for C := 'C' to 'Z' do
  begin
    begin
      S := GetVolumeID(C);
      Memo1.Lines.Add(S);
    end;
  end;
end;

end.

()

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


function GetVolumeID(DriveChar: Char): string;
var
 MaxFileNameLength, VolFlags, SerNum: DWord;
begin
  if GetVolumeInformation(PAnsiChar(DriveChar + ':\'), nil, 0, @SerNum, MaxFileNameLength, VolFlags, nil, 0)
  then
  begin
    Result := IntToHex(SerNum,8);
    Insert('-', Result, 5);
  end
  else
    Result := '';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  C:      Char;
  S:      string;
begin
  for C := 'C' to 'Z' do
  begin
    begin
      S := GetVolumeID(C);
      Memo1.Lines.Add(S);
    end;
  end;
end;

end.
hingman 0 Newbie Poster

I use GetDiskSerial DLL in my product.

This dll file can read the unique serial number of hard disk In your windows application.

Good luck :-)

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.