Inno Setup - prevent executing the installer multiple times simultaneously

Since Inno Setup 5.5.6 you can use the SetupMutex directive:

[Setup]
AppId=MyProgram
SetupMutex=SetupMutex{#SetupSetting("AppId")}

If you want to change a text of the message, that displays when another installer is running already, use:

[Messages]
SetupAppRunningError=Setup has detected that %1 is currently running.%n%nPlease close all instances of it now, then click OK to continue, or Cancel to exit.

Before this version, there was no built-in mechanism available. But you could write your own pretty simply. Principle is that you create a unique mutex when the setup starts. But, as first you check if there is no such mutex already created. If so, you exit the setup, if not, you create the mutex:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
const
  { this needs to be system-wide unique name of the mutex (up to MAX_PATH long), }
  { there is a discussion on this topic http://stackoverflow.com/q/464253/960757 }
  { you can expand here e.g. the AppId directive and add it some 'salt' }
  MySetupMutex = 'My Program Setup 2336BF63-DF20-445F-AAE6-70FD7E2CE1CF';

function InitializeSetup: Boolean;
begin
  { allow the setup to run only if there is no thread owning our mutex (in other }
  { words it means, there's no other instance of this process running), so allow }
  { the setup if there is no such mutex (there is no other instance) }
  Result := not CheckForMutexes(MySetupMutex);
  { if this is the only instance of the setup, create our mutex }
  if Result then
    CreateMutex(MySetupMutex)
  { otherwise tell the user the setup will exit }
  else
    MsgBox('Another instance is running. Setup will exit.', mbError, MB_OK);
end;

Tags:

Inno Setup