Mega Code Archive

 
Categories / Delphi / Files
 

Class to extract file version in a variety of formats

Title: Class to extract file version in a variety of formats. Question: This class is a wrapper around the windows API call GetFileVersionInfo(). It allows access to the file version numbers and sub-numbers in a variety of ways via class properties. Methods ------------------- Create(const AFileName : string = '') - If AFilename='' then the currently running app name is used, else the specified ath/filename is used. Properties ---------------------- FileName - Get current filename or Set new filename. VerMajor - Returns the MAJOR part of the Version. VerMinor - Returns the MINOR part of the Version. VerRelease - Returns the RELEASE part of the Version. VerBuild - Returns the BUILD part of the Version. AsString - As Compressed String Form eg. '2.0.12.456' AsStringZero - In String Zero format eg. '002.000.012.456' AsHex - As Hex Format (16 char) eg. '00020000000C01C8' AsHexDelim - As '-' Delimited Hex eg. '0002-0000-000C-01C8' AsInt64 - As Int64 value eg. 562949954208200 This is useful for storing in a database field for easy version comparisons. Simple Example : -------------------------- procedure TForm1.Button1Click(Sender: TObject); var oVer : TFileVersion; begin oVer := TFileVersion.Create; // Use current application label1.Caption := IntToStr(oVer.VerMajor); label2.Caption := IntToStr(oVer.VerMinor); label3.Caption := IntToStr(oVer.VerRelease); label4.Caption := IntToStr(oVer.VerBuild); label5.Caption := oVer.AsString; label6.Caption := oVer.AsStringZero; label7.Caption := oVer.AsHex; label8.Caption := oVer.AsHexDelim; label9.Caption := IntToStr(oVer.AsInt64); oVer.Free; end; Answer: unit MahFileVer; interface uses Windows, SysUtils, Classes, Forms,; type {TFileVersion Class} TFileVersion = class(TObject) private FMajor,FMinor, FRelease,FBuild : word; FFileName : string; procedure SetFFileName(const AValue : string); function GetAsString : string; function GetAsStringZero : string; function GetAsHex : string; function GetAsHexDelim : string; function GetAsInt64 : Int64; protected procedure GetVersionInfo; public constructor Create(const AFileName : string = ''); // Properties property FileName : string read FFileName write SetFFileName; property VerMajor : word read FMajor; property VerMinor : word read FMinor; property VerRelease : word read FRelease; property VerBuild : word read FBuild; property AsString : string read GetAsString; property AsStringZero : string read GetAsStringZero; property AsHex : string read GetAsHex; property AsHexDelim : string read GetAsHexDelim; property AsInt64 : Int64 read GetAsInt64; end; // ------------------------------------------------------------------------ interface // =========================================================== // Construct Class, if AFileName is nullstring then use the // currently executing application filenam // =========================================================== constructor TFileVersion.Create(const AFileName : string = ''); begin inherited Create; if AFileName = '' then FFileName := ExtractFilePath(Application.ExeName) + ExtractFileName(Application.ExeName) else FFileName := AFileName; GetVersionInfo; end; // ============================= // Property Set methods // ============================= procedure TFileVersion.SetFFileName(const AValue : string); begin FFileName := AValue; GetVersionInfo; end; // =========================== // Property Get Methods // =========================== function TFileVersion.GetAsString : string; begin Result := IntToStr(FMajor) + '.' + IntToStr(FMinor) + '.' + IntToStr(FRelease) + '.' + IntToStr(FBuild); end; function TFileVersion.GetAsStringZero : string; begin // If any sub ver is 999 then only the last 3 digits // are used. eg. VerBuild=25312 would use diguts 312 only Result := FormatFloat('000',FMajor mod 1000) + '.' + FormatFloat('000',FMinor mod 1000) + '.' + FormatFloat('000',FRelease mod 1000) + '.' + FormatFloat('000',FBuild mod 1000); end; function TFileVersion.GetAsHex : string; begin Result := IntToHex(FMajor,4) + IntToHex(FMinor,4) + IntToHex(FRelease,4) + IntToHex(FBuild,4); end; function TFileVersion.GetAsHexDelim : string; begin Result := IntToHex(FMajor,4) + '-' + IntToHex(FMinor,4) + '-' + IntToHex(FRelease,4) + '-' + IntToHex(FBuild,4); end; function TFileVersion.GetAsInt64 : Int64; var iResult : Int64; begin iResult := FMajor; iResult := (iResult shl 16) + FMinor; iResult := (iResult shl 16) + FRelease; iResult := (iResult shl 16) + FBuild; Result := iResult; end; // =================================== // Get File Version Information // =================================== procedure TFileVersion.GetVersionInfo; var iVerInfoSize,iVerValueSize,iDummy : DWORD; pVerInfo : Pointer; rVerValue : PVSFixedFileInfo; begin FMajor := 0; FMinor := 0; FRelease := 0; FBuild := 0; iVerInfoSize := GetFileVersionInfoSize(PChar(FFileName),iDummy); if iVerInfoSize 0 then begin GetMem(pVerInfo,iVerInfoSize); try GetFileVersionInfo(PChar(FFileName),0,iVerInfoSize,pVerInfo); VerQueryValue(pVerInfo,'\',Pointer(rVerValue),iVerValueSize); with rVerValue^ do begin FMajor := dwFileVersionMS shr 16; FMinor := dwFileVersionMS and $FFFF; FRelease := dwFileVersionLS shr 16; FBuild := dwFileVersionLS and $FFFF; end; finally FreeMem(pVerInfo,iVerInfoSize); end; end; end; end.