Skip to content

Commit f29cb3d

Browse files
desktop: Only attach to parent console on Windows if stdout is not a pipe or a regular file
1 parent c357a96 commit f29cb3d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

desktop/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async-task = "4.7.1"
6060
ashpd = "0.11.0"
6161

6262
[target.'cfg(windows)'.dependencies]
63-
windows-sys = { version = "0.61.2", features = ["Win32_System_Console"] }
63+
windows-sys = { version = "0.61.2", features = ["Win32_System_Console", "Win32_Storage_FileSystem"] }
6464

6565
[build-dependencies]
6666
embed-resource = "3"

desktop/src/main.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,32 @@ fn init() {
6161
// When linked with the windows subsystem windows won't automatically attach
6262
// to the console of the parent process, so we do it explicitly. This fails
6363
// silently if the parent has no console.
64+
//
65+
// However, if stdout/stderr are already redirected (e.g., `ruffle.exe > file.txt`),
66+
// we should NOT attach to the console as that would bypass the redirection.
67+
// See: https://github.com/ruffle-rs/ruffle/issues/9145
6468
#[cfg(windows)]
6569
unsafe {
66-
use windows_sys::Win32::System::Console::{ATTACH_PARENT_PROCESS, AttachConsole};
67-
AttachConsole(ATTACH_PARENT_PROCESS);
70+
use windows_sys::Win32::Storage::FileSystem::{
71+
FILE_TYPE_DISK, FILE_TYPE_PIPE, GetFileType,
72+
};
73+
use windows_sys::Win32::System::Console::{
74+
ATTACH_PARENT_PROCESS, AttachConsole, GetStdHandle, STD_OUTPUT_HANDLE,
75+
};
76+
77+
// Check if stdout is already redirected to a file or pipe
78+
let stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
79+
let file_type = GetFileType(stdout_handle);
80+
81+
match file_type {
82+
// If output is redirected to a file or pipe, don't attach to console
83+
// as that would bypass the redirection
84+
FILE_TYPE_DISK | FILE_TYPE_PIPE => {}
85+
// Otherwise, attach to parent console for interactive use
86+
_ => {
87+
AttachConsole(ATTACH_PARENT_PROCESS);
88+
}
89+
}
6890
}
6991

7092
let prev_hook = std::panic::take_hook();

0 commit comments

Comments
 (0)