Dear Sir,

Please help me on Delphi. Could you tell me how can I get disk serial?

type 
MIDPtr = ^MIDRec; 
MIDRec = Record 
InfoLevel: word; 
SerialNum: LongInt; 
VolLabel: Packed Array [0..10] of Char; 
FileSysType: Packed Array [0..7] of Char; 
end; 

function GetDriveSerialNum(MID: MIDPtr; drive: Word): Boolean; assembler; 
asm 
push DS { Just for safety, I dont think its really needed } 
mov ax,440Dh { Function Get Media ID } 
mov bx,drive { drive no (0-Default, 1-A ...) } 
mov cx,0866h { category and minor code } 
lds dx,MID { Load pointeraddr. } 
call DOS3Call { Supposed to be faster than INT 21H } 
jc @@err 
mov al,1 { No carry so return TRUE } 
jmp @@ok 
@@err: 
mov al,0 { Carry set so return FALSE } 
@@ok: 
pop DS { Restore DS, were not supposed to change it } 
end; 

procedure TForm1.NrBtnClick(Sender: TObject); 
var 
Info: MIDRec; 
begin 
Info.InfoLevel:=0; { Information Level } 
If GetDriveSerialNum(@Info,0) then { Do something with it... } 
ListBox.Items.Add(IntToStr(Info.SerialNum)+' '+Info.VolLabel); 
end; 
//////////////////////////////////////////////////// 
BOOL GetVolumeInformation( 

LPCTSTR lpRootPathName, // address of root directory of the file system 
LPTSTR lpVolumeNameBuffer, // address of name of the volume 
DWORD nVolumeNameSize, // length of lpVolumeNameBuffer 
LPDWORD lpVolumeSerialNumber, // address of volume serial number 
LPDWORD lpMaximumComponentLength, // address of system's maximum filename length 
LPDWORD lpFileSystemFlags, // address of file system flags 
LPTSTR lpFileSystemNameBuffer, // address of name of file system 
DWORD nFileSystemNameSize // length of lpFileSystemNameBuffer 
);

Easiest way I do it:

var
VolumeSerialNumber : DWORD;
MaximumComponentLength : DWORD;
FileSystemFlags : DWORD;
TheSerialNumber : String;
begin
if GetVolumeInformation('C:\',nil,0,@VolumeSerialNumber,
MaximumComponentLength,FileSystemFlags,nil,0)
then
begin
TheSerialNumber := IntToHex(HiWord(VolumeSerialNumber), 4) +
IntToHex(LoWord(VolumeSerialNumber), 4);
end;
ShowMessage('The drive serial number is: '+TheSerialNumber);

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.