Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Check if Microsoft Visual C++ 2015 Redistributable Installed on a Device

Tags:

c#

As the title says, I currently cannot find any answers for this problem.

I'm currently using C# to do the checking.

Mostly answers are for version 2013 and below.

If you guys have any suggestion, do share.

Thanks.

like image 975
Ahmad Syahir Avatar asked Oct 26 '18 02:10

Ahmad Syahir


4 Answers

It is hard to get all registry values for VC 2015 so I have written a small function which will go through all dependencies and match on specified version(C++ 2015 x86)

public static bool IsVC2015x86Installed()
{
    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\+\+ 2015.*\(x86\)")) //here u can specify your version.
                {
                    return true;
                }
            }
        }
    }

    return false;
}

Dependencies:

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

EDIT:

C++ 2017 is valid replacement for C++ 2015 so if you want to check it as well edit the regex like this:

Regex.IsMatch(value, @"C\+\+ (2015|2017).*\(x86\)")
like image 100
ssamko Avatar answered Oct 25 '22 15:10

ssamko


The basic answer is: Do not bother if it is there at runtime. Put it into your installer. Let it be executed as part of the normal "Elevated Rights required" Installation process.

If it was already there, the Installer will just do nothing.

If it was not there, it will now run under Administrator rights and be there afterwards.

If it was damaged, hopefully the installer will fix the installation.

If that somehow did not work, there is nothing your puny usercode can do to fix it at runtime. It is the administrators job.

Every installer does that, not the least of which are the Visual Studio and SQL Server ones. The only slight modification I know off is Steam, which runs those installers under Elevated rights before a program is executed for the first time. But that is just "making certain it is there" from a slightly different angle.

I only know one kind of programmer that does not do that: The one never tested his program on a freshly installed Windows (Virtual Machines work) and thus is not aware the requirements even exists (because every other program installs VC Redist and current DX versions).

like image 43
Christopher Avatar answered Oct 25 '22 15:10

Christopher


As mentioned in comments and answer, one way is to let the installer run and see if a more recent version is installed. The installer will display an error and quit. If the installer is run with /quiet flag, then no error is being displayed. Other way is to simply check the registry values:

HKEY_LOCAL_MACHINE\SOFTWARE[\Wow6432Node]\Microsoft\VisualStudio\vs-version\VC\Runtimes\{x86|x64|ARM} key

Here vs-version is the version of Visual Studio (14.0 for Visual Studio 2015 and 2017) The key is ARM, x86 or x64 depending upon the platform.

The version number is stored in the REG_SZ string value Version. If the package being installed is less than the version of the installed, then no need to install.

More info here: https://learn.microsoft.com/en-us/cpp/ide/redistributing-visual-cpp-files?view=vs-2017

like image 26
Gauravsa Avatar answered Oct 25 '22 15:10

Gauravsa


I modified @ssamko 's Version to check for x64 and x86 redistributables. Hope it will help someone:

public static bool IsVC2015Installed()
{
    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 (Environment.Is64BitOperatingSystem)
                {
                    if (Regex.IsMatch(value, @"C\+\+ 2015.*\((x64|x86)\)"))
                    {
                        return true;
                    }
                }
                else
                {
                    if (Regex.IsMatch(value, @"C\+\+ 2015.*\(x86\)"))
                    {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

Dependencies:

using System;
using System.Text.RegularExpressions;
using Microsoft.Win32;
like image 43
Phyti1 Avatar answered Oct 25 '22 14:10

Phyti1