Uninstall fails because program is running. How do I make Inno Setup check for running process prior to attempting delete?
Asked Answered
S

5

28

Inno Setup fails to remove components during uninstall cause my program is still running and the executable cannot be deleted. How do I have it check to see if it is running before allowing uninstall to proceed?

Spermous answered 16/10, 2009 at 8:25 Comment(0)
R
28

Check these

Inno Setup: Detect instances running in any user session with AppMutex

Inno Setup: Is application running?

Inno Setup: Detect if an application is running

There are several ways. If your program defines a mutex use

[Setup]
AppMutex=MyMutexName

or for a specified mutex in the Code section

function CheckForMutexes (Mutexes: String): Boolean;

You could also use

function FindWindowByClassName (const ClassName: String): Longint; 

to get the window handle by class name and send it messages.

Or get it by the name

function FindWindowByWindowName (const WindowName: String): Longint;

Or you use one of several DLL files for this specific use case

Or do it yourself after reading

How To Terminate an Application "Cleanly" in Win32

How To Enumerate Applications Using Win32 APIs (this one links to the german version as for some reason I can't find the english version of KB175030

or try the google translated version of the KB175030-DE

KB175030 DE -> EN

Repairer answered 16/10, 2009 at 9:41 Comment(3)
Awesome answer. Very thorough; thank you! The global mutex option took 5 minutes to get it right! Nothing better than that.Spermous
The latest English version of KB 175030 that I could find on the Wayback Machine is from February 2007: web.archive.org/web/20070216103757/http://support.microsoft.com/….Straka
A couple more options for checking for a running app at this answer: https://mcmap.net/q/503110/-how-to-determine-with-certainty-if-an-appliction-is-running-before-innosetup-install-script-is-executed.Straka
C
28

We used an other way than described above. Because this is an uninstallation we can kill the application and unistall it. The simpliest way, when u can't use AppMutex: (related to Really killing a process in Windows)

[UninstallRun]
Filename: "{cmd}"; Parameters: "/C ""taskkill /im <precessname>.exe /f /t"

Hope somebody will help this. I searched a long time for this.

Caerleon answered 21/5, 2014 at 13:35 Comment(3)
Does the taskkill kill more than one running instance of the same process ?Pastime
Thanks! Yup, it kills all the instances unless you run the setup with lower privileges than the process is elevated. [+1ed anyway :)]Pastime
Seems to kill the process, but the uninstaller still sees the files as locked and does not remove them.Goalkeeper
G
13

Try this solution! I had issues with other solutions closing the app, but Inno Setup still thought the installed files were locked.

Remember to define your constants:

#define MyAppName "AppName"
#define MyAppExeName "AppName.exe"

[Code]
function InitializeUninstall(): Boolean;
  var ErrorCode: Integer;
begin
  ShellExec('open','taskkill.exe','/f /im {#MyAppExeName}','',SW_HIDE,ewNoWait,ErrorCode);
  ShellExec('open','tskill.exe',' {#MyAppName}','',SW_HIDE,ewNoWait,ErrorCode);
  result := True;
end;

Major props to the original source of this solution.

Goalkeeper answered 20/11, 2014 at 17:25 Comment(2)
This is great, one thing that I changed was to use a different procedure instead of "function InitializeUninstall(): Boolean;" due to the fact that this function will kill the app even if the user cancels the uninstall process whereas "procedure InitializeUninstallProgressForm;" will run only if he confirms to uninstall.Staid
Great solution, worked really well in my case. Thx!Slavery
R
11

Use the AppMutex directive to prevent the uninstaller from proceeding, when an application is running.

[Setup]
AppMutex=MyProgMutex

The application has to create the mutex specified by the directive. See the linked AppMutex directive documentation for examples.


If you want to have the uninstaller kill the application, when it is still running, use this code instead:

function InitializeUninstall(): Boolean;
var
  ErrorCode: Integer;
begin
  if CheckForMutexes('MyProgMutex') and
     (MsgBox('Application is running, do you want to close it?',
             mbConfirmation, MB_OKCANCEL) = IDOK) then
  begin
    Exec('taskkill.exe', '/f /im MyProg.exe', '', SW_HIDE, 
         ewWaitUntilTerminated, ErrorCode);
  end;

  Result := True;
end;

As with the AppMutex directive above, the application has to create the mutex specified in the CheckForMutexes call.


Note that for installer, you do not have to code this. The installer has restart manager built-in.
See Kill process before (re)install using "taskkill /f /im" in Inno Setup.

Rodarte answered 23/9, 2016 at 6:5 Comment(0)
M
0

perhaps add this property

CloseApplications=yes it will look at all the [Files] and [InstallDelete] elements and work using windows restart manager

https://jrsoftware.org/ishelp/index.php?topic=setup_closeapplications

Mouton answered 19/4, 2022 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.