繁体   English   中英

CefSharp如何重命名和嵌入BrowserSubProcess.exe

[英]CefSharp how to rename and embed BrowserSubProcess.exe

我在这里很绝望。 我在 C# 中找不到任何示例代码。 我想重命名 BrowserSubProcess.exe,如果可能的话,我希望它嵌入我的主 exe。

我知道这个解决方案;

https://github.com/cefsharp/CefSharp/issues/1149#issuecomment-225547869

重命名 CefSharp.BrowserSubprocess winforms

但我无法实现它。 我需要示例程序或代码来理解。 我希望@amaitland 能看到这一点并帮助我。

我将 BrowserSubProcess Program.cs 嵌入到我的 Program.cs 中,因此它现在已嵌入。

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        if (args.Count() < 5)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new LoginForm());
        }
        else
        {
            MyBrowserSubProcess(args);
        }
    }

    static int MyBrowserSubProcess(string[] args)
    {
        Debug.WriteLine("BrowserSubprocess starting up with command line: " + String.Join("\n", args));

        SubProcess.EnableHighDPISupport();

        int result;
        var type = args.GetArgumentValue(CefSharpArguments.SubProcessTypeArgument);

        var parentProcessId = -1;

        // The Crashpad Handler doesn't have any HostProcessIdArgument, so we must not try to
        // parse it lest we want an ArgumentNullException.
        if (type != "crashpad-handler")
        {
            parentProcessId = int.Parse(args.GetArgumentValue(CefSharpArguments.HostProcessIdArgument));
            if (args.HasArgument(CefSharpArguments.ExitIfParentProcessClosed))
            {
                Task.Factory.StartNew(() => AwaitParentProcessExit(parentProcessId), TaskCreationOptions.LongRunning);
            }
        }

        // Use our custom subProcess provides features like EvaluateJavascript
        if (type == "renderer")
        {
            var wcfEnabled = args.HasArgument(CefSharpArguments.WcfEnabledArgument);
            var subProcess = wcfEnabled ? new WcfEnabledSubProcess(parentProcessId, args) : new SubProcess(args);

            using (subProcess)
            {
                result = subProcess.Run();
            }
        }
        else
        {
            result = SubProcess.ExecuteProcess();
        }

        Debug.WriteLine("BrowserSubprocess shutting down.");

        return result;
    }

    private static async void AwaitParentProcessExit(int parentProcessId) 
    {
        try 
        {
            var parentProcess = Process.GetProcessById(parentProcessId);
            parentProcess.WaitForExit();
        }
        catch (Exception e) 
        {
            //main process probably died already
            Debug.WriteLine(e);
        }

        await Task.Delay(1000); //wait a bit before exiting

        Debug.WriteLine("BrowserSubprocess shutting down forcibly.");

        Environment.Exit(0);
    }

}

我的 BrowserSubprocessPath 是我的主要 exe。

settings.BrowserSubprocessPath = System.AppDomain.CurrentDomain.FriendlyName;

我终于设法重命名了这个子进程! 还没有找到任何解决方案如何通过 CefSharp API 做到这一点,但找到了我自己的工作解决方案。

因此,在您使用 CefSharp 的代码中,在Cef.Initialize()之前向 Cef 设置添加一项设置

using CefSharp;
using CefSharp.Wpf;
using System.IO;
using System.Reflection;
using System.Windows;

public App()
{
    var settings = new CefSettings
    {
        BrowserSubprocessPath = Path.Combine(GetAppPath(), $@"runtimes\win-x64\native{ GetAppName() }.exe")
    };
    Cef.InitializeAsync(settings);
}

private static string GetAppPath()
{
    return new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
}

private static string GetAppName()
{
    return Assembly.GetExecutingAssembly().GetName().Name;
}

在此之后转到bin\\Debug\\net6.0-windows\\runtimes\\win-x64\\native\\并将CefSharp.BrowserSubprocess.exe重命名为Name you want to use

完毕。 现在它将使用您需要的自定义名称的文件。

PS 对于自动名称集,您始终可以使用带有命令的 Post-Build 事件在项目构建后重命名文件并将名称设置为与您的程序集名称相同。 我使用这种方法来满足我的需要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM