如題,檢測 Microsoft Visual C++ 2015 Redistributable (x64)
依賴并安裝,若程序安裝或卸載時應(yīng)用運行中將檢測并退出文章來源地址http://www.zghlxwxcb.cn/news/detail-821324.html
所需依賴
-
Microsoft Visual C++ 2015 Redistributable (x64)
Microsoft 官網(wǎng)下載 -
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';
// 通過注冊表判斷所需服務(wù)是否安裝
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;
// 通過名稱終結(jié)進程
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;
文章來源:http://www.zghlxwxcb.cn/news/detail-821324.html
到了這里,關(guān)于有關(guān) Inno Setup 的實踐:檢查并安裝依賴,運行時退出安裝或卸載的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!