Solved

getting Volume serial number

Posted on 2001-09-04
9
349 Views
Last Modified: 2010-04-06
How do I get the volume serial number from any kind of drive (ex. floppydisk or harddrive)?
0
Comment
Question by:hutelihut
9 Comments
 
LVL 3

Expert Comment

by:Slavak
ID: 6453570
Use GetVolumeInformation function
0
 
LVL 5

Accepted Solution

by:
alanwhincup earned 50 total points
ID: 6453623
Here is an example. To use it just stick a DriveComboBox and a Label on a form and put the below code into the DriveComboBox's OnChange event:

procedure TForm1.DriveComboBox1Change(Sender: TObject);
var
  Drive, Name : array[0..255] of Char;
  SerialNum, CompLength, Flags : DWord;
begin
  StrPCopy(Drive, DriveComboBox1.Drive + ':\');
  if (GetVolumeInformation(Drive, Name, 256, @SerialNum, CompLength, Flags, nil, 256)) then
  begin
    Label1.Caption := IntToHex(SerialNum shr 16, 4) + '-' + IntToHex((SerialNum shl 16) shr 16, 4);
  end
  else
    Label1.Caption := 'Error...';
end;

Cheers,

Alan
0
 
LVL 3

Expert Comment

by:VSF
ID: 6458104
NuttyProfessor Graded me with an A on this link for this above answer
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20170016


Here is complete and easy to use function to get HDD serial number:

function GetVolumeID(DriveChar: Char): String;
var
 MaxFileNameLength, VolFlags, SerNum: DWord;
begin
 if GetVolumeInformation(PChar(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);
begin
edit1.text:=GetVolumeID(extractfilepath(application.exename)[1]);    //This will return the drive where

the program is runnig into (Note: it may not be the C drive!)
end;


This should do it!
VSF
www.victory.hpg.com.br 
0
 

Author Comment

by:hutelihut
ID: 6479091
Is'nt it true, that GetVolumeInformation only works on harddrives???
(im not sure..)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:hutelihut
ID: 6479095
(ohh bad gramma, I think - hope you will understand)
0
 
LVL 3

Expert Comment

by:VSF
ID: 6479221
No!
It's not true!
I just tested it on my machine using Delphi 5 and Windows 98 and it worked perfectly for my floppy disk!

Take a better look at the code!
Trust me.... it works!
VSF
www.victory.hpg.com.br


function GetVolumeID(DriveChar: Char): String;
var
MaxFileNameLength, VolFlags, SerNum: DWord;
begin
if GetVolumeInformation(PChar(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);
begin
button1.caption:=GetVolumeID(edit1.text[1]); //Type A in the edit1 and it will work for sure!
end;
0
 

Author Comment

by:hutelihut
ID: 6482460
Okey, who should now have the points?
0
 
LVL 3

Expert Comment

by:VSF
ID: 6482822
It's up to you to decide which solution fits better!

VSF
www.victory.hpg.com.br
0
 

Author Comment

by:hutelihut
ID: 6489980
Alanwhincup was the first one who mentioned GetVolumeInformation
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now