安装前判断是否已安装
[Registry]
Root: HKCU; Subkey: "Software\PowerIDE_Install"; Flags: uninsdeletekey
[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;
var
IsInstall: Boolean;
IsExist: Boolean;
begin
IsInstall:=true;
//注册表是否存在key
IsExist := RegKeyExists(HKCU, 'Software\PowerIDE_Install');
//程序是否运行
Result := IsAppRunning('PowerIDE.exe') or IsAppRunning('IDE.exe');
if IsExist then
begin
MsgBox('程序已安装,请先卸载再安装! ', mbError, MB_OK);
IsInstall:=false;
end
if Result then
begin
MsgBox('检测到客户端PowerIDE正在运行。' #13#13 '请先关闭它然后继续安装! ', mbError, MB_OK);
IsInstall:=false;
end
result:=IsInstall;
end;
卸载检查程序是否运行
// 卸载
function InitializeUninstall(): Boolean;
begin
Result := IsAppRunning('PowerIDE.exe') or IsAppRunning('IDE.exe');
if Result then
begin
MsgBox('检测到客户端PowerIDE正在运行。' #13#13 '请先关闭它然后继续卸载! ', mbError, MB_OK);
result:=false;
end
else
begin
result := true;
end;
end;
判断程序运行状态
1、只是判断程序是否运行
procedure InitializeWizard();
begin
if IsAppRunning('**.exe') then begin
MsgBox('程序正在运行!', mbInformation, MB_OK);
end
else
MsgBox('程序没有运行!', mbInformation, MB_OK);
end;
2、如果未关闭程序,一直循环提示
procedure InitializeWizard();
begin
while IsAppRunning('**.exe') do begin
Msgbox('安装程序检测到客户端正在运行。' #13#13 '请先关闭它然后继续安装',
mbInformation, MB_OK)
end;
end;
3、安装时如果在运行 提示用户关闭运行,或者退出安装,如果用户没有关闭运行 点击继续安装的话还是在循环内,直到用户关闭已运行程序 再点击继续才会退出循环 继续安装
function InitializeSetup(): Boolean;
var isReslut : Boolean;
begin
if IsAppRunning('**.exe') then begin
isReslut := IsAppRunning('**.exe');
while isReslut do
begin
if MsgBox('安装程序检测到{#MyAppName}客户端正在运行,请先关闭它!'#13''#13'点击“是”继续安装;'#13''#13'点击“否”退出安装!', mbConfirmation, MB_YESNO) = IDYES then
begin
// 获取程序是否在运行, 为false 结束循环,为true 继续循环
isReslut := IsAppRunning('**.exe');
Result := true; // 安装继续
end
else
begin
isReslut := false; // 结束循环
Result := false; // 结束安装
end;
end;
end
else
begin
Result := true;
end;
end;
4、安装时检测程序运行中,提示关闭,关闭弹窗同时也结束安装
function InitializeSetup(): Boolean;
begin
Result := IsAppRunning('**.exe');
if Result then
begin
MsgBox('检测到客户端正在运行。' #13#13 '请先关闭它然后继续安装! ', mbError, MB_OK);
result:=false;
end
else
begin
result := true;
end;