Close running version of program before installing update (Inno Setup)

inno-setupinstallation

This should be simple, I need to stop any previous version of my program from running when the installer starts.

Most people suggested making an exe which does this and calling it before Inno Setup starts. I created an exe using AutoIt which kills all processes of my program. The problem is I don't know how to get Inno Setup to call it before it installs anything.

How do I call an executable before installing files?

Alternatively, if I can just detect if a program is running and tell the user to close it, that would work too.

Best Answer

If the application has a Mutex, you can add an AppMutex value in your Inno Setup installer and it will display a message telling the user to stop the program. You might be able to find the Mutex (if it's got one) by using SysInternals Process Explorer and selecting the program / process and looking at the Handles (CTRL-H) in the Lower Pane.

Here's a link to the a KB article that mentions several methods:
http://www.vincenzo.net/isxkb/index.php?title=Detect_if_an_application_is_running

Alternatively, you might try this (UNTESTED) code in the InitializeSetup:

[Setup]
;If the application has  Mutex, uncomment the line below, comment the InitializeSetup function out, and use the AppMutex.
;AppMutex=MyApplicationMutex

[Code]
const
  WM_CLOSE = 16;

function InitializeSetup : Boolean;
var winHwnd: Longint;
    retVal : Boolean;
    strProg: string;
begin
  Result := True;
  try
    //Either use FindWindowByClassName. ClassName can be found with Spy++ included with Visual C++. 
    strProg := 'Notepad';
    winHwnd := FindWindowByClassName(strProg);
    //Or FindWindowByWindowName.  If using by Name, the name must be exact and is case sensitive.
    strProg := 'Untitled - Notepad';
    winHwnd := FindWindowByWindowName(strProg);
    Log('winHwnd: ' + IntToStr(winHwnd));
    if winHwnd <> 0 then
      Result := PostMessage(winHwnd,WM_CLOSE,0,0);
  except
  end;
end;
Related Topic