代码改变世界

检测客户pc电脑端VC++环境并安装

2017-12-17 17:25  CodeCy  阅读(5513)  评论(0编辑  收藏  举报

CefSharp 是一个非常不错的cef封装。但它依赖于VC++环境。

具体如下:

BranchCEF VersionVC++ Version.Net VersionStatus
master 3202 2013 4.5.2 Development
cefsharp/62 3202 2013 4.5.2 Pre-Release
cefsharp/57 2987 2013 4.5.2 Release
cefsharp/55 2883 2013 4.5.2 Unsupported
cefsharp/53 2785 2013 4.5.2 Unsupported
cefsharp/51 2704 2013 4.5.2 Unsupported
cefsharp/49 2623 2013 4.0 Unsupported
cefsharp/47 2526 2013 4.0 Unsupported
cefsharp/45 2454 2013 4.0 Unsupported
cefsharp/43 2357 2012 4.0 Unsupported
cefsharp/41 2272 2012 4.0 Unsupported
cefsharp/39 2171 2012 4.0 Unsupported
cefsharp/37 2062 2012 4.0 Unsupported
 

于是我们在初始化cef前需要先检测vc++环境情况。

通过检测注册表方式,具体实现代码如下:

 public static bool IsInstallVc2013x86()
        {
            try
            {
                Microsoft.Win32.RegistryKey uninstallNode = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");

                foreach (string subKeyName in uninstallNode.GetSubKeyNames())
                {
                    Microsoft.Win32.RegistryKey subKey = uninstallNode.OpenSubKey(subKeyName);
                    object BundleProviderKey = subKey.GetValue("BundleProviderKey");
                    if (BundleProviderKey != null)
                    {
                        //HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{ 5e4b593b - ca3c - 429c - bc49 - b51cbf46e72a}
                        //HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{13A4EE12-23EA-3371-91EE-EFB36DDFFF3E} 中文版本
                        if (BundleProviderKey.ToString() == "{5e4b593b-ca3c-429c-bc49-b51cbf46e72a}")
                        {
                            return true;
                            // MessageBox.Show(displayName.ToString());   
                        }
                        //if (BundleProviderKey.ToString() == "{f65db027-aff3-4070-886a-0d87064aabb1}")  英文版
                        //{
                        //    return true;
                        //    // MessageBox.Show(displayName.ToString());   
                        //}
                    }
                }
            }
            catch (Exception ex)
            {

                //LogTextHelper.Error(ex);
            }

            return false;
        }

  

当为检测到未安装时,提示用户安装。

用户选择确认后,以管理员方式执行安装文件。

        private static bool InitVc2013x86()
        {
            try
            {
                if (!IsInstallVc2013x86())
                {
                    //http://www.microsoft.com/zh-cn/download/details.aspx?id=40784

                    //LogTextHelper.Error("系统未安装Vc2013x86组件");

                    Splasher.Show(typeof(frmSysSplash));

                    Splasher.Status = string.Format("首次使用,正在检测系统组件...");
                    System.Threading.Thread.Sleep(10000);

                    Splasher.Close();

                    MessageUtil.ShowTips("检测到您的机器未安装Microsoft Visual C++ 2013运行组件,系统将为您自动安装。");

                    Splasher.Show(typeof(frmSysSplash));
                    Splasher.Status = string.Format("正在安装系统组件,请耐心等待...");


                    int duration = 10000;
                    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                    stopwatch.Start(); //  开始监视代码运行时间
                    string installMessage = string.Empty;
                    bool installSucces = InstallVc2013x86(out installMessage);
                    stopwatch.Stop(); //  停止监视
                    if (installSucces)//安装成功
                    {
                        if (stopwatch.Elapsed.TotalMilliseconds < duration)
                        {
                            int total = duration - (int)stopwatch.Elapsed.TotalMilliseconds;
                            int step = 30;
                            while (true)
                            {
                                System.Threading.Thread.Sleep(200);
                                Splasher.Status = string.Format("当前进度{0}%...", 100 * step / total);
                                step += 200;
                                if (step > total)
                                {
                                    break;
                                }
                            }
                        }

                        Splasher.Status = string.Format("操作完成!正在验证安装...");
                        System.Threading.Thread.Sleep(3000);
                        Splasher.Close();

                        if (!IsInstallVc2013x86())//校验安装
                        {
                            if (MessageBox.Show("自动安装失败!您可以尝试以管理员身份重新打开或从以下网站手动下载安装。(http://www.microsoft.com/zh-cn/download/details.aspx?id=40784)", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                            {
                                System.Diagnostics.Process.Start("http://www.microsoft.com/zh-cn/download/details.aspx?id=40784");
                            }
                            return false;
                        }
                        return true;
                    }
                    else
                    {
                        Splasher.Close();
                        MessageUtil.ShowTips(installMessage);
                        return false;
                    }
                }

                //LogTextHelper.Error("已安装环境");
                return true;
            }
            catch (Exception ex)
            {
                //LogTextHelper.Error(ex);
            }
            return false;
        }


        public static bool InstallVc2013x86(out string message)
        {
            message = string.Empty;
            string fileName = System.IO.Path.Combine(Application.StartupPath, "vcredist_x86\\vcredist_x86.exe");
            if (!System.IO.File.Exists(fileName))
            {
                //LogTextHelper.Info("未找到文件 " + fileName);
                message = "未找到文件" + fileName;
                return false;
            }

            /**
          * 当前用户是管理员的时候,直接启动应用程序
          * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
          */
            //获得当前登录的Windows用户标示
            System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
            System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);

            //LogTextHelper.Info("开始安装 InstallVc2013x86");
            //判断当前登录用户是否为管理员
            if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
            {
                //LogTextHelper.Info("当前身份为管理员");
                //LogTextHelper.Info("管理员方式运行");
                //LogTextHelper.Info("开始安装 InstallVc2013x86");
                System.Diagnostics.Process.Start(fileName).WaitForExit();
            }
            else
            {
                //LogTextHelper.Info("管理员方式运行");
                //LogTextHelper.Info("开始安装 InstallVc2013x86");
                //创建启动对象 
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                //设置运行文件 
                startInfo.FileName = fileName;
                //设置启动参数 
                //startInfo.Arguments = String.Join(" ", Args);
                //设置启动动作,确保以管理员身份运行 
                startInfo.Verb = "runas";
                //如果不是管理员,则启动UAC 

                System.Diagnostics.Process.Start(startInfo).WaitForExit();
            }
            return true;
        }

  

应用程序入口:

 

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            InitPlugins();
            if (!InitVc2013x86())
            {
                return;
            }
            if (!InitCef())
            {
                MessageUtil.ShowError("主界面初始化失败!");
                return;
            }
            Application.Run(new FrmFirst());
        }

  

 

附录vc++各版本 检查方法:

 

Visual C++ 2005

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

Direct Download URL: 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

Direct Download URL: 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)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Products\67D6ECF5CD5FBA732B8B22BAC8DE1B4D 
Configuration: x64
Version: 9.0.30729.5677

Direct Download URL: https://download.microsoft.com/download/5/D/8/5D8C65CB-C849-4025-8E95-C3966CAFD8AE/vcredist_x64.exe

Microsoft Visual C++ 2008 Redistributable (x86)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Products\6E815EB96CCE9A53884E7857C57002F0
Configuration: x86
Version: 9.0.30729.5677

Direct Download URL: https://download.microsoft.com/download/5/D/8/5D8C65CB-C849-4025-8E95-C3966CAFD8AE/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

Direct Download URL: 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

Direct Download URL: 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

Direct Download URL: 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

Direct Download URL: https://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x86.exe


Visual C++ 2013

Microsoft Visual C++ 2013 Redistributable (x64)
Registry Key: HKLM\SOFTWARE\Classes\Installer\Dependencies\{050d4fc8-5d48-4b8f-8972-47c82c46020f} 
Configuration: x64
Version: 12.0.30501.0

Direct Download URL: https://download.microsoft.com/download/2/E/6/2E61CFA4-993B-4DD4-91DA-3737CD5CD6E3/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

Direct Download URL: https://download.microsoft.com/download/2/E/6/2E61CFA4-993B-4DD4-91DA-3737CD5CD6E3/vcredist_x86.exe


Visual C++ 2015

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

Direct Download URL: 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: HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Dependencies\{e2803110-78b3-4664-a479-3611a381656a}
Configuration: x86
Version: 14.0.24215.1

Direct Download URL: https://download.microsoft.com/download/6/A/A/6AA4EDFF-645B-48C5-81CC-ED5963AEAD48/vc_redist.x86.exe


Visual C++ 2017

Caveat: There's either a new 2017 registry convention being used, or it hasn't been finalized, yet. As I'm guessing the upper-most keys of: [HKEY_CLASSES_ROOT\Installer\Dependencies\,,amd64,14.0,bundle] and [HKEY_CLASSES_ROOT\Installer\Dependencies\,,x86,14.0,bundle]

are subject to change, or at least have different nested GUIDs, I'm going to use list the key that ends with a GUID.

Microsoft Visual C++ 2017 Redistributable (x64) - 14.11.25325
Registry Key: [HKEY_CLASSES_ROOT\Installer\Dependencies\,,amd64,14.0,bundle\Dependents\{6c6356fe-cbfa-4944-9bed-a9e99f45cb7a}]
Configuration: x64
Version: 14.11.25325.0

Direct Download URL: https://download.visualstudio.microsoft.com/download/pr/11100230/15ccb3f02745c7b206ad10373cbca89b/VC_redist.x64.exe

Microsoft Visual C++ 2017 Redistributable (x86) - 14.11.25325
Registry Key: [HKEY_CLASSES_ROOT\Installer\Dependencies\,,x86,14.0,bundle\Dependents\{404c9c27-8377-4fd1-b607-7ca635db4e49}]
Configuration: x86
Version: 14.11.25325.0

Direct Download URL: https://download.visualstudio.microsoft.com/download/pr/11100229/78c1e864d806e36f6035d80a0e80399e/VC_redist.x86.exe


Changelog
September 8th, 2017 -- Added 2017's version for 14.11.25325.0 as the new Visual C++ 2017 entry 
April 7th, 2017 -- Added for 2017's version of 14.10.25008.0 as the new Visual C++ 2017 entry 
October 24th, 2016 -- Updated for 2015's version info for 14.0.24215.1 
August 18th, 2016 -- Updated 2015's version info for 14.0.24212 
May 27th, 2016 -- Updated info for MSVC2015 Update 2