检测是否安装了 VisualStudio2012的 VisualC + + 可再发行组件

如何检测是否安装了 VisualC + + 可重新发布的 VisualStudio2012?

我试过谷歌它,没有人问这个问题,惊喜!

155349 次浏览

你可以在注册表中搜索。实际上我没有 vs2012,但我有 vs2010。

There are 3 different (but very similar) registry keys for each of the 3 platform packages. Each key has a DWORD value called “Installed” with a value of 1.

  • MicrosoftVisualStudio10.0 VC VCRedist x86

  • MicrosoftVisualStudio10.0 VC VCRedist x64

  • MicrosoftVisualStudio10.0 VC VCRedist ia64

您可以使用注册表函数... ..。

There is no installcheck element in the bootstrapper package manifest shipped with Visual C++. Guess Microsoft wants to always install if you set it as a prerequisite.

当然你仍然可以调用 MsiQueryProductState 来检查 VC redist 包是否通过 MSI 安装,包代码可以通过运行

wmic product get

at command line, or if you are already at wmic:root\cli, run

product where "Caption like '%C++ 2012%'"

试试看

HKLM\SOFTWARE\Microsoft\DevDiv\VC\Servicing\11.0

作为一个起点。我将使用这个作为安装 VC + + 11(VS2012)运行时的检查。

自从 Visual Studio 2010以及后来停止使用 WinSxS 以来,仅检查% windir% system32 msvcr110.dll 就足够了。如果您想确认您有一个足够新的版本,您可以检查文件版本是11.0.50727.1(VS2012 RTM)还是11.0.51106.1(VS2012 Update 1)。

您可以在这个注册表位置检查 Installed值是否为 1:64位系统上的 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\11.0\VC\Runtimes\x86。在导致访问注册表项 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\11.0\VC\Runtimes\x86的代码中。注意缺少 Wow6432Node

在32位系统上,如果没有 Wow6432Node: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\11.0\VC\Runtimes\x86,注册表也是一样的

对我来说,这个地点起作用了: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DevDiv\vc\Servicing\11.0\RuntimeMinimum\Version

检查安装包之后的版本,并在安装程序中使用该版本作为条件。(在安装 VCred 之后,mine 被设置为11.0.50727)。

if RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0\VC\VCRedist\x86","Installed") = 0 Then
if RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\11.0\VC\Runtimes\x86","Installed") = 0 Then

我需要同样的东西,虽然 AFAIK 这不能做程序化,它为我工作。

我只是去开始—— > 卸载一个程序,并向下滚动,直到我发现 VC + + 可再发行版,其中包括一个版本号。谷歌一下版本号,告诉我它属于 VS2012 SP1。

不幸的是,这个简单问题的答案并不简单,而是100% 适用于所有系统,甚至可以扩展到众多系统。网络框架。

这种复杂性来自于这样一个事实: 有许多 VC 运行时修订版,这可能会导致这样的情况: 尽管安装了 VC10运行时,但它们的版本号不够新,所以你的 EXE 不会启动,除非你要么安装非常精确的运行时,你所需要的或一个较新的运行时,使这个版本和以前的版本相同的主要版本运行(并排地狱)。 Also, if you have a 64 bit EXE, you will have to check for both, the 32 AND 64 bit runtimes.

也就是说,唯一可靠的方法来确定您的 EXE 的运行时是否安装 是尝试运行 EXE-或另一个 EXE 是建立与您的主 EXE 相同的设置,其唯一目的是做-无。只需运行(这意味着运行时已安装)或运行失败(未安装时)。

对于需要安装 VC1032和64位运行时的安装程序,我做了以下操作: 安装程序尝试启动所有虚拟 EXE,如果成功,则认为相应的运行时已安装。这也解决了32/64位场景。

顺便说一下,这也可以用来确定。安装了 net 框架,这在 Windows8和 Windows10中是非常棘手的,因为它是可下载的内置。Net 3.5支持还支持。Net 版本3.0和2.0-这两个版本没有注册表项。 (更糟糕的是,你甚至不能在这里使用标准的框架安装程序,你 必须的使用内置的支持并通过 Windows 下载它,或者用。净4,但那是另一个故事)。

C + + 虚拟 EXE 可以使用以下代码构建(如果需要,还可以使用64位配置构建另一个) :

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

记住将项目的属性 MFC 的使用设置为 在共享 DLL 中使用 MFC。 可执行文件的大小将在4KB 左右——对于一个确定的结果来说,这是一个很小的代价。

为了给您的用户提供良好的安装体验,您可以执行以下操作(示例代码适用于 NSIS) :

Function TryLaunchApplication
Pop $1 ; pathname
nsExec::Exec $1
Pop $0


${If} $0 == "error"
${OrIf} $0 != 0
Push 0
${Else}
Push 1
${EndIf}
FunctionEnd

并在函数中调用它,例如 CheckRuntimes

Function CheckRuntimes
; Try to execute VC++ 10 application (32 bit)
Push "Vc10RuntimeCheckerApp.exe"
Call TryLaunchApplication
Pop $Vc10RuntimesFound


; Add 64 bit check if required.
; Remember to try running the 64 bit EXE only on a 64 bit OS,
; which requires further checks.


; Try to execute .net application
Push "DotNetRuntimeCheckerApp.exe"
Call TryLaunchApplication
Pop $DotNetFrameworkFound
FunctionEnd

Then launch the runtime check e.g. when leaving the 欢迎光临 page and cache the result, so you don't have to re-check every time the user clicks on the "Back" and "Next" button.

接下来,在安装树中创建一个只读部分,并在显示 零件页面之前执行的函数上预先选择或取消选择它。

这将确保每个缺少的运行时组件的安装都是强制性的,如果已经存在,则跳过它们。

只要进入控制面板 > 程序和功能,他们都出现在那里列出。

我不是专家,这个答案与人们回答的问题(查看注册表)相比非常简单,所以我不确定这个答案是否正确,但它确实帮了我一把。

Programs and Features

我已经成功地用 InnoSetup 做到了这一点。

I checked the existence of registry key:

HKLM\SOFTWARE\Microsoft\VisualStudio\11.0\VC\Runtimes

如果已卸载,则不存在。如果已安装,则存在。

顺便说一下,它也可能在 Wow6432Node 中:

HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\11.0\VC\Runtimes

Old question but here is the approach we have used ever since Visual Studio 2005 with success. I just tested it using Visual Studio 2012 Update 4 as well (since we are finally updating our software from 2010 to 2012).

由于 Visual C + + 可再发行包在 Windows 中注册了它们的卸载程序(因此它显示在控制面板的“程序和特性”列表中) ,我们只需检查注册表中卸载程序键的显示名称。

以下是相关的 NSIS 代码:

ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{33d1fd90-4274-48a1-9bc1-97e33d9c2d6f}\" "DisplayName"
StrCmp $0 "Microsoft Visual C++ 2012 Redistributable (x86) - 11.0.61030" vs2012redistInstalled
DetailPrint "Microsoft Visual C++ 2012 Update 4 Redistributable not found!"
DetailPrint "Downloading from www.mywebsite.com"
; insert applicable download code here
ExecWait '"<downloaded redist exe>" /promptrestart /passive'
vs2012redistInstalled:

Note that since our installer is a 32bit exe, windows handles determining if the registry key is actually in the virtualized Wow6432Node instead of the above location so the above code works on both 64bit and 32bit windows installs without having to check both keys explicitly.

还要注意,要将上面的代码更新为不同版本的 VC + + Redist,只需将注册表键路径中的 GUID 和显示名称更改为您需要的任何内容。

虽然这可能不是推荐的方法,但是在过去的10年里,它已经在10,000多台机器上运行了从 XP/XP64到 Windows 10的各种各样的窗口,在2005年、2010年、2010sp1和现在的2012u4中使用了重新编辑。

这取决于您使用的是什么版本。这两个2012年的密钥对我来说工作得很好,它们有相应的版本可供更新4下载。请注意,其中一些注册位置可能与操作系统有关。我从一个 视窗10x64盒子里收集了这些信息。接下来,我将转储所有这些 redist 版本和我搜索以检测安装的 reg 键:


Visual C + + 2005

Microsoft Visual C++ 2005 Redistributable (x64)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Products\1af2a8da7e60d0b429d7e6453b3d0182
Configuration: x64
Version: 6.0.2900.2180

直接下载网址: https://download.microsoft.com/download/8/B/4/8B42259F-5D70-43F4-AC2E-4B208FD8D66A/vcredist_x64.EXE

Microsoft Visual C++ 2005 Redistributable (x86)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Products\c1c4f01781cc94c4c8fb1542c0981a2a
Configuration: x86
Version: 6.0.2900.2180

直接下载网址: https://download.microsoft.com/download/8/B/4/8B42259F-5D70-43F4-AC2E-4B208FD8D66A/vcredist_x86.EXE


Visual C + + 2008

Microsoft Visual C++ 2008 Redistributable - x64 9.0.30729.6161 (SP1)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Products\67D6ECF5CD5FBA732B8B22BAC8DE1B4D
Configuration: x64
Version: 9.0.30729.6161 (Actual $Version data in registry: 0x9007809 [DWORD])

直接下载网址: https://download.microsoft.com/download/2/d/6/2d61c766-107b-409d-8fba-c39e61ca08e8/vcredist_x64.exe

Microsoft Visual C++ 2008 Redistributable - x86 9.0.30729.6161 (SP1)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Products\6E815EB96CCE9A53884E7857C57002F0
Configuration: x86
Version: 9.0.30729.6161 (Actual $Version data in registry: 0x9007809 [DWORD])

直接下载网址: https://download.microsoft.com/download/d/d/9/dd9a82d0-52ef-40db-8dab-795376989c03/vcredist_x86.exe


Visual C + + 2010

Microsoft Visual C++ 2010 Redistributable (x64)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Products\1926E8D15D0BCE53481466615F760A7F
Configuration: x64
Version: 10.0.40219.325

直接下载网址: https://download.microsoft.com/download/1/6/5/165255E7-1014-4D0A-B094-B6A430A6BFFC/vcredist_x64.exe

Microsoft Visual C++ 2010 Redistributable (x86)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Products\1D5E3C0FEDA1E123187686FED06E995A
Configuration: x86
Version: 10.0.40219.325

直接下载网址: https://download.microsoft.com/download/1/6/5/165255E7-1014-4D0A-B094-B6A430A6BFFC/vcredist_x86.exe


Visual C + + 2012

Microsoft Visual C++ 2012 Redistributable (x64)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Dependencies\{ca67548a-5ebe-413a-b50c-4b9ceb6d66c6}
Configuration: x64
Version: 11.0.61030.0

直接下载网址: https://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x64.exe

Microsoft Visual C++ 2012 Redistributable (x86)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Dependencies\{33d1fd90-4274-48a1-9bc1-97e33d9c2d6f}
Configuration: x86
Version: 11.0.61030.0

直接下载网址: https://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x86.exe

版本警告 : 根据用户 伟夏利的发现,“ ... VC + + 2012 update 4(ABC0)附带的二进制文件有 ATL 和 MFC 二进制文件的版本 ABC1,以及其他所有文件的版本 11.0.51106.1,例如 msvcp110.dll 和 msvcr110.dll...”

附加警告——2012年更新4 : 每个用户 < a href = “ https://stackoverflow. com/users/3311255/Krptodr”> Krptodr VC + + 2012 Update 4(x86)在 GUID {95716cce-fc71-413f-8ad5-56c2892d4b3a}下显示

VC + + 2012 Update 4(x64)在 GUID {a1909659-0a08-4554-8af1-2175904903a1}下显示

^ ^ 提供的 GUID 用于安装程序包。然而,这些分支成两个其他 GUID 各自的 arch 类型。有关进一步信息,请参阅以下来源 如何检查是否安装了 MicrosoftVisualC + + 运行时


Visual C + + 2013

Microsoft Visual C++ 2013 Redistributable (x64)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Dependencies\{042d26ef-3dbe-4c25-95d3-4c1b11b235a7}
Configuration: x64
Version: 12.0.40664.0

直接下载网址: https://download.visualstudio.microsoft.com/download/pr/10912041/cee5d6bca2ddbcd039da727bf4acb48a/vcredist_x64.exe

Microsoft Visual C++ 2013 Redistributable (x86)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Dependencies\{f65db027-aff3-4070-886a-0d87064aabb1}
Configuration: x86
Version: 12.0.30501.0

直接下载网址: https://download.microsoft.com/download/2/E/6/2E61CFA4-993B-4DD4-91DA-3737CD5CD6E3/vcredist_x86.exe


Visual C + + 2015

考虑使用2015-2019年捆绑包作为替代方案

Microsoft Visual C++ 2015 Redistributable (x64) - 14.0.24215
Registry Key: HKLM\SOFTWARE\Classes\Installer\Dependencies\{d992c12e-cab2-426f-bde3-fb8c53950b0d}
Configuration: x64
Version: 14.0.24215.1

直接下载网址: https://download.microsoft.com/download/6/A/A/6AA4EDFF-645B-48C5-81CC-ED5963AEAD48/vc_redist.x64.exe

Microsoft Visual C++ 2015 Redistributable (x86) - 14.0.24215
Registry Key: HKLM\SOFTWARE\Classes\Installer\Dependencies\{e2803110-78b3-4664-a479-3611a381656a}
Configuration: x86
Version: 14.0.24215.1

直接下载网址: https://download.microsoft.com/download/6/A/A/6AA4EDFF-645B-48C5-81CC-ED5963AEAD48/vc_redist.x86.exe


Visual C + + 2017

考虑使用2015-2019年捆绑包作为替代方案

警告 : 2017年要么有一个新的注册协议正在使用,要么还没有最终定稿。我猜最上面的键是: [HKEY_CLASSES_ROOT\Installer\Dependencies\,,amd64,14.0,bundle] 还有 [HKEY_CLASSES_ROOT\Installer\Dependencies\,,x86,14.0,bundle]

或者至少有不同的嵌套 GUID,我将使用列出以 GUID 结尾的键。

Microsoft Visual C++ 2017 Redistributable (x64) - 14.16.27012
Registry Key: [HKEY_CLASSES_ROOT\Installer\Dependencies\VC,redist.x64,amd64,14.16,bundle\Dependents\{427ada59-85e7-4bc8-b8d5-ebf59db60423}]
Configuration: x64
Version: 14.16.27012.6

直接下载网址: https://download.visualstudio.microsoft.com/download/pr/9fbed7c7-7012-4cc0-a0a3-a541f51981b5/e7eec15278b4473e26d7e32cef53a34c/vc_redist.x64.exe

Microsoft Visual C++ 2017 Redistributable (x86) - 14.16.27012
Registry Key: [HKEY_CLASSES_ROOT\Installer\Dependencies\VC,redist.x86,x86,14.16,bundle\Dependents\{67f67547-9693-4937-aa13-56e296bd40f6}]
Configuration: x86
Version: 14.16.27012.6

直接下载网址: https://download.visualstudio.microsoft.com/download/pr/d0b808a8-aa78-4250-8e54-49b8c23f7328/9c5e6532055786367ee61aafb3313c95/vc_redist.x86.exe


Visual C + + 2019(2015-2019 bundle)

注意 : Visual C + + 2019中使用了另一个新的注册表约定。也没有针对 Visual C + + 2019的独立安装程序,只有这个包安装程序是 Visual C + + 2015到2019。

14.21.27702

Microsoft Visual C++ 2015-2019 Redistributable (x64) - 14.21.27702
Registry Key: [HKEY_CLASSES_ROOT\Installer\Dependencies\VC,redist.x64,amd64,14.21,bundle\Dependents\{f4220b74-9edd-4ded-bc8b-0342c1e164d8}]
Configuration: x64
Version: 14.21.27702

直接下载网址: https://download.visualstudio.microsoft.com/download/pr/9e04d214-5a9d-4515-9960-3d71398d98c3/1e1e62ab57bbb4bf5199e8ce88f040be/vc_redist.x64.exe

Microsoft Visual C++ 2015-2019 Redistributable (x86) - 14.21.27702
Registry Key: [HKEY_CLASSES_ROOT\Installer\Dependencies\VC,redist.x86,x86,14.21,bundle\Dependents\{49697869-be8e-427d-81a0-c334d1d14950}]
Configuration: x86
Version: 14.21.27702

直接下载网址: https://download.visualstudio.microsoft.com/download/pr/c8edbb87-c7ec-4500-a461-71e8912d25e9/99ba493d660597490cbb8b3211d2cae4/vc_redist.x86.exe

14.22.27821

Microsoft Visual C++ 2015-2019 Redistributable (x86) - 14.22.27821
Registry Key: [HKEY_CLASSES_ROOT\Installer\Dependencies\VC,redist.x86,x86,14.22,bundle\Dependents\{5bfc1380-fd35-4b85-9715-7351535d077e}]
Configuration: x86
Version: 14.22.27821

直接下载网址: Https://download.visualstudio.microsoft.com/download/pr/0c1cfec3-e028-4996-8bb7-0c751ba41e32/1abed1573f36075bfdfc538a2af00d37/vc_redist.x86.exe

Microsoft Visual C++ 2015-2019 Redistributable (x86) - 14.22.27821
Registry Key: [HKEY_CLASSES_ROOT\Installer\Dependencies\VC,redist.x64,amd64,14.22,bundle\Dependents\{6361b579-2795-4886-b2a8-53d5239b6452}]
Configuration: x64
Version: 14.22.27821

直接下载网址: https://download.visualstudio.microsoft.com/download/pr/cc0046d4-e7b4-45a1-bd46-b1c079191224/9c4042a4c2e6d1f661f4c58cf4d129e9/vc_redist.x64.exe


Changelog :
2021年10月26日更新于10月21日进一步澄清调查结果。
2021年10月21日——根据 Krptodr 的发现,为 VC + + 2012更新4(x86)和(x64) GUID 增加了额外的警告细节。
2019年8月19日——增加了2015-2019捆绑版的新版本
2019年6月13日——为2015-2019捆绑包版本 14.21.27702增加了一个新的部分,并在2015年和2017年增加了 小纸条 < < sup > ,考虑使用新捆绑包作为替代。
2018年12月14日——根据 Jim Wolff 的发现,更新了 MSVC2008服务包1的 9.0.30729.6161更新
2018年11月27日 MSVC2017诉 14.16案更新信息
2018年9月12日——在2012年更新的版本警告中增加了每个伟夏利的调查结果4
2018年8月24日——为14.15.26706更新了2017版本,更新后的 Visual C + + 依赖项打包在 VS 201715.8.1中
2018年5月16日——2017年版本14.14.26405.0更新为新的 C + + 2017条目
2017年9月8日——作为 Visual C + + 2017的新条目,更新了2017年版本为14.11.25325.0
April 7th, 2017 -- Updated 2017's version of 14.10.25008.0 as the new Visual C++ 2017 entry
October 24th, 2016 -- Updated 2015's version info for 14.0.24215.1
2016年8月18日——更新2015年版本信息为14.0.24212
2016年5月27日—— MSVC2015更新信息2 < br >

Checking the install state for the product via MsiQueryProductState is pretty much equivalent to checking the registry directly, but you still need the GUID for the ProductCode 产品代码.

正如在其他地方提到的,这些方法的一个缺点是每个更新都有自己的 ProductCode!

值得庆幸的是,MSI 提供了一个 升级代码,它标识了一系列产品。您可以使用 orca 打开其中一个 MSI 来提取这些信息。例如,VS2015的可再发行版本的 UpgradeCode 是 {65E5BD06-6392-3027-8C26-853107D3CF1A}

可以使用 MsiEnumRelatedProducts获取该 UpgradeCode 的所有产品 ID。实际上,由于每个 redist 更新都会替换前一个更新,因此这只会产生一个 ProductCode ——比如用于 VS2015 Update 2 x86的 {B5FC62F5-A367-37A5-9FD2-A6E137C0096F}

无论如何,您都可以通过 MsiGetProductInfo(productCode,INSTALLPROPERTY _ VERSIONSTRING,...)或类似的函数来检查版本,以便与您想要的版本进行比较,例如检查等效版本或更高版本。

注意,在 C + + 应用程序中,如果使用 #include <crtversion.h>,也可以使用 _VC_CRT_MAJOR_VERSION_VC_CRT_MINOR_VERSION_VC_CRT_BUILD_VERSION——这样就可以确定构建二进制文件时使用的 CRT 版本。

这段 PowerShell 代码应该可以解决这个问题

Get-ItemProperty
HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Format-Table –AutoSize

我会检查 Installed的值

HKLM\SOFTWARE\[WOW6432Node]\Microsoft\Windows\CurrentVersion\Uninstall\{VCRedist_GUID}钥匙

  • where GUID of VC++ 2012 (x86) is {33d1fd90-4274-48a1-9bc1-97e33d9c2d6f}
  • WOW6432Node是否存在取决于 VC++ redist产品

大多数人都忽略了在 Windowsx64上检查密钥所需的 /reg:32

关于这个问题,请参阅 微软帮助文章

下面是一个脚本,演示如何正确检查 VisualC + + 可重新发布的 VisualStudio2012Update4。

@ECHO OFF


:Author
REM "CREATED BY WAR59312"
REM "FEB 7th 2017"


REM Clear Screen
CLS


TITLE Detect Visual C++ 2012 Redistributables


REM This Batch Script Detects If Visual C++ Redistributable for Visual Studio 2012 Update 4 Is Installed


:DetectWindowsOS
REM Are We Running On x86 Or x64
IF NOT DEFINED PROCESSOR_ARCHITEW6432 (
IF %PROCESSOR_ARCHITECTURE% EQU x86 (
REM Windows Is x86
GoTo Check32Bit
) ELSE (
REM Windows Is x64
SET NeededFor64BitOnly=/reg:32
GoTo Check64Bit
)) ELSE (
REM Windows Is Unknown But Assume x64 To Be Safe
SET NeededFor64BitOnly=/reg:32
GoTo Check64Bit
)


:Check64Bit
REM Checks If Visual C++ 64Bit Redistributable for Visual Studio 2012 Update 4 Is Installed
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\11.0\VC\Runtimes\x64" /v "Version" %NeededFor64BitOnly% 2>NUL^ | (
FIND "v11.0.61030.00" >NUL
) && (
ECHO.
ECHO 64bit Visual C++ Redistributable for Visual Studio 2012 Update 4 Is Installed
ECHO.
GoTo Check32Bit
) || (
ECHO.
ECHO 64bit Visual C++ Redistributable for Visual Studio 2012 Update 4 Is NOT Installed
ECHO.
GoTo Check32Bit
)


:Check32Bit
REM Checks If Visual C++ 32Bit Redistributable for Visual Studio 2012 Update 4 Is Installed
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\11.0\VC\Runtimes\x86" /v "Version" %NeededFor64BitOnly% 2>NUL^ | (
FIND "v11.0.61030.00" >NUL
) && (
ECHO.
ECHO 32bit Visual C++ Redistributable for Visual Studio 2012 Update 4 Is Installed
) || (
ECHO.
ECHO 32bit Visual C++ Redistributable for Visual Studio 2012 Update 4 Is NOT Installed
)


:END
ECHO.
PAUSE


EXIT

我碰巧遇到这个问题,在检查由 WiX 创建的 MSI 安装程序中的 Visual C + + 可再发行部分的上下文中寻找答案。

我不喜欢 GUID 在版本和操作系统上的改变,所以我最终创建了一个用 C # 编写的自定义操作来检查 Visual C + + 的可再发行性。

下面的所有内容都是专门针对 Visual C + + 2015可再发行版(x64)的,但是可以很容易地对任何版本进行修改。

using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Win32;


namespace CustomActions
{
public class DependencyChecks
{
[CustomAction]
public static ActionResult IsVC2015RedistInstalled(Session session)
{
session.Log("Begin Visual C++ 2015 Redistributable installation check.");


var dependenciesKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Classes\\Installer\\Dependencies");


foreach(var subKey in dependenciesKey.GetSubKeyNames())
{
var dependency = dependenciesKey.OpenSubKey(subKey);
var displayName = (string)dependency.GetValue("DisplayName");
if(displayName != null)
{
if (displayName.Contains("Microsoft Visual C++ 2015 Redistributable (x64)"))
{
session.Log("Visual C++ 2015 Redistributable is installed.");
return ActionResult.Success;
}
}
}


session.Log("Visual C++ 2015 Redistributable is not installed.");
session.Message(InstallMessage.Error, new Record(1, "This application requires Visual C++ 2015 Redistributable. Please install, then run this installer again. https://www.microsoft.com/en-us/download/details.aspx?id=53587"));
return ActionResult.Failure;
}
}
}

然后在 wxs 文件中

<Binary Id='VC2015RedistCheck' SourceFile='!(wix.ResourcesDir=resources)\CustomActions.CA.dll'/>
<CustomAction
Id='VC2015RedistCheckAction'
Execute='immediate'
BinaryKey='VC2015RedistCheck'
DllEntry="IsVC2015RedistInstalled"
Return='check'/>


<InstallExecuteSequence>
<Custom Action='VC2015RedistCheckAction' After='InstallInitialize'/>
</InstallExecuteSequence>

剪辑 I'm updating this answer with some basic info on creating and using a custom action.

要在安装了 WiX Toolset Visual Studio 2017扩展的 Visual Studio 2017中创建自定义操作,我使用项目模板来创建自定义操作(C # Custom Action Project for WiX v3)。

我检查了生成的项目,它似乎已经有了本文开头列出的更改: https://www.codeproject.com/Articles/132918/Creating-Custom-Action-for-WIX-Written-in-Managed,所以我在 Adding Custom Action to the Installer部分选择了这篇文章,并对其进行了一些调整。

我做的另一件事是修改了。NET 框架,该项目是在3.5的基础上构建的。

I didn't find it really useful but you can also see http://wixtoolset.org/documentation/manual/v3/wixdev/extensions/authoring_custom_actions.html

Powershell 脚本解决方案:

根据@kayleeFrye _ onDeck 答案中的信息

I have created a powershell script that checks and installs the versions the user specifies, i haven't done extensive testing with it, but for my own CI (Continuous Integration) scenario it work perfectly.

The full script and info on github

The approach i used was based on checking the regkeys based on information provided here. The following is the gist of what the script does:

function Test-RegistryValue {
param (
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]$Path,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]$Value
)
try {
Get-ItemProperty -Path "$($Path+$Value)" -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
}

基于 $redistInfo的检查/下载/静默安装,其中包含来自 kayleeFrye _ onDeck 的已编译信息。

$redistInstalled = Test-RegistryValue -Path $redistInfo.RegPath -Value $redistInfo.RegValue
if($redistInstalled -eq $False) {
Invoke-WebRequest -Uri $redistInfo.DownloadUrl -OutFile $downloadTargetPath
Start-Process -FilePath $downloadTargetPath -ArgumentList "$($redistInfo.SilentInstallArgs)" -Wait -NoNewWindow | Wait-Process
}

完整的脚本和更多的信息可以在 Github上找到

欢迎任何人贡献,如果我有时间,我会做更广泛的测试脚本,并继续尝试添加新的软件包作为信息添加在这里。

很难为 VC 2012获得所有的注册表值,所以我编写了一个小函数,它将通过所有的依赖关系和匹配指定的版本。

public static bool IsVC2012Installed()
{
string dependenciesPath = @"SOFTWARE\Classes\Installer\Dependencies";


using (RegistryKey dependencies = Registry.LocalMachine.OpenSubKey(dependenciesPath))
{
if (dependencies == null) return false;


foreach (string subKeyName in dependencies.GetSubKeyNames().Where(n => !n.ToLower().Contains("dotnet") && !n.ToLower().Contains("microsoft")))
{
using (RegistryKey subDir = Registry.LocalMachine.OpenSubKey(dependenciesPath + "\\" + subKeyName))
{
var value = subDir.GetValue("DisplayName")?.ToString() ?? null;
if (string.IsNullOrEmpty(value)) continue;


if (Regex.IsMatch(value, @"C\+\+ 2012")) //here u can specify your version.
{
return true;
}
}
}
}


return false;
}

依赖性:

using System.Text.RegularExpressions;
using Microsoft.Win32;
using System.Linq;

I use this one liner for PowerShell in Server 2019 and Windows 10. I'm do not know 这是多么向后兼容。

Get-CimInstance -Class Win32_Product -Filter "Name LIKE '%Visual C++ %Redistri%'" | Select Name, Caption, Version