Inno Setup - 防止同时多次执行安装程序
Inno Setup - prevent executing the installer multiple times simultaneously
我对 Inno Setup 有点疑惑:在用户计算机上,我的安装程序 运行 运行缓慢(我尚未诊断,可能是该计算机特有的问题,我还是不知道)。这导致所述用户再次 运行 安装程序,而第一个实例仍在执行 - 令我惊讶的是,它们似乎都 运行 宁了一段时间,然后崩溃和燃烧......
我四处搜索但没有找到任何方法来禁用此行为 - 我的大部分查询都针对 Inno Setup 互斥功能,这并不是我真正想要的。有人知道如何确保安装程序只执行一个实例/进程吗?谢谢!
例如,如果您的安装程序名为 setup.exe,那么您可以使用以下代码检查 setup.exe 是否为 运行 并终止安装。
[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;
function InitializeSetup: boolean;
begin
result := not IsAppRunning('setup.exe');
if not result then
MsgBox('setup.exe is already running', mbError, MB_OK);
end;
从 Inno Setup 5.5.6 开始,您可以使用 SetupMutex
指令:
[Setup]
AppId=MyProgram
SetupMutex=SetupMutex{#SetupSetting("AppId")}
如果您想更改消息的文本,当另一个安装程序已经 运行 时显示,请使用:
[Messages]
SetupAppRunningError=Setup has detected that %1 is currently running.%n%nPlease close all instances of it now, then click OK to continue, or Cancel to exit.
在此版本之前,没有可用的内置机制。但是您可以非常简单地编写自己的。原则是您在设置开始时创建一个唯一的互斥体。但是,首先检查是否没有创建这样的互斥体。如果是,则退出设置,如果不是,则创建互斥锁:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
const
{ this needs to be system-wide unique name of the mutex (up to MAX_PATH long), }
{ there is a discussion on this topic }
{ you can expand here e.g. the AppId directive and add it some 'salt' }
MySetupMutex = 'My Program Setup 2336BF63-DF20-445F-AAE6-70FD7E2CE1CF';
function InitializeSetup: Boolean;
begin
{ allow the setup to run only if there is no thread owning our mutex (in other }
{ words it means, there's no other instance of this process running), so allow }
{ the setup if there is no such mutex (there is no other instance) }
Result := not CheckForMutexes(MySetupMutex);
{ if this is the only instance of the setup, create our mutex }
if Result then
CreateMutex(MySetupMutex)
{ otherwise tell the user the setup will exit }
else
MsgBox('Another instance is running. Setup will exit.', mbError, MB_OK);
end;
我对 Inno Setup 有点疑惑:在用户计算机上,我的安装程序 运行 运行缓慢(我尚未诊断,可能是该计算机特有的问题,我还是不知道)。这导致所述用户再次 运行 安装程序,而第一个实例仍在执行 - 令我惊讶的是,它们似乎都 运行 宁了一段时间,然后崩溃和燃烧......
我四处搜索但没有找到任何方法来禁用此行为 - 我的大部分查询都针对 Inno Setup 互斥功能,这并不是我真正想要的。有人知道如何确保安装程序只执行一个实例/进程吗?谢谢!
例如,如果您的安装程序名为 setup.exe,那么您可以使用以下代码检查 setup.exe 是否为 运行 并终止安装。
[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;
function InitializeSetup: boolean;
begin
result := not IsAppRunning('setup.exe');
if not result then
MsgBox('setup.exe is already running', mbError, MB_OK);
end;
从 Inno Setup 5.5.6 开始,您可以使用 SetupMutex
指令:
[Setup]
AppId=MyProgram
SetupMutex=SetupMutex{#SetupSetting("AppId")}
如果您想更改消息的文本,当另一个安装程序已经 运行 时显示,请使用:
[Messages]
SetupAppRunningError=Setup has detected that %1 is currently running.%n%nPlease close all instances of it now, then click OK to continue, or Cancel to exit.
在此版本之前,没有可用的内置机制。但是您可以非常简单地编写自己的。原则是您在设置开始时创建一个唯一的互斥体。但是,首先检查是否没有创建这样的互斥体。如果是,则退出设置,如果不是,则创建互斥锁:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
const
{ this needs to be system-wide unique name of the mutex (up to MAX_PATH long), }
{ there is a discussion on this topic }
{ you can expand here e.g. the AppId directive and add it some 'salt' }
MySetupMutex = 'My Program Setup 2336BF63-DF20-445F-AAE6-70FD7E2CE1CF';
function InitializeSetup: Boolean;
begin
{ allow the setup to run only if there is no thread owning our mutex (in other }
{ words it means, there's no other instance of this process running), so allow }
{ the setup if there is no such mutex (there is no other instance) }
Result := not CheckForMutexes(MySetupMutex);
{ if this is the only instance of the setup, create our mutex }
if Result then
CreateMutex(MySetupMutex)
{ otherwise tell the user the setup will exit }
else
MsgBox('Another instance is running. Setup will exit.', mbError, MB_OK);
end;