From 2bea778e0f7cc78a575c77db5104e49e26231b6c Mon Sep 17 00:00:00 2001 From: Revertron Date: Fri, 19 Feb 2021 22:04:58 +0100 Subject: [PATCH] Fixed running with console on Windows when compiled as GUI app. --- Cargo.toml | 6 +++++- src/main.rs | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index fb580eb..54e43ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,10 @@ [package] name = "alfis" version = "0.1.0" -authors = ["Revertron "] +authors = ["Revertron "] edition = "2018" build = "build.rs" +readme = "README.md" homepage = "https://alfis.name" repository = "https://github.com/Revertron/Alfis" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -30,6 +31,9 @@ mio = { version = "0.7", features = ["os-poll", "net"] } # for DNS from hermes derive_more = "0.99.9" +[target.'cfg(windows)'.dependencies] +winapi = { version = "0.3.7", features = ["impl-default", "wincon"]} + [build-dependencies] winres = "0.1" diff --git a/src/main.rs b/src/main.rs index 9c43e5b..2fbef3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +// With the default subsystem, 'console', windows creates an additional console window for the program. +// This is silently ignored on non-windows systems. +// See https://msdn.microsoft.com/en-us/library/4cc7ya5b.aspx for more details. #![windows_subsystem = "windows"] extern crate web_view; extern crate tinyfiledialogs as tfd; @@ -8,6 +11,9 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::thread; use std::time::Duration; +#[cfg(windows)] +use winapi::um::wincon::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS}; + use rand::RngCore; use serde::Deserialize; use web_view::*; @@ -34,6 +40,13 @@ const KEYSTORE_DIFFICULTY: usize = 24; const SETTINGS_FILENAME: &str = "alfis.cfg"; fn main() { + // When linked with the windows subsystem windows won't automatically attach + // to the console of the parent process, so we do it explicitly. This fails silently if the parent has no console. + #[cfg(windows)] + unsafe { + AttachConsole(ATTACH_PARENT_PROCESS); + } + println!("ALFIS 0.1.0"); let args: Vec = env::args().collect(); let program = args[0].clone(); @@ -89,6 +102,12 @@ fn main() { } else { run_interface(context.clone(), miner.clone()); } + + // Without explicitly detaching the console cmd won't redraw it's prompt. + #[cfg(windows)] + unsafe { + FreeConsole(); + } } fn start_dns_server(context: &Arc>, settings: &Settings) {