CefSharp 如何重命名和嵌入 BrowserSubProcess.exe
CefSharp how to rename and embed BrowserSubProcess.exe
我在这里很绝望。我在 C# 中找不到任何示例代码。
我想重命名 BrowserSubProcess.exe,如果可能的话,我希望它嵌入我的主 exe。
我知道这个解决方案;
https://github.com/cefsharp/CefSharp/issues/1149#issuecomment-225547869
Rename 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
。
完成。现在它将使用您需要的自定义名称的文件。
P.S。对于自动名称集,您始终可以使用 Post-Build 事件和命令在项目构建后重命名文件并将名称设置为与程序集名称相同。我使用这种方法来满足我的需要。
我在这里很绝望。我在 C# 中找不到任何示例代码。 我想重命名 BrowserSubProcess.exe,如果可能的话,我希望它嵌入我的主 exe。
我知道这个解决方案;
https://github.com/cefsharp/CefSharp/issues/1149#issuecomment-225547869
Rename 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()
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
。
完成。现在它将使用您需要的自定义名称的文件。
P.S。对于自动名称集,您始终可以使用 Post-Build 事件和命令在项目构建后重命名文件并将名称设置为与程序集名称相同。我使用这种方法来满足我的需要。