@@ -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