从Inno Setup中的程序调用函数?

时间:2016-04-28 21:31:49

标签: inno-setup pascal

我试图在Inno Setup退出之前检查我刚刚安装的服务是否正在运行。我需要在之后执行一个程序,所以我试图在run参数中调用一个使用BeforeInstall函数的过程。

我在另一篇文章中找到了这个例子,我试图改变它以检查我的服务是否在安装后但在执行运行行之前运行。我是pascal的新手,我似乎无法解决如何从程序中调用函数的问题。任何帮助,将不胜感激。谢谢!

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

1 个答案:

答案 0 :(得分:3)

您需要更改代码的排列,以便在IsAppRunning尝试使用它之前知道AfterInstall - 否则编译器不知道它在那里。 (它没有展望未来,但Delphi的编译器也没有。)

您还有第二个问题(从您的问题中不明显)。程序没有函数所做的预定义Result变量,因为过程没有结果。您还需要在AfterInstallProc过程中声明一个局部变量,以避免variable "Result" is not declared错误。

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