# HG changeset patch # User Jiri Vanek # Date 1552410660 -3600 # Node ID 9bacada77a2347edfac0c832945c3036a2dc83bf # Parent fce279b33577e5fac247f700611314d34f9c8c10 Hidden console on Windows * rust-launcher/src/dirs_paths_helper.rs: factory for windows os now supply third parameter * rust-launcher/src/main.rs: (get_os) on windows now have three params. Call to get os if-windows cfged out (main) parent process is now checking ATTACH_PARENT_PROCESS before get_os on windows * rust-launcher/src/os_access.rs: oon windows, without console, no window is enforced. Trait got win-only inside_console method windows impl of os_access. Win module made public to expose AttachConsole function. * rust-launcher/src/utils.rs: test logger got windows-only inside_console diff -r fce279b33577 -r 9bacada77a23 ChangeLog --- a/ChangeLog Tue Mar 12 15:42:25 2019 +0100 +++ b/ChangeLog Tue Mar 12 18:11:00 2019 +0100 @@ -1,3 +1,13 @@ +2019-03-12 Lars Herschke + + Hidden console on Windows + * rust-launcher/src/dirs_paths_helper.rs: factory for windows os now supply third parameter + * rust-launcher/src/main.rs: (get_os) on windows now have three params. Call to get os if-windows cfged out + (main) parent process is now checking ATTACH_PARENT_PROCESS before get_os on windows + * rust-launcher/src/os_access.rs: oon windows, without console, no window is enforced. Trait got win-only inside_console method + windows impl of os_access. Win module made public to expose AttachConsole function. + * rust-launcher/src/utils.rs: test logger got windows-only inside_console + 2019-03-12 Jiri Vanek Pre-release tuning diff -r fce279b33577 -r 9bacada77a23 rust-launcher/src/dirs_paths_helper.rs --- a/rust-launcher/src/dirs_paths_helper.rs Tue Mar 12 15:42:25 2019 +0100 +++ b/rust-launcher/src/dirs_paths_helper.rs Tue Mar 12 18:11:00 2019 +0100 @@ -108,7 +108,7 @@ #[cfg(windows)] fn get_os() -> os_access::Windows { - os_access::Windows::new(false, false) + os_access::Windows::new(false, false, true) } diff -r fce279b33577 -r 9bacada77a23 rust-launcher/src/main.rs --- a/rust-launcher/src/main.rs Tue Mar 12 15:42:25 2019 +0100 +++ b/rust-launcher/src/main.rs Tue Mar 12 18:11:00 2019 +0100 @@ -1,3 +1,5 @@ +#![windows_subsystem = "windows"] + mod hardcoded_paths; mod property_from_file; mod os_access; @@ -19,8 +21,8 @@ } #[cfg(windows)] -fn get_os(debug: bool, al: bool) -> os_access::Windows { - os_access::Windows::new(debug, al) +fn get_os(debug: bool, al: bool, ic: bool) -> os_access::Windows { + os_access::Windows::new(debug, al, ic) } fn is_debug_on() -> bool { @@ -29,7 +31,10 @@ return val; } _none => { + #[cfg(not(windows))] let os = get_os(false, false); + #[cfg(windows)] + let os = get_os(false, false, true); return property_from_files_resolver::try_main_verbose_from_properties(&os); } } @@ -74,7 +79,18 @@ } fn main() { - let os = get_os(is_debug_on(), true); + let os; + #[cfg(windows)] + { + use os_access::win; + let acr: i32; + unsafe { acr = win::AttachConsole(win::ATTACH_PARENT_PROCESS) }; + os = get_os(is_debug_on(), true, acr != 0); + } + #[cfg(not(windows))] + { + os = get_os(is_debug_on(), true); + } os.log(&dirs_paths_helper::path_to_string(&dirs_paths_helper::current_program())); let java_dir = utils::find_jre(&os); let mut info2 = String::new(); diff -r fce279b33577 -r 9bacada77a23 rust-launcher/src/os_access.rs --- a/rust-launcher/src/os_access.rs Tue Mar 12 15:42:25 2019 +0100 +++ b/rust-launcher/src/os_access.rs Tue Mar 12 18:11:00 2019 +0100 @@ -12,6 +12,13 @@ for ar in args.into_iter() { cmd.arg(ar); } + #[cfg(windows)] + { + if !os.inside_console() { + use std::os::windows::process::CommandExt; + cmd.creation_flags(win::CREATE_NO_WINDOW); + } + } let mut info = String::new(); write!(&mut info, "itw-rust-debug: command {}", format!("{:?}", cmd)).expect("unwrap failed"); os.log(&info); @@ -54,6 +61,8 @@ fn get_classpath_separator(&self) -> char; fn get_exec_suffixes(&self) -> &'static [&'static str]; fn is_verbose(&self) -> bool; + #[cfg(windows)] + fn inside_console(&self) -> bool; } #[cfg(not(windows))] @@ -188,15 +197,16 @@ pub struct Windows { verbose: bool, al: log_helper::AdvancedLogging, + ic: bool } #[cfg(windows)] impl Windows { - pub fn new(debug: bool, load_advanced: bool) -> Windows { + pub fn new(debug: bool, load_advanced: bool, ic: bool) -> Windows { if ! load_advanced { - Windows { verbose: debug, al: log_helper::AdvancedLogging::default() } + Windows { verbose: debug, al: log_helper::AdvancedLogging::default(), ic: ic } } else { - Windows { verbose: debug, al: log_helper::AdvancedLogging::load(&Windows::new(debug, false)) } + Windows { verbose: debug, al: log_helper::AdvancedLogging::load(&Windows::new(debug, false, ic)), ic: ic } } } } @@ -226,6 +236,10 @@ return self.verbose; } + fn inside_console(&self) -> bool { + return self.ic; + } + fn get_registry_java(&self) -> Option { std::panic::catch_unwind(|| { match win::java_registry_path() { @@ -293,7 +307,7 @@ #[cfg(windows)] #[allow(non_snake_case)] #[allow(non_camel_case_types)] -mod win { +pub mod win { // https://crates.io/crates/scopeguard macro_rules! defer { ($e:expr) => { @@ -347,6 +361,8 @@ use std::ptr::{null, null_mut}; // constants + pub const ATTACH_PARENT_PROCESS: c_ulong = 0xFFFFFFFF; + pub const CREATE_NO_WINDOW: c_ulong = 0x08000000; const CP_UTF8: c_ulong = 65001; const FORMAT_MESSAGE_ALLOCATE_BUFFER: c_ulong = 0x00000100; const FORMAT_MESSAGE_FROM_SYSTEM: c_ulong = 0x00001000; @@ -372,6 +388,8 @@ // function declarations extern "system" { + pub fn AttachConsole(dwProcessId: c_ulong) -> c_int; + fn MultiByteToWideChar( CodePage: c_uint, dwFlags: c_ulong, diff -r fce279b33577 -r 9bacada77a23 rust-launcher/src/utils.rs --- a/rust-launcher/src/utils.rs Tue Mar 12 15:42:25 2019 +0100 +++ b/rust-launcher/src/utils.rs Tue Mar 12 18:11:00 2019 +0100 @@ -241,6 +241,11 @@ return true; } + #[cfg(windows)] + fn inside_console(&self) -> bool { + return true; + } + fn log(&self, s: &str) { let ss = String::from(s); self.vec.borrow_mut().push(ss);