处理可执行文件版本号的类。实现代码如下:
TMFileVersion = class private FMajor : Integer; FMinor : Integer; FRelease : Integer; FBuild : Integer; Function GetVerStr : String; Procedure SetVerStr( aVerStr : String ); public constructor Create; destructor Destroy; override; Procedure GetVersionFromFile( aFileName : String ); Function Compare( aVer : TMFileVersion ) : Integer; Property VerStr : String read GetVerStr write SetVerStr; End; { TMFileVersion } // init constructor TMFileVersion.Create; Begin Inherited; FMajor := 0; FMinor := 0; FRelease := 0; FBuild := 0; End; destructor TMFileVersion.Destroy; Begin Inherited; End; // Get version info from a file Procedure TMFileVersion.GetVersionFromFile( aFileName : String ); Type PVS_FIXEDFILEINFO = ^VS_FIXEDFILEINFO; Var h : Cardinal; // a handle, ignore nSize : Cardinal; // version info size pData : Pointer; // version info data pffiData : PVS_FIXEDFILEINFO; // fixed file info data nffiSize : Cardinal; // fixed file info size Begin FMajor := 0; FMinor := 0; FRelease := 0; FBuild := 0; If ( FileExists( aFileName ) ) Then FBuild := 1; nSize := GetFileVersionInfoSize( PChar( aFileName ), h ); If ( nSize = 0 ) Then Exit; GetMem( pData, nSize ); Try GetFileVersionInfo( PChar( aFileName ), h, nSize, pData ); If ( VerQueryValue( pData, '/', Pointer( pffiData ), nffiSize ) ) Then Begin FMajor := ( pffiData^.dwFileVersionMS ) SHR 16; FMinor := ( pffiData^.dwFileVersionMS ) AND $FFFF; FRelease := ( pffiData^.dwFileVersionLS ) SHR 16; FBuild := ( pffiData^.dwFileVersionLS ) AND $FFFF; End; Finally FreeMem( pData ); End; End; // Compare two version info Function TMFileVersion.Compare( aVer : TMFileVersion ) : Integer; Var n1, n2 : Cardinal; Begin n1 := ( FMajor SHL 16 ) OR FMinor; With aVer Do n2 := ( FMajor SHL 16 ) OR FMinor; If ( n1 > n2 ) Then Result := 1 Else If ( n1 < n2 ) Then Result := -1 Else Begin n1 := ( FRelease SHL 16 ) OR FBuild; With aVer Do n2 := ( FRelease SHL 16 ) OR FBuild; If ( n1 > n2 ) Then Result := 1 Else IF ( n1 < n2 ) Then Result := -1 Else Result := 0; End; End; // Get/Set property - VerStr Function TMFileVersion.GetVerStr : String; Begin Result := Format( '%d,%.02d,%d,%.02d', [FMajor, FMinor, FRelease, FBuild] ); End; Procedure TMFileVersion.SetVerStr( aVerStr : String ); Var sTemp : TStrings; Begin FMajor := 0; FMinor := 0; FRelease := 0; FBuild := 0; sTemp := TStringList.Create; Try sTemp.CommaText := aVerStr; Try FMajor := StrToInt( sTemp.Strings[0] ); FMinor := StrToInt( sTemp.Strings[1] ); FRelease := StrToInt( sTemp.Strings[2] ); FBuild := StrToInt( sTemp.Strings[3] ); Except // Do nothing End; Finally sTemp.Free; End; End;