TL;DR version:
In an InnoSetup script, how can I detect if a restart is needed because of files that were in use?
More detailed version:
I have an Inno Setup script with the following characteristics:
-
the
ShouldSkipPagefunction is implemented so that all pages (except the welcome page) are skipped unless a custom “Advanced options” checkbox on the welcome page is checked:function ShouldSkipPage(PageID: Integer): Boolean; begin if ((PageID = wpSelectDir) or (PageID = wpSelectProgramGroup) or (PageID = wpSelectTasks) or (PageID = wpFinished) or (PageID = wpReady)) then begin Result := not advancedCheckBox.Checked; end; end; -
CloseApplicationsandRestartApplicationsare set tofalse(*), and some files have therestartreplaceanduninsrestartdeleteflags, so a restart will be required to complete the installation if the files were in use
Now, if a restart is needed, I want to show the Finished page regardless of the state of the “Advanced options” checkbox, because I don’t want to cause a restart without prompting the user. So my code would be something like that:
function ShouldSkipPage(PageID: Integer): Boolean;
begin
if ((PageID = wpSelectDir) or
(PageID = wpSelectProgramGroup) or
(PageID = wpSelectTasks) or
(PageID = wpReady)) then
begin
Result := not advancedCheckBox.Checked;
end
else if ((PageID = wpFinished)) then
begin
Result := (not advancedCheckBox.Checked) and (not IsRestartNeeded);
end
end;
Unfortunately, there is no IsRestartNeeded function (NeedRestart exists, but it’s an event function). I spent a long time looking at the documentation, but I didn’t find any function that could give me this information.
The only option I can think of is to look at HKLM\System\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations to see if it contains any of my files, but it’s a rather ugly solution…
(*) The files I want to replace or remove are a shell extension and some DLLs used by this extension. The reason why I’m not relying on the Restart Manager is because it doesn’t seem to work with explorer.exe: the process is immediately restarted, and my files are locked again.
The usual recommendation is to call
MakePendingFileRenameOperationsChecksumnear the start of your installation process, and then again whenever you want to check whether a restart will be required. As long as it keeps returning the same value, a restart is not required.Note that this won’t take into account “forced restarts” eg. from you implementing
NeedRestartand returning true or from a component marked with therestartflag; you’re expected to be able to figure that out on your own, since you’re in control of that.