kyy
2025-07-02 07558e32634314eec359ec8437d97bdc5def64f9
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
// Copyright © 2020 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.
 
//NOTE:Classes in the CefSharp.Core namespace have been hidden from intellisnse so users don't use them directly
 
using CefSharp.Internals;
using System;
using System.IO;
 
namespace CefSharp.BrowserSubprocess
{
    /// <summary>
    /// SelfHost allows your application executable to be used as the BrowserSubProcess
    /// with minimal effort.
    /// https://github.com/cefsharp/CefSharp/wiki/SelfHost-BrowserSubProcess
    /// </summary>
    /// <example>
    /// //WinForms Example
    /// public class Program
    /// {
    ///      [STAThread]
    ///   public static int Main(string[] args)
    ///   {
    ///     var exitCode = CefSharp.BrowserSubprocess.SelfHost.Main(args);
    ///
    ///     if (exitCode >= 0)
    ///     {
    ///       return exitCode;
    ///     }
    ///
    ///     var settings = new CefSettings();
    ///     //Absolute path to your applications executable
    ///     settings.BrowserSubprocessPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
    ///
    ///     Cef.Initialize(settings);
    ///
    ///     var browser = new BrowserForm(true);
    ///     Application.Run(browser);
    ///
    ///     return 0;
    ///   }
    /// }
    /// </example>
    public class SelfHost
    {
        /// <summary>
        /// This function should be called from the application entry point function (typically Program.Main)
        /// to execute a secondary process e.g. gpu, renderer, utility
        /// This overload is specifically used for .Net Core. For hosting your own BrowserSubProcess
        /// it's preferable to use the Main method provided by this class.
        /// - Pass in command line args
        /// </summary>
        /// <param name="args">command line args</param>
        /// <returns>
        /// If called for the browser process (identified by no "type" command-line value) it will return immediately
        /// with a value of -1. If called for a recognized secondary process it will block until the process should exit
        /// and then return the process exit code.
        /// </returns>
        public static int Main(string[] args)
        {
            var type = CommandLineArgsParser.GetArgumentValue(args, CefSharpArguments.SubProcessTypeArgument);
 
            if (string.IsNullOrEmpty(type))
            {
                //If --type param missing from command line CEF/Chromium assums
                //this is the main process (as all subprocesses must have a type param).
                //Return -1 to indicate this behaviour.
                return -1;
            }
 
 
#if NETCOREAPP
            var browserSubprocessDllPath = Initializer.BrowserSubProcessCorePath;
            if (!File.Exists(browserSubprocessDllPath))
            {
                browserSubprocessDllPath = Path.Combine(Path.GetDirectoryName(typeof(CefSharp.Core.BrowserSettings).Assembly.Location), "CefSharp.BrowserSubprocess.Core.dll");
            }
            var browserSubprocessDll = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(browserSubprocessDllPath);
            var browserSubprocessExecutableType = browserSubprocessDll.GetType("CefSharp.BrowserSubprocess.BrowserSubprocessExecutable");
#else
            var browserSubprocessDllPath = Path.Combine(Path.GetDirectoryName(typeof(CefSharp.Core.BrowserSettings).Assembly.Location), "CefSharp.BrowserSubprocess.Core.dll");
            var browserSubprocessDll = System.Reflection.Assembly.LoadFrom(browserSubprocessDllPath);
            var browserSubprocessExecutableType = browserSubprocessDll.GetType("CefSharp.BrowserSubprocess.WcfBrowserSubprocessExecutable");
#endif
 
            var browserSubprocessExecutable = Activator.CreateInstance(browserSubprocessExecutableType);
 
            var mainMethod = browserSubprocessExecutableType.GetMethod("MainSelfHost", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
            var argCount = mainMethod.GetParameters();
 
            var methodArgs = new object[] { args };
 
            var exitCode = mainMethod.Invoke(null, methodArgs);
 
            return (int)exitCode;
        }
    }
}