954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Get Disk Serial Number (A classic)

By Lord Soth on Mar 18th, 2006 4:56 pm

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

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;

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

Please explain.

Arne

AKJo
Junior Poster in Training
59 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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.

123ofOliveTree4
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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.

AKJo
Junior Poster in Training
59 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

The next code works for all HDD Volumes in my Computer:
[code]
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.
finalist
Junior Poster in Training
87 posts since Dec 2009
Reputation Points: 15
Solved Threads: 15
 

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 :-)

hingman
Newbie Poster
2 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You