2
votes

My program has 4 services and the first small service controls the other three services (stops them on a file server for overwriting). The first one is very small and simple and normally it should not be closed (only when newer).

[Files]
Source: "ctrlserversvc3.exe"; DestDir: "{code:GetInstallDir|Program}"

It is in the Files section. Inno Setup asks me everytime for closing the service and restarting it at the end. But it should only ask me when my service in the setup has a newer version, not when it is the same version.

How can I tell Inno Setup to skip this file or question when nothing to change?

1

1 Answers

0
votes

Inno Setup, unfortunately, does not check file version before checking if the file is locked. It should, imho. Because the file won't get installed in the end, if it has the same version.

Anyway, you can implement the check yourself, using the Check parameter;

[Files]
Source: "ctrlserversvc3.exe"; DestDir: "{code:GetInstallDir|Program}"; \
    Check: IsNewer('{#GetFileVersion("ctrlserversvc3.exe")}')
[Code]

procedure CutVersionPart(var VersionString: string; var VersionPart: Word);
var
  P: Integer;
begin
  P := Pos('.', VersionString);
  if P > 0 then
  begin
    VersionPart := StrToIntDef(Copy(VersionString, 1, P - 1), 0);
    Delete(VersionString, 1, P);
  end
    else
  begin
    VersionPart := StrToIntDef(VersionString, 0);
    VersionString := '';
  end;
end;

function IsNewer(InstalledVersion: string): Boolean;
var
  Filename: string;
  ExistingMS, ExistingLS: Cardinal;
  ExistingMajor, ExistingMinor, ExistingRev, ExistingBuild: Cardinal;
  InstalledMajor, InstalledMinor, InstalledRev, InstalledBuild: Word;
begin
  Filename := ExpandConstant(CurrentFilename);
  Log(Format('Checking if %s should be installed', [Filename]));
  if not FileExists(Filename) then
  begin
    Log(Format('File %s does not exist yet, allowing installation', [FileName]));
    Result := True;
  end
    else
  if not GetVersionNumbers(FileName, ExistingMS, ExistingLS) then
  begin
    Log(Format('Cannot retrieve version of existing file %s, allowing installation',
      [FileName]));
    Result := True;
  end
    else
  begin
    ExistingMajor := ExistingMS shr 16;
    ExistingMinor := ExistingMS and $FFFF;
    ExistingRev := ExistingLS shr 16;
    ExistingBuild := ExistingLS and $FFFF;
    Log(Format('Existing file %s version: %d.%d.%d.%d',
      [FileName, ExistingMajor, ExistingMinor, ExistingRev, ExistingBuild]));

    Log(Format('Installing version: %s', [InstalledVersion]));
    CutVersionPart(InstalledVersion, InstalledMajor);
    CutVersionPart(InstalledVersion, InstalledMinor);
    CutVersionPart(InstalledVersion, InstalledRev);
    CutVersionPart(InstalledVersion, InstalledBuild);
    Log(Format('Installing version: %d.%d.%d.%d',
      [InstalledMajor, InstalledMinor, InstalledRev, InstalledBuild]));

    if (InstalledMajor > ExistingMajor) or
       ((InstalledMajor = ExistingMajor) and (InstalledMinor > ExistingMinor)) or
       ((InstalledMajor = ExistingMajor) and (InstalledMinor = ExistingMinor) and
        (InstalledRev > ExistingRev)) then
    begin
      Log('Installing file that is newer than existing file, ' +
        'allowing installation.');
      Result := True;
    end
      else
    begin
      Log('Installing file that is same or older than existing file, ' +
        'skipping installation.');
      Result := False;
    end;
  end;
end;