How to check with Inno Setup, if a process is running at a Windows 2008 R2 64bit? How to check with Inno Setup, if a process is running at a Windows 2008 R2 64bit? windows windows

How to check with Inno Setup, if a process is running at a Windows 2008 R2 64bit?


You can use the WMI and the Win32_Process.

Try adding this function to your Inno Setup script.

function IsAppRunning(const FileName : string): Boolean;var    FSWbemLocator: Variant;    FWMIService   : Variant;    FWbemObjectSet: Variant;begin    Result := false;    FSWbemLocator := CreateOleObject('WBEMScripting.SWBEMLocator');    FWMIService := FSWbemLocator.ConnectServer('', 'root\CIMV2', '', '');    FWbemObjectSet :=      FWMIService.ExecQuery(        Format('SELECT Name FROM Win32_Process Where Name="%s"', [FileName]));    Result := (FWbemObjectSet.Count > 0);    FWbemObjectSet := Unassigned;    FWMIService := Unassigned;    FSWbemLocator := Unassigned;end;


I don't have enough rep points to add a comment to RRUZ's excellent answer, so I'll just add this here.Make sure you catch exceptions, otherwise the installer will fail for users who can't access the service.

try      FSWbemLocator := CreateOleObject('WBEMScripting.SWBEMLocator');      FWMIService := FSWbemLocator.ConnectServer('', 'root\CIMV2', '', '');      FWbemObjectSet := FWMIService.ExecQuery(Format('SELECT Name FROM Win32_Process Where Name="%s"',[FileName]));      Result := (FWbemObjectSet.Count > 0);exceptend;


There's an even simpler solution to this; using the code suggested by RRUZ depends on you knowing the install path, which if you run when the installer initialises, you don't know this.

The best solution is to use FindWindowByClassName. It does have a slight prerequisite that you have a main form that's always open, but you can always run multiple checks if you have a variety of forms that could be open. It also goes without saying that you need to make the classname as unique as possible!

Example function:

function IsAppRunning(): Boolean;begin                                                                  Result := (FindWindowByClassName( '{#AppWndClassName}') <> 0) or (FindWindowByClassName( '{#AltAppWndClassName}') <> 0);end;

The # precompile references are defined earlier up the install script...

#define AppWndClassName "TMySplashScreen"#define AltAppWndClassName "TMyMainForm"

Then in the code section, you call it as follows:

function InitializeUninstall(): Boolean;begin  // check if application is running  if IsAppRunning() then  begin    MsgBox( 'An Instance of MyFantasticApp is already running. - Please close it and run the uninstall again.', mbError, MB_OK );    Result := false;  end  else     Result := true;End;

If you need anything more complex than this then you need to look into mutexes, but the beauty with the above code is that its simple, quick and as long as you have reasonably unique classnames, as good as anything else.

(Although admittedly if you're running on a multi-user system, then this probably won't find the window if its in another user's session. But as I said, for the majority of simple situations, this would be fine.)