Inno setup 脚本判断 Microsoft Visual C++ Redistributable 不同版本区别

发布时间 2023-07-03 18:07:17作者: 池中物王二狗

有个需要是需要在安装包安装初始化时安装 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/