1

According to this question : Boost::process hide console on windows

How can I achieve the same but on Linux platform using boost::process ? (prevent console window creation on the newly spawn child process)

My use case is as followed:

I'm trying to call a cross-platform GUI app built from .NET 5.0 AvaloniaUI (C#). And I'm calling that from another GUI app built from gtkmm (c++). It does create black console along with GUI window.

This symptom doesn't occur when I tried calling the same mentioned Avalonia app with another cross-platform Avalonia app. It doesn't create any console, only the GUI window as intended. (Using System.Diagnostics.Process from .NET C#).

So maybe there is some detail behind that make these api behave differently? (boost::process / System.Diagnostics.Process)

Basically I'm looking for the api to do the same thing with System.Diagnostics.Process (C#) on c++

Wasenshi
  • 33
  • 1
  • 8
  • What symptoms are you trying to fix? Starting a process with `boost::process` on a posix system should not create any "console" - unless the program you start is actually creating it. – Ted Lyngmo Mar 27 '22 at 13:25
  • I'm trying to call a cross-platform GUI app built from .NET 5.0 (AvaloniaUI C#) and Im calling that from another GUI app built from gtkmm (c++). It creates black console along with GUI window. In another case, I tried calling the same mentioned .NET app with another cross-platform Avalonia app, and it doesn't create any console, only GUI window. – Wasenshi Mar 27 '22 at 14:41

2 Answers2

0

On linux a new console isn't created anyways. So, the only thing you might want to do is prevent the child process from sharing the parent's, like so:

#include <boost/process.hpp>

namespace bp = boost::process;
int main() {
    bp::child child(bp::search_path("xclock"), //
                    bp::std_in.null(),         //
                    bp::std_out.null(),        //
                    bp::std_err.null());

    child.wait();
}

If, on the other hand, you would like to create a terminal, you have to tell the OS which one you want and how to start that:

bp::child child(bp::search_path("gnome-terminal"),  //
                std::vector<std::string>{"-x", "/usr/bin/htop"}, //
                bp::std_in.null(),                  //
                bp::std_out.null(),                 //
                bp::std_err.null());

Or, e.g.

bp::child child(bp::search_path("gnome-terminal"),  //
                std::vector<std::string>{"-x", "/usr/bin/htop"}, //
                bp::std_in.null(),                  //
                bp::std_out.null(),                 //
                bp::std_err.null());

Note that the gnome-terminal launcher forks (so it "returns" immediately, leaving the terminal running with the htop inside), but xterm does not. So, on UNIX, like ever, you have lots of choices.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you for your answer. My use case is a bit different, as I have commented on the question, I'm calling a cross-platform app built with .NET AvaloniaUI and it does create a black console along with GUI window. This symtoms doesn't occur when I call it from another cross-platform app. – Wasenshi Mar 27 '22 at 14:44
  • "You call it from another cross-platform app"? What does that mean? How does that do it? Why don't you do it in the same way? – sehe Mar 27 '22 at 17:11
  • By the way, if yours is a .NET core app, than simply make it a GUI binary, not a Console application. Windows distinguishes those, so likely as well for .NET core assemblies – sehe Mar 27 '22 at 17:12
  • Also, have you tried my code? It's not unthinkable that `dotnet` will detect the absence of input/output streams and respond to that. – sehe Mar 27 '22 at 17:14
  • 1
    In fact, I just [created a simple Console assembly](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet#examples) and then ran it with http://coliru.stacked-crooked.com/a/e3ed064e41d54a9d - it didn't create a console/terminal window, not even when ran without terminal (via `at` or `nohup`). You'll have to debug what is different, and perhaps find the option that prevents .NET from creating the console. – sehe Mar 27 '22 at 17:34
  • Yes, perhaps this is more related with the UI framework I use with it (Avalonia). [(their website)](https://avaloniaui.net/) – Wasenshi Mar 27 '22 at 18:35
  • I think the problem may lie within how the System.Diagnostic.Process of C# .NET do thing differently than boost::process Maybe .NET would create console explicitly if some arg hasn't been passed from the caller (which in the problem case, is boost::process)? – Wasenshi Mar 27 '22 at 18:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243364/discussion-between-porameth-kaptaphol-and-sehe). – Wasenshi Mar 27 '22 at 18:44
0

Linux does not create new console, like the others suggested. (unless explicitly define to)

After more investigation, I found that, it's only a misunderstanding.

If you use some GUI lib (like GTKmm, in this case), and try to spawn new process, which is a GUI window also, there might be an afterimage-like effect if you don't do it asynchonously

(If that second GUI window is initialized with black background, like the one created with AvaloniaUI, you may confuse it as another console :p)

Wasenshi
  • 33
  • 1
  • 8