Inno setup 腳本判斷 Microsoft Visual C++ Redistributable 不同版本區別

有個需要是需要在安裝包安裝初始化時安裝 Microsoft Visual c++ 2013 Redistributable

也就是判斷軟件安裝前需不需要運行 vcredist_x64.exeVC_redist.x64.exe 這兩個程序

第一反應就是可以通過註冊表判斷是否已經安裝過環境

但測試發現需求的兩個版本不同,註冊表位置竟然也不一樣

問 chatgpt 答案不對,bing 搜索半天也沒找到答案,stackoverflow 也有很多類的答案測試後很多都無法成功,最後結合多個結果終於折騰成功

記錄如下:

判斷 Microsoft Visual c++ 2013 Redistributable(x64) - 12.0.30501 是否已經安裝

註冊表位置 'SOFTWARE\WOW6432Node\Microsoft\DevDiv\vc\Servicing\12.0\RuntimeMinimum'

官方下載的安裝包名爲 vcredist_x64.exe

function IsVCRedistInstalled2013: Boolean;
var
  ResultCode: Cardinal;
begin
  Result := RegQueryDWordValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\WOW6432Node\Microsoft\DevDiv\vc\Servicing\12.0\RuntimeMinimum', 'Install', ResultCode);
  if Result and (ResultCode = 1) then
  begin
    Log('vcredist_x64 2013 visual c++ redistrbutable has already been installed.');
  end
  else
  begin
    Log('vcredist_x64 2013 never installed. installing...');
  end;
end;

判斷 Microsoft Visual C++ 2015-2022 Redistributable(x64) - 14.31.31103 是否已經安裝

註冊表位置 計算機\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64

官方下載的安裝包名爲 VC_redist.x64.exe

function IsVCRedistInstalled2015_2022: Boolean;
var
  ResultCode: Cardinal;
begin
  Result := RegQueryDWordValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64', 'Installed', ResultCode);
  if Result and (ResultCode = 1) then
  begin
    Log('VC_redist.x64 2015+  visual c++ redistrbutable has already been installed.');
  end
  else
  begin
    Log('VC_redist.x64 2015+  visual c++ redistrbutable never installed. installing...');
  end;
end;

有兩點需要注意:

  1. 測試時卸載後記得手動刪除一下注冊表內的信息,不然會有信息殘留
  2. 攤牌卸載安裝測試時,改動一下 Result := RegQueryDWordValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64' 中的路徑,因爲有可能會有緩存導致測試的時候判斷不準確

至於判斷後的安裝就簡單一點了:

[Files] 節點說明安裝包的位置

[Files]
Source: "{#DIST_PATH}\win-unpacked\resources\bin\VC_redist.x64.exe"; DestDir: "{tmp}\resources\bin"
Source: "{#DIST_PATH}\win-unpacked\resources\bin\vcredist_x64.exe"; DestDir: "{tmp}\resources\bin"

在 [code] 節點解壓並執行安裝

[code]
function InstallVC_redist2015_2022: Boolean;
var
  ResultCode: Integer;
begin
  ExtractTemporaryFiles('{tmp}\resources\bin\VC_redist.x64.exe')
  if not Exec(ExpandConstant('{tmp}\resources\bin\VC_redist.x64.exe'), '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
  begin
    Log('Failed to execute VC_redist.x64.exe. Error code: ' + IntToStr(ResultCode));
  end;
end;

vcredist_x64 2013 照葫蘆畫瓢即可

我的 windows 版本

版本 Windows 10 企業版

版本號 21H2

安裝日期 ‎2021/‎11/‎3

操作系統內部版本 19044.3086

體驗 Windows Feature Experience Pack 1000.19041.1000.0


博客園: http://cnblogs.com/willian/
github: https://github.com/willian12345/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章