CefSharp 是一個非常不錯的cef封裝。但它依賴於VC++環境。
具體如下:
| Branch | CEF Version | VC++ Version | .Net Version | Status |
|---|---|---|---|---|
| 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
