为 CefSharp 应用内置 C++ 运行环境并启用 AnyCPU 支持

一个 CefSharp 应用程序要想正确运行,有两个必要条件:

  1. .NET Framework 4.5.2
  2. VC++ 2015

在部署 CefSharp 应用时经常会遇到因为没有 VC++ 2015 而无法运行的问题:

通过事件查看器,可以观察到一个类型为: System.IO.FileNotFoundException 的异常。

检测 VC++ 2015 运行环境是否安装。

我们可以使用以下 C# 代码来检测本机上是否已经部署了 VC++ 2015 运行环境:

public static bool IsVc2015Installed()
{
    var dependenciesPath = @"SOFTWARE\Classes\Installer\Dependencies";
    var plat = Environment.Is64BitProcess ? "x64" : "x86";
    using (var dependencies = Registry.LocalMachine.OpenSubKey(dependenciesPath))
    {
        if (dependencies == null)
        {
            return false;
        }

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

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

    return false;
}

内置 VC++ 2015 运行时文件

VC++ 2015 运行环境是可以通过 XCopy 部署的。即:如果我们的程序需要 VC++ 2015 运行环境,仅需将 VC++ 2015 的全部文件复制到应用程序目录即可。事实上,有很多商业软件也是这么做的,比如:Navicat 。应用程序目录下一系列 “api-ms-win” 开头的 DLL 文件就是运行时文件。

当我们的 CefSharp 的目标平台仅为 x86 或 x64 ,内置 VC++ 2015 运行时仅需将对应的文件复制到输出目录即可。

如果要启用 AnyCPU 支持,我们仍需为主程序装载 VC++ 2015 运行时。

启用 AnyCPU 支持

以上一篇 让 CefSharp.WinForms 应用程序同时支持32位(x86)和64位(x64)的解决方案 的代码为模板,在 Program 文件中增加 _dllLoaded 变量来保存 VC++ 2015 环境是否已经具备。在 Program 类型的静态构造函数中调用 IsVc2015Installed 函数来确认是否已经安装了 VC++ 2015 环境,并将结果赋值给 _dllLoaded 变量。这代表着,如果本机已经安装过 VC++ 2015 运行环境,则程序内置的环境将不会被加载。

可以使用以下 Windows API 加载非托管类库:

[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string lpFileName);

声明静态的只读字符串数组变量 DllList 用于保存 VC++ 2015 的全部文件名:

private static readonly string[] DllList =
{
    "api-ms-win-core-console-l1-1-0.dll", "api-ms-win-core-datetime-l1-1-0.dll",
    "api-ms-win-core-debug-l1-1-0.dll", "api-ms-win-core-errorhandling-l1-1-0.dll",
    "api-ms-win-core-file-l1-1-0.dll", "api-ms-win-core-file-l1-2-0.dll",
    "api-ms-win-core-file-l2-1-0.dll", "api-ms-win-core-handle-l1-1-0.dll",
    "api-ms-win-core-heap-l1-1-0.dll", "api-ms-win-core-interlocked-l1-1-0.dll",
    "api-ms-win-core-libraryloader-l1-1-0.dll", "api-ms-win-core-localization-l1-2-0.dll",
    "api-ms-win-core-memory-l1-1-0.dll", "api-ms-win-core-namedpipe-l1-1-0.dll",
    "api-ms-win-core-processenvironment-l1-1-0.dll", "api-ms-win-core-processthreads-l1-1-0.dll",
    "api-ms-win-core-processthreads-l1-1-1.dll", "api-ms-win-core-profile-l1-1-0.dll",
    "api-ms-win-core-rtlsupport-l1-1-0.dll", "api-ms-win-core-string-l1-1-0.dll",
    "api-ms-win-core-synch-l1-1-0.dll", "api-ms-win-core-synch-l1-2-0.dll",
    "api-ms-win-core-sysinfo-l1-1-0.dll", "api-ms-win-core-timezone-l1-1-0.dll",
    "api-ms-win-core-util-l1-1-0.dll", "api-ms-win-crt-conio-l1-1-0.dll",
    "api-ms-win-crt-convert-l1-1-0.dll", "api-ms-win-crt-environment-l1-1-0.dll",
    "api-ms-win-crt-filesystem-l1-1-0.dll", "api-ms-win-crt-heap-l1-1-0.dll",
    "api-ms-win-crt-locale-l1-1-0.dll", "api-ms-win-crt-math-l1-1-0.dll",
    "api-ms-win-crt-multibyte-l1-1-0.dll", "api-ms-win-crt-private-l1-1-0.dll",
    "api-ms-win-crt-process-l1-1-0.dll", "api-ms-win-crt-runtime-l1-1-0.dll",
    "api-ms-win-crt-stdio-l1-1-0.dll", "api-ms-win-crt-string-l1-1-0.dll",
    "api-ms-win-crt-time-l1-1-0.dll", "api-ms-win-crt-utility-l1-1-0.dll",
    "ucrtbase.dll"
};

新增 CheckDll 方法,根据运行环境加载对应的 VC++ 2015 运行时:

private static void CheckDll() // 检查浏览器的DLL是否载入
{
    if (_dllLoaded)
    {
        return;
    }

    var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Environment.Is64BitProcess ? "x64" : "x86");
    foreach (var fname in DllList)
    {
        try
        {
            var path = Path.Combine(dir, fname);
            if (File.Exists(path))
            {
                LoadLibrary(path);
            }
        }
        catch
        {
        }
    }

    _dllLoaded = true;
}

注意:CheckDll 需要在 Resolver 方法中被调用。完成以上操作后,CefSharp程序就可以在未安装 VC++ 2015 环境的机器上成功运行了:

开源

本文所展示的全部代码和项目工程均在 Gitee 上开源。完整的项目文件和依赖文件可以在以下地址拿到:

https://gitee.com/coderbusy/demo/tree/master/WinForm/CefSharpEmbedCppRunTime

posted @ 2020-12-20 16:59  Soar、毅  阅读(882)  评论(1编辑  收藏  举报