星期一, 9月 03, 2012

取得固定硬碟的序號

procedure TForm1.Button1Click(Sender: TObject);
var
  VolumeSerialNumber : DWORD;
  MaximumComponentLength : DWORD;
  FileSystemFlags : DWORD;
  TheSerialNumber : String;
  DR: Char;
  RootPath: PChar;
begin
  for DR := 'C' To 'Z' Do
  begin
    RootPath := PChar(DR + ':\');
    if (GetDriveType(RootPath) = DRIVE_FIXED) Then
    begin
      if GetVolumeInformation(RootPath,nil,0,@VolumeSerialNumber,
         MaximumComponentLength,FileSystemFlags,nil,0) then
      begin
        TheSerialNumber := IntToHex(HiWord(VolumeSerialNumber), 4) +
                            IntToHex(LoWord(VolumeSerialNumber), 4);
      end;
      memo1.Clear;
      memo1.Lines.Add( Format('固定磁碟序號:%s',[TheSerialNumber]) );
    end;
  end;
end;