GetVolumeInformation
GetVolumeInformation函数来获取硬盘的序列号
函数声明:
BOOL GetVolumeInformation(
LPCTSTR lpRootPathName, // 与获取信息卷的根路径
LPTSTR lpVolumeNameBuffer, // 用于装在卷名的一个字符串
DWORD nVolumeNameSize, // 字符串的长度
LPDWORD lpVolumeSerialNumber, // 用于装载磁盘卷序列号的变量
LPDWORD lpMaximumComponentLength, // 指定一个变量,用于装载文件名每一部分的长度
LPDWORD lpFileSystemFlags, // 用于装载一个或多个二进制位标志的长度
LPTSTR lpFileSystemNameBuffer, // 指定一个缓冲区
DWORD nFileSystemNameSize // lpFileSystemNameBuffer,的长度
);
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Button4: TButton;
OpenDialog1: TOpenDialog;
ComboBox1: TComboBox;
Edit5: TEdit;
Label2: TLabel;
Button5: TButton;
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
function gefiletime(sFilename:string;timetype:integer):tdatetime;
var
ffd:twin32finddata;
dft:dword;
lft,time:tfiletime;
h:thandle;
begin
h:=windows.FindFirstFile(pchar(sfilename),ffd);
case timetype of
0:time:=ffd.ftCreationTime;
1:time:=ffd.ftLastWriteTime;
2:time:=ffd.ftLastAccessTime;
end;
if(h<>invalid_handle_value) then
begin
windows.FindClose(h);
filetimetolocalfiletime(time,lft);
filetimetodosdatetime(lft,longrec(dft).hi,longrec(dft).lo);
result:=filedatetodatetime(dft);
end
else
result:=0;
end;
function getdiskvoiserialld(drivename:string):dword;
var
dwtemp1,dwtemp2:dword;
nresult:pdword;
begin
new(nresult);
getvolumeinformation(pchar(drivename+'\'),nil,0,nresult,dwtemp1,dwtemp2,nil,0);
result:=nresult^;
dispose(nresult);
end;
{$R *.dfm}
procedure TForm1.Button4Click(Sender: TObject);
begin
if opendialog1.Execute then
edit1.Text:=opendialog1.FileName;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
edit2.Text:=datetostr(gefiletime(edit1.text,0));
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
edit3.Text:=datetostr(gefiletime(edit1.text,1));
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
edit4.Text:=datetostr(gefiletime(edit1.text,2));
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
edit5.Text:=inttostr(getdiskvoiserialld(combobox1.Items[combobox1.ItemIndex]));
end;
procedure TForm1.Button5Click(Sender: TObject);
var
nu : DWORD;
Vf : DWORD;
Num : DWORD; //用来存储得到的硬盘序列号
vl : array[0..MAX_PATH] of Char; //字符串的长度
begin
GetVolumeInformation(PChar(Trim(Edit1.Text)),nil,SizeOf(vl),@Num,nu,Vf,nil,0);
Edit2.Text := IntToStr(Num);
end;
end.
posted on 2011-09-13 09:15 delphi初学者 阅读(1957) 评论(0) 编辑 收藏 举报