我们很多时候要和WIN32 API函数打交道。
比如下例中的FileSetAttr、DeleteFile、DateTimeToFileDate
-------------------------------------------------------------------------------------------------美丽分割线---------------------------
年代:2006
文件:MY0601.7z
图1:获取文件版本信息
图2:获得文件属性信息
单元文件:
Unit1.pas
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) btnsetTimeFormat: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Button5: TButton; Memo1: TMemo; Button1: TButton; Edit4: TEdit; procedure Button2Click(Sender: TObject); procedure btnsetTimeFormatClick(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; function GetExeFileVersion(FileName:String):String; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button2Click(Sender: TObject); begin DeleteFile(Edit1.Text); end; procedure TForm1.btnsetTimeFormatClick(Sender: TObject); var Err : DWord; P : PChar; begin P := pchar(Edit2.Text); if SetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_SSHORTDATE,P) then begin Showmessage('OK'); end else begin Err := GetLastError; case Err of ERROR_INVALID_ACCESS : ShowMessage('INVALID_ACCESS'); ERROR_INVALID_FLAGS : showMessage('INVALID_FLAGS'); ERROR_INVALID_PARAMETER : ShowMessage('INVALID_PARAMETER'); end; end; end; procedure TForm1.Button3Click(Sender: TObject); begin FileSetAttr(Edit1.Text,StrToInt(Edit3.Text)); end; procedure TForm1.Button4Click(Sender: TObject); var i22:integer; begin i22:=GetFileAttributes(pchar(Edit1.Text)); ShowMessage(IntToStr(i22)); end; procedure TForm1.Button5Click(Sender: TObject); var i23:integer; begin i23:=DateTimeToFileDate(Now); Memo1.Lines.Add(IntToStr(i23)); end; function GetExeFileVersion(FileName:String):String; var InfoSize,Wnd:DWORD; VerBuf:Pointer; VerInfo:^VS_FIXEDFILEINFO; begin Result:=''; InfoSize:=GetFileVersionInfoSize(PChar(FileName),Wnd); if InfoSize<>0 then begin GetMem(VerBuf,InfoSize); try if GetFileVersionInfo(PChar(FileName),Wnd,InfoSize,VerBuf) then begin VerInfo:=nil; VerQueryValue(VerBuf,'\',Pointer(VerInfo),Wnd); if VerInfo<>nil then Result:=Format('%d.%d.%d.%d',[VerInfo^.dwFileVersionMS shr 16, VerInfo^.dwFileVersionMS and $0000ffff, VerInfo^.dwFileVersionLS shr 16, VerInfo^.dwFileVersionLS and $0000ffff]); end; finally FreeMem(VerBuf,InfoSize); end; end; end; procedure TForm1.Button1Click(Sender: TObject); var verInfo:string; begin verInfo:= GetExeFileVersion(Edit4.Text); ShowMessage(verInfo); end; end.