inno setup 内配置如下图:

下面代码直接追加到上面截图的编辑器内 Inno Setup ,复制即用
[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;
1.测试判断程序是否运行
procedure InitializeWizard();
begin
if IsAppRunning('hezhong.exe') then begin
MsgBox('智慧园区正在运行', mbInformation, MB_OK);
end
else
MsgBox('智慧园区没有运行', mbInformation, MB_OK);
end;
2.第一种写法:安装时 如果在运行提示用户关闭在运行的程序 ,如果用户不关则无限次的循环下去
procedure InitializeWizard();
begin
while IsAppRunning('hezhong.exe') do begin
Msgbox('安装程序检测到客户端正在运行。' #13#13 '请先关闭它然后继续安装',
mbInformation, MB_OK)
end;
end;
第二种写法:安装时如果在运行 提示用户关闭运行,或者退出安装,如果用户没有关闭运行 点击继续安装的话还是在循环内,知道用户关闭已运行程序 再点击继续才会退出循环 继续安装,卸载时同理
// 安装
function InitializeSetup(): Boolean;
var isReslut : Boolean;
begin
if IsAppRunning('hezhong.exe') then begin
isReslut := IsAppRunning('hezhong.exe');
while isReslut do
begin
if MsgBox('安装程序检测到{#MyAppName}客户端正在运行,请先关闭它!'#13''#13'点击“是”继续安装;'#13''#13'点击“否”退出安装!', mbConfirmation, MB_YESNO) = IDYES then
begin
// 获取程序是否在运行, 为false 结束循环,为true 继续循环
isReslut := IsAppRunning('hezhong.exe');
Result := true; // 安装继续
end
else
begin
isReslut := false; // 结束循环
Result := false; // 结束安装
end;
end;
end
else
begin
Result := true;
end;
end;
// 卸载
function InitializeUninstall(): Boolean;
var isReslut : Boolean;
begin
if IsAppRunning('hezhong.exe') then begin
isReslut := IsAppRunning('hezhong.exe');
while isReslut do
begin
if MsgBox('卸载程序检测到{#MyAppName}客户端正在运行,请先关闭它!'#13''#13'点击“是”继续卸载;'#13''#13'点击“否”退出卸载!', mbConfirmation, MB_YESNO) = IDYES then
begin
// 获取程序是否在运行, 为false 结束循环,为true 继续循环
isReslut := IsAppRunning('hezhong.exe');
Result := true; // 卸载继续
end
else
begin
isReslut := false; // 结束循环
Result := false; // 结束卸载
end;
end;
end
else
begin
Result := true;
end;
end;
第三种写法:简单写法 安装时检测到已有运行程序 提示关闭,关闭弹窗同时也结束继续安装,卸载同理。
// 安装
function InitializeSetup(): Boolean;
begin
Result := IsAppRunning('hezhong.exe');
if Result then
begin
MsgBox('检测到客户端正在运行。' #13#13 '请先关闭它然后继续安装! ', mbError, MB_OK);
result:=false;
end
else
begin
result := true;
end;
end;
// 卸载
function InitializeUninstall(): Boolean;
begin
Result := IsAppRunning('hezhong.exe');
if Result then
begin
MsgBox('检测到客户端正在运行。' #13#13 '请先关闭它然后继续卸载! ', mbError, MB_OK);
result:=false;
end
else
begin
result := true;
end;
end;
本文介绍了如何在使用Inno Setup进行安装和卸载时,检查目标程序是否正在运行,并提供了三种不同的处理策略:1) 无限循环提示用户关闭;2) 用户选择关闭或退出安装;3) 直接终止安装请求,直至程序关闭。
4172

被折叠的 条评论
为什么被折叠?



