You cannot use floats for comparison reliably. Version number is not a (float) number. It's a sequence of integer numbers. You can even have a version like 1.2.3, you cannot even represent that as a float.
You better implement some handy utility function like:
function AreVersionNumbersAtLeast(
Major, Minor, MajorAtLeast, MinorAtLeast: Cardinal): Boolean;
begin
Result :=
(Major > MajorAtLeast) or
((Major = MajorAtLeast) or (Minor >= MinorAtLeast));
end;
And use it like:
if AreVersionNumbersAtLeast(Major, Minor, 1, 3) then
Log('Program is 1.3 or newer');
If you commonly use the MS returned by the GetVersionNumbers
, you can wrap it to:
function IsVersionMSAtLeast(VersionMS, MajorAtLeast, MinorAtLeast: Cardinal): Boolean;
var
Major: Cardinal;
Minor: Cardinal;
begin
Major := VersionMS shr 16;
Minor := VersionMS and $FFFF;
Result := AreVersionNumbersAtLeast(Major, Minor, MajorAtLeast, MinorAtLeast);
end;
And use it like:
if IsVersionMSAtLeast(VersionMS, 1, 3) then
Log('Program is 1.3 or newer');
Or even a shorthand for a specific file like:
function IsFileVersionAtLeast(
FileName: string; MajorAtLeast, MinorAtLeast: Integer): Boolean;
var
VersionMS, VersionLS: Cardinal;
begin
Result :=
GetVersionNumbers(FileName, VersionMS, VersionLS) and
IsVersionMSAtLeast(VersionMS, MajorAtLeast, MinorAtLeast);
end;
And use it like:
if IsFileVersionAtLeast(ExpandConstant('{app}\MyProg.exe'), 1, 3) then
Log('Program is 1.3 or newer');
If you prefer the 1.3 syntax, use strings, not floats.
Parse the string with a function like:
function VersionStrToNumbers(Version: string; var Major, Minor: Cardinal): Boolean;
var
V1, V2: Integer;
P: Integer;
begin
P := Pos('.', Version);
Result := (P > 0);
if Result then
begin
V1 := StrToIntDef(Copy(Version, 1, P - 1), -1);
V2 := StrToIntDef(Copy(Version, P + 1, Length(Version) - P), -1);
Result := (V1 >= 0) and (V2 >= 0);
if Result then
begin
Major := Cardinal(V1);
Minor := Cardinal(V2);
end;
end;
end;
And use it to implement a handy function like:
function IsFileVersionAtLeastStr(FileName, Version: string): Boolean;
var
MajorAtLeast, MinorAtLeast: Cardinal;
begin
Result :=
VersionStrToNumbers(Version, MajorAtLeast, MinorAtLeast) and
IsFileVersionAtLeast(FileName, MajorAtLeast, MinorAtLeast);
end;
And use it like:
if IsFileVersionAtLeast(ExpandConstant('{app}\MyProg.exe'), '1.3') then
Log('Program is 1.3 or newer');
(This is just a conceptual code, I didn't test any of these)
Another approach is to calculate the "MS" and compare that:
function Version(Major, Minor: Cardinal): Cardinal;
begin
Result := (Major shl 16) + Minor;
end;
Use it like:
if VersionMS >= Version(1, 3) then
Log('Program is 1.3 or newer');