10

I install a service/daemon, which needs to be killed before uninstall and reinstall.

I already found out how to do it for uninstall:

[UninstallRun]
Filename: "taskkill"; Parameters: "/im ""My Service.exe"" /f"; Flags: runhidden

The [Run] section, however, runs after install, so I can't use it for that. What is the best way to kill the process using taskkill before install?

Please note that I specifically want to kill the process. A more complex solution using IPC offers no benefits in my case, I just want to execute taskkill before installing a particular file.

sashoalm
  • 75,001
  • 122
  • 434
  • 781

2 Answers2

28

I found a way using the BeforeInstall parameter and a simple Pascal Script function in the code section. I added a string parameter so it can be reused for multiple processes.

[Files]
Source: "My Service 1.exe"; DestDir: "{app}"; Flags: ignoreversion; \
    BeforeInstall: TaskKill('My Service 1.exe')
Source: "My Service 2.exe"; DestDir: "{app}"; Flags: ignoreversion; \
    BeforeInstall: TaskKill('My Service 2.exe')
[Code]
procedure TaskKill(FileName: String);
var
  ResultCode: Integer;
begin
    Exec('taskkill.exe', '/f /im ' + '"' + FileName + '"', '', SW_HIDE,
     ewWaitUntilTerminated, ResultCode);
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • 1
    In case taskkill.exe isn't found by environment PATH, you could set an absolute path to it: `Exec(ExpandConstant('{sys}\taskkill.exe'), '/f /im ' + '"' + FileName + '"', ExpandConstant('{sys}'), SW_HIDE, ewWaitUntilTerminated, ResultCode);` – Andy Feb 16 '19 at 21:52
  • not always work, taskkill in one of my client sometime response 'Out of memory' error – joe Apr 24 '20 at 04:38
14

Unless the installer needs to run on a Windows XP machine, or you have set CloseApplications directive to no (the default is yes), the installer should close the application automatically:

enter image description here

The functionality is available since Inno Setup 5.5 on Windows Vista and newer.


Though sometimes yes is not enough, you need to use force:
Installer created via Inno Setup, can't close applications during installation on Windows 10

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992