如题,检测 Microsoft Visual C++ 2015 Redistributable (x64)
依赖并安装,若程序安装或卸载时应用运行中将检测并退出文章来源地址https://www.toymoban.com/news/detail-821324.html
所需依赖
-
Microsoft Visual C++ 2015 Redistributable (x64)
Microsoft 官网下载 -
psvince.dll
Github 下载
编辑 iss 文件
[Files]
...
Source: "psvince_path\psvince.dll"; DestDir: "{app}"
[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent; BeforeInstall: IsAllDependciesInstalled
[Code]
// 通过注册表检测 VC2015+ 是否安装
function IsVCServiceInstalled(): Boolean;
var
Names: TArrayOfString;
I: Integer;
RootKey: Integer;
Subkey: String;
begin
Result := false;
if IsWin64 then
begin
RootKey := HKLM64;
end
else
begin
RootKey := HKLM32;
end;
if RegGetSubKeyNames(RootKey, 'SOFTWARE\Classes\Installer\Dependencies', Names) then
begin
for I := 0 to GetArrayLength(Names)-1 do
begin
SubKey := Names[I];
if Pos('VC,redist.x64,amd64,14', Subkey) > 0 then
begin
Result := true;
Break;
end
else
begin
Result := false;
end
end
end
end;
// 退出安装程序
procedure ExitProcess(uExitCode: Integer);
external 'ExitProcess@kernel32.dll stdcall';
// 通过注册表判断所需服务是否安装
procedure IsAllDependciesInstalled;
var
VCServiceInstalled: Boolean;
MissingService: String;
ResultCode: Integer;
begin
MissingService := '';
VCServiceInstalled := IsVCServiceInstalled();
if not VCServiceInstalled then
MissingService := MissingService + ExpandConstant('{#StringChange(VCServiceName, '&', '&&')}') + #13#10; // #13#10 代表换行
if not VCServiceInstalled then
begin
if MsgBox(FmtMessage(CustomMessage('MissingDependcies'), [MissingService]), mbConfirmation, MB_OKCANCEL) = IDOK then
begin
if not VCServiceInstalled then
Exec(ExpandConstant('{app}\{#VCServiceExeName}'), '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode);
// 如果是 .msi 形式安装
// ShellExec('', 'msiexec.exe', ExpandConstant('/I "{app}\{#VCServiceExeName}" /qb'), '', SW_SHOW, ewWaitUntilTerminated, ResultCode);
end
end
end;
// 检测程序进程是否启动
function IsModuleLoaded2(modulename: String ): Boolean;
external 'IsModuleLoaded2@files:psvince.dll stdcall setuponly';
// 判断进程是否存在
function IsAppRunning(const FileName : string): Boolean;
var
FSWbemLocator: Variant;
FWMIService : Variant;
FWbemObjectSet: Variant;
begin
Result := false;
try
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;
except
if (IsModuleLoaded2(FileName)) then
begin
Result := false;
end
else
begin
Result := true;
end
end;
end;
// 通过名称终结进程
procedure TaskKillProcessByName(AppName: String);
var
WbemLocator : Variant;
WMIService : Variant;
WbemObjectSet: Variant;
WbemObject : Variant;
begin;
WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
WMIService := WbemLocator.ConnectServer('localhost', 'root\CIMV2');
WbemObjectSet := WMIService.ExecQuery('SELECT * FROM Win32_Process Where Name="' + AppName + '"');
if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
begin
WbemObject := WbemObjectSet.ItemIndex(0);
if not VarIsNull(WbemObject) then
begin
WbemObject.Terminate();
WbemObject := Unassigned;
end;
end;
end;
// 安装的时候判断进程是否存在
function InitializeSetup(): Boolean;
begin
Result := IsAppRunning('{#MyAppExeName}');
if Result then
begin
MsgBox(ExpandConstant('{cm:SetupAppRunningError,{#StringChange(MyAppName, '&', '&&')}}'), mbError, MB_OK);
result:=false;
end
else
begin
result := true;
end;
end;
//;卸载的时候判断进程是否存在
function InitializeUninstall(): Boolean;
begin
Result := IsAppRunning('{#MyAppExeName}');
if Result then
begin
MsgBox(ExpandConstant('{cm:UninstallAppRunningError,{#StringChange(MyAppName, '&', '&&')}}'), mbError, MB_OK);
result:=false;
end
else
begin
result := true;
end;
end;
文章来源:https://www.toymoban.com/news/detail-821324.html
到了这里,关于有关 Inno Setup 的实践:检查并安装依赖,运行时退出安装或卸载的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!