Write to the console in .net MAUI
Asked Answered
T

2

5

Good day everyone,

i have a problem with MAUI. I am running a MAUI project that acts as a UI to configurate a capture device that is used to capture network traffic and display this in Wireshark using a custom extcap.

The two programs work in tandem, as Wireshark starts the MAUI program by calling it on a command line with parameters.

The problem is, Wireshark is communicating by reading of and writing to a console. This means I need to write from the MAUI application to the console it was started from by Wireshark.

I tried both Console.WriteLine as well as a Trace.Listener without success.

 string interfaceString = "interface {value=" + capturer.Name + "}{display=" + capturer.Name + " Capture Interface}";
 Console.WriteLine(interfaceString);

 Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
 Trace.WriteLine(interfaceString);

My question now is, is there a way to write to the console, or is it in fact impossible to get MAUI to communicate via the console. It would be sufficient to me if a solution for Windows exists.

Tenancy answered 14/12, 2022 at 13:20 Comment(5)
Start WireShark from within your C# program using System.Diagnostics.Process. Using the System.Diagnostics.Process instance representing the started Wireshark process, you can access its STDOUT, STDERR and STDIN channels/streams as TextReaders or receive Wiresharks's STDOUT/STDERR output through events provided by System.Diagnostics.Process. Read the official reference documentation for System.Diagnostics.Process or search here on Stackoverflow or on the web about details of how to use System.Diagnostics.Process.Aminaamine
Unfortunatly, this is not a viable solution, as Wireshark starts the MAUI program through an extcap capturer and not the other way around.Tenancy
You need to edit your question to provide a better explanation of your scenario. GUI apps don’t usually use the console for IOGlossal
I added some more information, i hope this makes my problem clearer.Tenancy
Vague brainstorms: Add Windows-specific code to Maui app, that looks for an existing process with a known identification. That known process should be a custom c# console project you write, that is able to communicate the way Wireshark wants. Have Wireshark start that console project, which then starts Maui. Which then looks for the console project. Use Windows APIs for inter-process communication. ALTERNATIVELY, looking at Maui source, trace startup process. In YourApp/Platforms/Windows/App.xaml.cs (NOT YourApp/App.xaml.cs), "somehow" inject different Console.Milt
T
1

To finish this up for anyone who has the same problem: As it is simply not possible to access the console MAUI runs from, it is the best idea to use a small middleware program.

This middleware is called by wireshark and starts the MAUI UI in a new thread. In MAUI itself, we can then reroute the output to the middleware that catches it and writes it back to Wireshark.

Middleware:

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)

The process id must be transfered to MAUI as a command line argument.

Tenancy answered 5/5, 2023 at 13:27 Comment(1)
The solution in this answer is actually much simpler and effective.Pulido
W
8

Use Trace.WriteLine along with builder.Logging.AddDebug(); inside CreateMauiApp().

The logging will appear in the Output window (Debug → Windows → Output) after selecting "Debug" from the dropdown.

No need to add Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)).

Wolk answered 17/3, 2023 at 13:5 Comment(0)
T
1

To finish this up for anyone who has the same problem: As it is simply not possible to access the console MAUI runs from, it is the best idea to use a small middleware program.

This middleware is called by wireshark and starts the MAUI UI in a new thread. In MAUI itself, we can then reroute the output to the middleware that catches it and writes it back to Wireshark.

Middleware:

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)

The process id must be transfered to MAUI as a command line argument.

Tenancy answered 5/5, 2023 at 13:27 Comment(1)
The solution in this answer is actually much simpler and effective.Pulido

© 2022 - 2024 — McMap. All rights reserved.