大家好,
我对 MAUI 有疑问。我正在运行一个 MAUI 项目,该项目充当 UI 来配置用于捕获网络流量的捕获设备,并使用自定义 extcap 在 Wireshark 中显示它。
这两个程序串联工作,因为 Wireshark 通过在带有参数的命令行上调用它来启动 MAUI 程序。
问题是,Wireshark 通过读取和写入控制台进行通信。这意味着我需要从 MAUI 应用程序写入它由 Wireshark 启动的控制台。
我尝试了 Console.WriteLine 和 Trace.Listener 都没有成功。
string interfaceString = "interface {value=" + capturer.Name + "}{display=" + capturer.Name + " Capture Interface}";
Console.WriteLine(interfaceString);
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
Trace.WriteLine(interfaceString);
我现在的问题是,有没有办法写入控制台,或者实际上不可能让 MAUI 通过控制台进行通信。 如果存在适用于 Windows 的解决方案,这对我来说就足够了。
在
Trace.WriteLine
,中使用
builder.Logging.AddDebug();
和
CreateMauiApp()
不需要
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
为遇到相同问题的任何人完成此操作: 由于根本无法访问运行 MAUI 的控制台,因此最好使用小型中间件程序。
这个中间件被wireshark调用,在一个新的线程中启动MAUI UI。在 MAUI 本身中,我们可以将输出重新路由到捕获它并将其写回 Wireshark 的中间件。
中间件:
Process p = new Process();
try
{
p.StartInfo.FileName = pathToEXE;
p.StartInfo.Arguments = $"im an arg";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.ErrorDialog = true;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(pathToEXE);
p.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
p.Start();
}
catch (Exception ex)
{
Console.WriteLine("Could not start MAUI: " + ex);
}
while (!p.HasExited)
{
Console.Out.Write(p.StandardOutput.ReadToEnd());
}
Console.WriteLine(p.StandardOutput.ReadToEnd());
MAUI Windows App.xaml.xs
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(int pid);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FreeConsole();
private static extern bool FreeConsole();
bool parentConsole = AttachConsole(Int32.Parse(processID)
进程 ID 必须作为命令行参数传输到 MAUI。