slint icon indicating copy to clipboard operation
slint copied to clipboard

document how to prevent a console to open on Windows

Open asuper0 opened this issue 2 years ago • 13 comments

We need to add documentation for windows_subsystem attribute in our rust documentation. See #1190 And also how to keep log messages when wanted.

Original:

How to keep output message in console, but not to open a console window when run the app?

I disable the console window with the solution in #1190 . However, I use clap to let my app got some cli parameters and print help message. The code #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] totally disable all print info. Is there any solution to keep the print info when I'm in a console, but not to open a new one if not?

asuper0 avatar Aug 08 '23 03:08 asuper0

Thanks for reporting this. When you write "disable all print info", do you mean for example the output of clap when you run with --help? Or do you mean your own calls to say eprintln!?

tronical avatar Aug 08 '23 06:08 tronical

I don't know the answer to this problem, but looking on the internet, it seems you may get along with winapi::um::wincon::AttachConsole https://stackoverflow.com/questions/64346513/how-do-i-have-a-hidden-console-when-using-windows-subsystem

We should document the windows_subsystem trick in our rust documentation. Please let us know what you tried and if you found something that worked for you.

ogoffart avatar Aug 08 '23 08:08 ogoffart

Thanks for reporting this. When you write "disable all print info", do you mean for example the output of clap when you run with --help? Or do you mean your own calls to say eprintln!?

Yes, both cargo run -- --help and println! can output nothing. I didn't try eprintln! till now.

asuper0 avatar Aug 09 '23 15:08 asuper0

I don't know the answer to this problem, but looking on the internet, it seems you may get along with winapi::um::wincon::AttachConsole https://stackoverflow.com/questions/64346513/how-do-i-have-a-hidden-console-when-using-windows-subsystem

Ok, I'm glad to try it. Will do this next week since I'm on vacation.

asuper0 avatar Aug 09 '23 16:08 asuper0

Hi, I have tested the winapi::um::wincon::AttachConsole() method, it works.

#![windows_subsystem = "windows")]
// I don't use the #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
// If you want to use it, you may disable code below in debug mode

fn main() {
    unsafe {
        winapi::um::wincon::AttachConsole(0xFFFFFFFF);
    }

    // other code ...

    unsafe {
        winapi::um::wincon::FreeConsole();
    }
}

However, it's not perfect because the cursor of console is not correct. Look pictures below

attach1 The console when app running attach2 The console when app exit, and type some command attach3 Press enter

As you can see, the console's cursor don't know the output content, so it keeps the location when the ./test.exe command is executed.

asuper0 avatar Aug 16 '23 06:08 asuper0

I'd prefer another solution: don't set windows_subsystem but detach console when you run the app from explorer.

fn get_parent_process_name() -> Option<String> {
    use sysinfo::{ProcessExt, RefreshKind, System, SystemExt};

    let sys =
        System::new_with_specifics(RefreshKind::new().with_processes(ProcessRefreshKind::new()));

    if let Some(process) = sys.process(sysinfo::get_current_pid().unwrap()) {
        if let Some(parent_pid) = process.parent() {
            if let Some(parent_process) = sys.process(parent_pid) {
                return Some(parent_process.name().to_lowercase());
            }
        }
    }

    None
}

fn main() {
    let parent_process = get_parent_process_name();
    let run_without_console = match &parent_process {
        Some(s) if s == "explorer.exe" => true,
        _ => false,
    };

    if run_without_console {
        unsafe {
            winapi::um::wincon::FreeConsole();
        }
    }
}

Notice this code don't use #![windows_subsystem = "windows")]. It works well when you run it from console. When run it run explorer, a new console window would show and disappear, I think it's ok.

asuper0 avatar Aug 16 '23 06:08 asuper0

Can we do something in Slint to help with your use case or can we close this issue now?

hunger avatar Aug 16 '23 12:08 hunger

We need to document this in a "platform specific considerations" page or something in our rust documentation. Cf #1190 which was closed before we actually documented the problem.

ogoffart avatar Aug 16 '23 14:08 ogoffart

The problem is solved for me. However, it's better to add something to the document, as @ogoffart has mentioned.

asuper0 avatar Aug 16 '23 14:08 asuper0

Same with C++ https://github.com/slint-ui/slint/issues/1190

ogoffart avatar Jan 19 '24 12:01 ogoffart

How we can solve this issue with Slint-UI Javascript?

ansarizafar avatar Jan 19 '24 14:01 ansarizafar

For each programming language we should have a chapter that explains - on a per-platform basis - what the necessary steps are to prepare the application for deployment. This includes console subsystem de-selection on Windows.

tronical avatar Jan 23 '24 13:01 tronical

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] prevents the console from showing but also --help messages from clap from being available to a user.

sedan-cell avatar May 21 '24 13:05 sedan-cell