Pregunta

I have an Inno Setup script in which I need to perform a custom action before a specific file is going to be installed or replaced.

The documentation states:

A BeforeInstall or AfterInstall function isn't called if Setup already determined the entry it shouldn't be processed.

So I thought I could use BeforeInstall to specify what should be done before the file is installed, but it seems that Inno Setup takes the decision of installing the file or not after calling the BeforeInstall function, so this function is actually called every time.

How can I perform an action just before a file is installed, but not if the file isn't going to be installed (e.g. because its version hasn't changed)?

EDIT:

Here's the relevant pieces of the code:

...
[Files]
...
Source: "..\bin\{#BuildConfig}\FooBar.dll"; DestDir: "{app}"; Flags: uninsrestartdelete; BeforeInstall: PrepareInstallFooBar

...
[Code]
...
procedure PrepareInstallFooBar();
begin
  Log(Format('BeforeInstall: %s', [CurrentFileName]));
  ...
end;

...

Here's what the log is showing:

...
2014-03-28 10:40:46.778   BeforeInstall: {app}\FooBar.dll
2014-03-28 10:40:46.778   -- File entry --
2014-03-28 10:40:46.778   Dest filename: C:\Users\tom\AppData\Local\MyApp\bin\FooBar.dll
2014-03-28 10:40:46.778   Time stamp of our file: 2014-03-28 10:35:10.000
2014-03-28 10:40:46.778   Dest file exists.
2014-03-28 10:40:46.778   Time stamp of existing file: 2014-03-28 10:35:10.000
2014-03-28 10:40:46.778   Version of our file: 1.0.0.0
2014-03-28 10:40:46.778   Version of existing file: 1.0.0.0
2014-03-28 10:40:46.778   Same version. Skipping.
...
¿Fue útil?

Solución

Since I couldn't make Inno Setup behave the way I wanted, I eventually checked the version myself in a Check function:

...
[Files]
Source: "..\bin\{#BuildConfig}\FooBar.dll"; DestDir: "{app}"; Flags: uninsrestartdelete; Check: ShouldInstallFooBar; BeforeInstall: BeforeInstallFooBar;
...

[Code]
function ShouldInstallFooBar() : Boolean;
var
  fooBarTempPath: String;
  installedFooBarPath: String;
  newFooBarVersion: String;
  installedFooBarVersion: String;
begin
  installedFooBarPath := ExpandConstant('{app}\FooBar.dll');
  if FileExists(installedFooBarPath) then begin
    // Get version of currently installed file
    if GetVersionNumbersString(installedFooBarPath, installedFooBarVersion) then begin
      Log('File already exists; version = ' + installedFooBarVersion);
      // Extract new file and get its version
      ExtractTemporaryFile('FooBar.dll');
      fooBarTempPath := ExpandConstant('{tmp}\FooBar.dll');
      if GetVersionNumbersString(fooBarTempPath, newFooBarVersion) then begin
        Log('New version = ' + newFooBarVersion);
        if newFooBarVersion <> installedFooBarVersion then begin
          fooBarShouldBeInstalled := true;
        end else begin
          fooBarShouldBeInstalled := false;
        end;
      end else begin
        // Failed to get version for new file; assume it's different and install it
        fooBarShouldBeInstalled := true;
      end;
    end else begin
      // Failed to get version for existing file; assume it's different and install the new one
      fooBarShouldBeInstalled := true;
    end;
  end else begin
    // File doesn't exist, install it
    fooBarShouldBeInstalled := true;
  end;
  Result := fooBarShouldBeInstalled;
end;

procedure BeforeInstallFooBar();
begin
  // what I need to do before the file is installed...
end;

...
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top