我正在尝试检查刚刚安装的服务是否正在运行,然后在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;
您需要更改代码的排列方式,以便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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句