unit clFileVersion; interface uses Windows, Classes, SysUtils, Dialogs; type TFileVersion = class(TObject) private { Private declarations } sVersion : string; iMajor : Integer; iMinor : Integer; iRelease : Integer; iBuild : Integer; iDateTime : LongInt; procedure SetDefaults; function ReadVersionInfo(sProgram: string; Major, Minor, Release, Build : pWord) :Boolean; public { Public declarations } property Version: string read sVersion; property Major: Integer read iMajor default 0; property Minor: Integer read iMinor default 0; property Release: Integer read iRelease default 0; property Build: Integer read iBuild default 0; property DateTime:LongInt read iDateTime default 0; constructor Create; function GetFileVersion(sFile: string): boolean; function MajorMinor : Double; end; function GetFileDate(sFile : String; var dtCreate : TDateTime) : Boolean; implementation constructor TFileVersion.Create; begin inherited Create; SetDefaults; end; function TFileVersion.GetFileVersion(sFile: string): boolean; var Major,Minor, Release, Build : Word; begin Result := ReadVersionInfo(sFile, @Major, @Minor, @Release, @Build); if Result then begin iMajor := Integer(Major); {set major version property} iMinor := Integer(Minor); {set minor version property} iRelease := Integer(Release); {set release version property} iBuild := Integer(Build); {set build version property} sVersion := IntToStr(iMajor) + '.' + IntToStr(iMinor) + '.' + IntToStr(iRelease) + '.' + IntToStr(iBuild); end else begin //showMessage('Could not extract file version info for file' + chr(13) + sFile); SetDefaults; end; end; procedure TFileVersion.SetDefaults; begin sVersion := ''; iMajor := 0; iMinor := 0; iRelease := 0; iBuild := 0; end; function TFileVersion.MajorMinor : Double; begin Result := StrToFloat(IntToStr(iMajor) + '.' + IntToStr(iMinor)); end; function TFileVersion.ReadVersionInfo(sProgram: string; Major, Minor, Release, Build : pWord) :Boolean; var Info: PVSFixedFileInfo; //{$ifdef VER120} {Delphi 4 definition for this differs from D2 & D3} // InfoSize: Cardinal; //{$else} InfoSize: UINT; //{$endif} nHwnd: DWORD; BufferSize: DWORD; Buffer: Pointer; begin BufferSize := GetFileVersionInfoSize(pchar(sProgram),nHWnd); {Get buffer size} Result := True; if BufferSize <> 0 then begin {if zero, there is no version info} GetMem( Buffer, BufferSize); {allocate buffer memory} try if GetFileVersionInfo(PChar(sProgram),nHWnd,BufferSize,Buffer) then begin {got version info} if VerQueryValue(Buffer, '\', Pointer(Info), InfoSize) then begin {got root block version information} if Assigned(Major) then begin Major^ := HiWord(Info^.dwFileVersionMS); {extract major version} end; if Assigned(Minor) then begin Minor^ := LoWord(Info^.dwFileVersionMS); {extract minor version} end; if Assigned(Release) then begin Release^ := HiWord(Info^.dwFileVersionLS); {extract release version} end; if Assigned(Build) then begin Build^ := LoWord(Info^.dwFileVersionLS); {extract build version} end; iDateTime := (Info^.dwFileDateMS) shl 32; iDateTime := iDateTime + Info^.dwFileDateLS; //always comes back zero for some reason end else begin Result := False; {no root block version info} end; end else begin Result := False; {couldn't extract version info} end; finally FreeMem(Buffer, BufferSize); {release buffer memory} end; end else begin Result := False; {no version info at all in the file} end; end; function GetFileDate(sFile : String; var dtCreate : TDateTime) : Boolean; var iFileHandle : Integer; iFileDate : Integer; begin iFileHandle := FileOpen(sFile, fmOpenRead); if iFileHandle = -1 then //not found Result:= false else begin iFileDate := FileGetDate(iFileHandle); dtCreate := FileDateToDateTime(iFileDate); FileClose(iFileHandle); Result := true; end; end; end.