Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Function From Procedure in Inno Setup?

I'm trying to check if a service that I just installed is running or not before exit in Inno Setup. I need to execute a program just after so I am trying to call a procedure that uses a function from BeforeInstall in a run parameter.

I found this example from another post and I'm trying to alter it to check if my service is running after it installs but before the run line is executed. I'm new to pascal and I can't seem to work out how to call the function from the procedure. Any help would be appreciated. Thanks!

[Run]
; Launch the Setup App here
Filename: "{app}\MyApp.exe"; BeforeInstall: AfterInstallProc

[Code]
procedure AfterInstallProc;
begin
  result := not IsAppRunning('MyService.exe');
  if not result then
    MsgBox('Error message here', mbError, MB_OK);
end;

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;
like image 406
raximus Avatar asked Apr 28 '16 21:04

raximus


1 Answers

You need to change the arrangement of your code so that IsAppRunning is known before AfterInstall tries to use it - otherwise the compiler doesn't know it's there. (It doesn't look ahead, but neither does Delphi's compiler.)

You also have a second problem (which isn't apparent from your question). Procedures don't have the pre-defined Result variables that functions do, because procedures don't have results. You'll also need to declare a local variable in your AfterInstallProc procedure to avoid a variable "Result" is not declared error.

[Run]
; Launch the Setup App here
Filename: "{app}\MyApp.exe"; BeforeInstall: AfterInstallProc

[Code]
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;

procedure AfterInstallProc;
var 
  Result: Boolean;
begin
  Result := not IsAppRunning('MyService.exe');
  if not Result then
    MsgBox('Error message here', mbError, MB_OK);
end;
like image 199
Ken White Avatar answered Oct 17 '22 19:10

Ken White