How to detect if Visual C++ Redistributable for Visual Studio 2012 is installed?
I tried Google it and nobody has asked this question, surprise!
This question is related to
visual-c++
This PowerShell code should do the trick
Get-ItemProperty
HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Format-Table –AutoSize
You can check for the Installed
value to be 1
in this registry location: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\11.0\VC\Runtimes\x86
on 64-bit systems. In code that would result in accessing registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\11.0\VC\Runtimes\x86
. Notice the absence of Wow6432Node
.
On a 32-bit system the registry is the same without Wow6432Node
: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\11.0\VC\Runtimes\x86
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.
Of course you can still call MsiQueryProductState to check if the VC redist package is installed via MSI, The package code can be found by running
wmic product get
at command line, or if you are already at wmic:root\cli, run
product where "Caption like '%C++ 2012%'"
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
For me this location worked: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DevDiv\vc\Servicing\11.0\RuntimeMinimum\Version
Check what version you have after you installed the package and use that as a condition in your installer. (mine is set to 11.0.50727 after installing VCred).
What most people miss is the required /reg:32
to check for the key on Windows x64.
See Microsoft Help article on this subject.
Here is a script that demonstrates how to correctly check for Visual C++ Redistributable for Visual Studio 2012 Update 4.
@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
Since Visual Studio 2010 and later stopped using WinSxS, it may be enough to just check for %windir%\system32\msvcr110.dll. If you want to verify you have a new enough version, you can check whether the file version is 11.0.50727.1 (VS2012 RTM) or 11.0.51106.1 (VS2012 Update 1).
It is hard to get all registry values for VC 2012 so I have written a small function which will go through all dependencies and match on specified version.
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;
}
Dependencies:
using System.Text.RegularExpressions;
using Microsoft.Win32;
using System.Linq;
Just go to Control Panel > Programs and Features, and they all appear listed there.
I'm no expert and this answer is pretty simple compared to what people are answering (checking registry), so I'm not sure if it's the correct answer but it did the trick for me.
I needed the same thing, and although AFAIK this cannot be done programmatically, it worked for me.
I just went to Start --> Uninstall a program, and scrolled down until I found the VC++ redistributable, which includes a version number. Googling the version number, told me it belongs to VS2012 SP1.
you can search in registry.Actually I do'nt have vs2012 but I have 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.
HKLM\SOFTWARE\Microsoft\VisualStudio\10.0\VC\VCRedist\x86
HKLM\SOFTWARE\Microsoft\VisualStudio\10.0\VC\VCRedist\x64
HKLM\SOFTWARE\Microsoft\VisualStudio\10.0\VC\VCRedist\ia64
You can use registry function for that......
The answer to this simple questions is unfortunately not a simple one, but working in 100% of all systems, and even extendable to the numerous .net frameworks.
The complexity comes from the fact that there are (and were) many VC runtimes revisions which could lead to the case that although VC10 runtimes were installed, their build number was not recent enough so your EXE wouldn't start unless you either installed the very exact runtimes you required or one of the newer runtimes which enable this and previous versions for the same major version to run with it (the side-by-side hell). Also, if you have a 64 bit EXE, you will have to check for both, the 32 AND 64 bit runtimes.
That said, the only reliable way to determine whether the runtimes for your EXE are installed is to attempt to run the EXE - or a another EXE which is built with the same settings as your main EXE and whose only purpose is to do - nothing. Just run (which means the runtimes are installed) or fail to run (when not installed).
I did the following for an installer which required the VC10 32 and 64 bit runtimes installed: The installer attempts to launch all dummy EXEs and if it succeeds, the corresponding runtime is considered to be installed. This also resolves the 32/64 bit scenario.
This, by the way, works also to determine if the proper .net framework is installed, which is very tricky in Windows 8 and 10, as the downloadable built-in .net 3.5 support also supports the .net versions 3.0 and 2.0 - there are no registry entries for these. (And worse, you cannot even use the standard framework installers here, you must use the built-in support and download it via Windows, or rebuild your app with .net 4, but that's another story).
The C++ dummy EXE can be built using a project with the following code (and another one in a 64 bit configuration if necessary):
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Remember to set the project's properties Use of MFC to Use MFC in a shared DLL. The executables will be around 4KB in size - a small price to pay for a sure result.
To give your users a nice installation experience, you could do the following (sample code is for NSIS):
Function TryLaunchApplication
Pop $1 ; pathname
nsExec::Exec $1
Pop $0
${If} $0 == "error"
${OrIf} $0 != 0
Push 0
${Else}
Push 1
${EndIf}
FunctionEnd
and call it in a function, e.g. 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 Welcome page and cache the result, so you don't have to re-check every time the user clicks on the "Back" and "Next" button.
Next, make a read-only section in the install tree and pre-select or unselect it on the a function which is executed before the Components page is shown.
This will make sure that the installation of each missing runtime component is mandatory, and is skipped if it is already present.
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).
Since the Visual C++ Redistributable packages register their uninstaller with windows (so it shows up in the Control Panel "Programs and Features" list), we simply check for the Display Name of the uninstaller key in the registry.
Here is the relevant NSIS code:
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.
Also note that to update the above code to a different version of the VC++ Redist, simply change the GUID in the registry key path and the display name to whatever you need.
While this may not be the recommended method, It has worked on 10,000+ machines over the past 10 years running every flavor of windows from XP/XP64 Through Windows 10 using redists for 2005, 2010, 2010sp1, and now 2012u4.
I would check the Installed
value of
HKLM\SOFTWARE\[WOW6432Node]\Microsoft\Windows\CurrentVersion\Uninstall\{VCRedist_GUID}
key
VC++ 2012 (x86)
is {33d1fd90-4274-48a1-9bc1-97e33d9c2d6f}
WOW6432Node
will be present or not depending on the VC++ redist
productI've succeded doing this with InnoSetup.
I checked the existence of registry key:
HKLM\SOFTWARE\Microsoft\VisualStudio\11.0\VC\Runtimes
If uninstalled, it does not exist. If installed, it exists.
By the way, it could also be in the Wow6432Node:
HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\11.0\VC\Runtimes
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.
As mentioned elsewhere, one drawback with these approaches is that each update has its own ProductCode!
Thankfully, MSI provides an UpgradeCode which identifies a 'family' of products. You can use orca to open up one of the MSIs to extract this information. For example, the UpgradeCode for VS2015's redistributable is {65E5BD06-6392-3027-8C26-853107D3CF1A}
You can use MsiEnumRelatedProducts to get all Product IDs for that UpgradeCode. In practice, since each redist update replaces the previous one, this will only yield one ProductCode - such as {B5FC62F5-A367-37A5-9FD2-A6E137C0096F}
for VS2015 Update 2 x86.
Regardless, you can then check the version via MsiGetProductInfo(productCode, INSTALLPROPERTY_VERSIONSTRING, ...) or similar functions to compare with the version you want, eg to check for an equivalent or later version.
Note that within a C++ application, you can also use _VC_CRT_MAJOR_VERSION
, _VC_CRT_MINOR_VERSION
, _VC_CRT_BUILD_VERSION
if you #include <crtversion.h>
-- this way you can determine calculate the CRT version that your binary was built with.
Try
HKLM\SOFTWARE\Microsoft\DevDiv\VC\Servicing\11.0
as a starting point. I will be using this as a check for installing the VC++ 11 (VS 2012) runtime.
The powershell script solution:
Based on the information in the answer from @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
}
}
The checking/downloading/silently installing based on $redistInfo
which contains the compiled information from kayleeFrye_onDeck's.
$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
}
The full script and more information can be found on github
Anyone is welcome to contribute, if i have time i will do more extensive testing of the script and keep trying to add new packages as information is added here.
I came across this question looking for an answer in the context of checking for the Visual C++ redistributable as part of an MSI installer created by WiX.
I didn't like how the GUID's change with version and operating system, so I ended up creating a custom action written in C# to check for the Visual C++ redistributable.
Everything below is specifically for Visual C++ 2015 Redistributable (x64), but it can be easily modified for any version.
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;
}
}
}
Then in the wxs file
<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>
Edit I'm updating this answer with some basic info on creating and using a custom action.
To create the custom action in Visual Studio 2017 with the WiX Toolset Visual Studio 2017 extension installed, I used the project template to create a custom action (C# Custom Action Project for WiX v3).
I checked the generated project and it seemed to already have the changes listed at the beginning of this article: https://www.codeproject.com/Articles/132918/Creating-Custom-Action-for-WIX-Written-in-Managed so I picked that article up at the section Adding Custom Action to the Installer
and followed it through with some tweaks.
One other thing that I did was change the version of the .NET framework the project is built against to 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
Source: Stackoverflow.com