从 Inno Setup 中的过程调用函数?
Call Function From Procedure in Inno Setup?
我试图在 Inno Setup 退出之前检查我刚刚安装的服务是否 运行ning。我需要在之后执行一个程序,所以我试图调用一个过程,该过程在 运行 参数中使用 BeforeInstall 中的函数。
我从另一个 post 中找到了这个示例,我正在尝试更改它以检查我的服务是否在 运行ning 安装之后但在执行 运行 行之前.我是 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;
我试图在 Inno Setup 退出之前检查我刚刚安装的服务是否 运行ning。我需要在之后执行一个程序,所以我试图调用一个过程,该过程在 运行 参数中使用 BeforeInstall 中的函数。
我从另一个 post 中找到了这个示例,我正在尝试更改它以检查我的服务是否在 运行ning 安装之后但在执行 运行 行之前.我是 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;