Inno Setup升级版本比较


客户端升级的时候,我们往往需要比较版本信息,如果当前版本已经是最新版本,则不需要再重新安装,并给用户做出提示信息。主要代码如下:

//此函数用于截取版本号数字
procedure DecodeVersion (verstr: String; var verint: array of Longint);
var
  i,p: Longint; s: string;
begin
  verint := [0,0,0,0];
  i := 0;
  while ((Length(verstr) > 0) and (i < 4)) do
  begin
    p := pos ('.', verstr);//分隔符“.”,eg:1.0.1.1
    if p > 0 then
    begin
      if p = 1 then s:= '0' else s:= Copy (verstr, 1, p - 1);
      verint[i] := StrToIntDef(s,-1);
      i := i + 1;
      verstr := Copy (verstr, p+1, Length(verstr));
    end
    else
    begin
      verint[i] := StrToIntDef(verstr,-1);
      verstr := '';
    end;
  end;
end;


// 此函数用于比较版本字符串
// 若 ver1 > ver2 返回值??1
// 若 ver1 = ver2 返回值??0
// 若 ver1 < ver2 返回值 -1
function CompareVersion (ver1, ver2: String) : Integer;
var
  verint1, verint2: array of Longint;
  i: integer;
begin
  SetArrayLength (verint1, 4);
  DecodeVersion (ver1, verint1);
  SetArrayLength (verint2, 4);
  DecodeVersion (ver2, verint2);
  Result := 0; i := 0;
  while ((Result = 0) and (i < 4)) do
  begin
  if verint1[i] > verint2[i] then
      Result := 1
    else
    if verint1[i] < verint2[i] then
      Result := -1
    else
      Result := 0;
    i := i + 1;
  end;
end;

function InitializeSetup(): boolean;
var
  regVersion: String;
begin

if RegQueryStringValue(HKLM, ExpandConstant('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{#emit SetupSetting("AppId")}_is1'),
     'DisplayVersion', regVersion) then
  begin
    // Successfully read the value
    if CompareVersion(regVersion,ExpandConstant('{#ProductVerion}')) >= 0 then
    begin
      if WizardSilent() = false then //静默安装不弹窗
      begin
        MsgBox('当前已经是最新的版本', mbInformation, MB_OK);
      end;
      exit;
    end;
  end;
end;