况洋洋
2025-07-04 0d247bd2a17e0f99f3609774a1ce54ae00857997
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright © 2021 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 
using System;
using System.IO;
using System.Reflection;
 
namespace CefSharp
{
    /// <summary>
    /// CefRuntime - Used to simplify loading of the CefSharp architecture specific resources.
    /// Typical use case would be when you are targeting AnyCPU
    /// </summary>
    public static class CefRuntime
    {
        private static ResolveEventHandler currentDomainAssemblyResolveHandler;
 
        /// <summary>
        /// When using AnyCPU the architecture specific version of CefSharp.Core.Runtime.dll
        /// needs to be loaded (x64/x86).
        /// This method subscribes to the <see cref="AppDomain.AssemblyResolve"/> event
        /// for <see cref="AppDomain.CurrentDomain"/> and loads the CefSharp.Core.Runtime.dll
        /// based on <see cref="Environment.Is64BitProcess"/>.
        /// This method MUST be called before you call Cef.Initialize, create your first ChromiumWebBrowser instance, basically
        /// before anything CefSharp related happens. This method is part of CefSharp.dll which is an AnyCPU library and
        /// doesn't have any references to the CefSharp.Core.Runtime.dll so it's safe to use.
        /// </summary>
        /// <param name="basePath">
        /// The path containing the x64/x86 folders which contain the CefSharp/CEF resources.
        /// If null then AppDomain.CurrentDomain.SetupInformation.ApplicationBase will be used as the path.
        /// (</param>
        public static void SubscribeAnyCpuAssemblyResolver(string basePath = null)
        {
            if(currentDomainAssemblyResolveHandler != null)
            {
                throw new Exception("UseAnyCpuAssemblyResolver has already been called, call ");
            }
 
            if(basePath == null)
            {
                basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            }
 
            currentDomainAssemblyResolveHandler = (sender, args) =>
            {
                if (args.Name.StartsWith("CefSharp.Core.Runtime"))
                {
                    string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll";
                    string archSpecificPath = Path.Combine(basePath,
                                                           Environment.Is64BitProcess ? "x64" : "x86",
                                                           assemblyName);
 
                    return File.Exists(archSpecificPath)
                               ? System.Reflection.Assembly.LoadFile(archSpecificPath)
                               : null;
                }
 
                return null;
            };
 
            AppDomain.CurrentDomain.AssemblyResolve += currentDomainAssemblyResolveHandler;
        }
 
        /// <summary>
        /// Unsubscribe  from the <see cref="AppDomain.AssemblyResolve"/> event
        /// for <see cref="AppDomain.CurrentDomain"/> that was added in <see cref="UseAnyCpuAssemblyResolver"/>
        /// </summary>
        public static void UnsubscribeAnyCpuAssemblyResolver()
        {
            AppDomain.CurrentDomain.AssemblyResolve -= currentDomainAssemblyResolveHandler;
 
            currentDomainAssemblyResolveHandler = null;
        }
 
        /// <summary>
        /// When using AnyCPU the architecture specific version of CefSharp.Core.Runtime.dll
        /// needs to be loaded (x64/x86).
        /// This method calls <see cref="Assembly.LoadFile(string)"/> to immediately load CefSharp.Core.Runtime.dll
        /// based on <see cref="Environment.Is64BitProcess"/>.
        /// This method MUST be called before you call Cef.Initialize, create your first ChromiumWebBrowser instance, basically
        /// before anything CefSharp related happens. This method is part of CefSharp.dll which is an AnyCPU library and
        /// doesn't have any references to the CefSharp.Core.Runtime.dll so it's safe to use.
        /// </summary>
        /// <param name="basePath">
        /// The path containing the x64/x86 folders which contain the CefSharp/CEF resources.
        /// If null then AppDomain.CurrentDomain.SetupInformation.ApplicationBase will be used as the path.
        /// (</param>
        public static void LoadCefSharpCoreRuntimeAnyCpu(string basePath = null)
        {
            const string assemblyName = "CefSharp.Core.Runtime.dll";
 
            if (basePath == null)
            {
                basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            }
 
            var env = Environment.Is64BitProcess ? "x64" : "x86";
            string archSpecificPath = Path.Combine(basePath,
                                                   env,
                                                   assemblyName);
 
            if (File.Exists(archSpecificPath))
            {
                Assembly.LoadFile(archSpecificPath);
            }
            else
            {
                throw new FileNotFoundException("Unable to load for arch " + env, archSpecificPath);
            }
        }
    }
}