I've read the following post. My Code looks exactly the same, but does not work:
Inno Setup Checking for running process
I copied the example from http://www.vincenzo.net/isxkb/index.php?title=PSVince
But the example doesn't work either, even if I change the code like this:
[Code]
function IsModuleLoaded(modulename: AnsiString): Boolean;
external 'IsModuleLoaded@files:psvince.dll stdcall';
The code always returns false
(the program is not running, even it is running).
Tested at Windows 2008 R2 and Windows 7.
In fact I want to check, if the tomcat5.exe
is running or not. So I think I can't work with a AppMutex
.
I have also seen https://code.google.com/p/psvince/source/detail?r=5
But I can't find any facts about compatibility of that DLL.
Complete code:
[Files]
Source: psvince.dll; Flags: dontcopy
[Code]
function IsModuleLoaded(modulename: AnsiString ): Boolean;
external 'IsModuleLoaded@files:psvince.dll stdcall';
function InitializeSetup(): Boolean;
begin
if(IsModuleLoaded( 'notepad.exe' )) then
begin
MsgBox('Running', mbInformation, MB_OK);
Result := false;
end
else
begin
MsgBox('Not running', mbInformation, MB_OK);
Result := true;
end
end;
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);
except
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With