-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run Hidden (On Windows) #100
Comments
Thanks for reporting! I don't know much about Windows myself. What would be the drawback of |
Well, the normal action of a terminal application being run from the GUI on Windows is to open a console and close the console immediately when the application completes. So for most console applications on Windows, this means the console will flash on the screen and disappear a second later if you tried to double-click their binaries from the file explorer. In Rust a way around this that I know is to specify using the windows subsystem (typically used for GUI applications under Rust to make the console not show), and then reattach the console allowing console output to still display if run from Windows command prompt but nothing to show if run directly from explorer. This might be the most appropriate approach for rust-script on Windows. Here's a working example. Maybe someone else can comment on the efficacy or shortcomings of this method but I've never had an issue with it. (I'm running Windows 11 just to be clear) cargo.toml [target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["wincon"] } main.rs #![windows_subsystem = "windows"]
fn main() {
#[cfg(target_os = "windows")]
{
use winapi::um::wincon::{AttachConsole, ATTACH_PARENT_PROCESS};
unsafe {
AttachConsole(ATTACH_PARENT_PROCESS);
}
}
println!("Hello, world!");
} |
@wintersnare Thanks a lot! I added |
20230417190129.mp4I think I've attached a video demonstrating this. The [Test 1] Using the executable included with the crate, it works normally from the console. [Test 2] The second test is run from the #101 build. [Test 3] The third test is executing the compiled script executable directly from the rust-script cache directory. Test 1 and 3 work as expected. In my use cases Python on Windows handles this by providing 2 executables. So maybe inspiration can be taken from that. |
@wintersnare Thanks for all this helpful information! Perhaps the python way - distributing two binaries, and using two file extensions - is the best way then? |
@fornwall Yes, I think this would be the best way for Windows environments. I was thinking though that in the event of an error, it should probably write a unique The environment variable in Windows for the user's temp directory is |
Normally running Rust applications with
#![windows_subsystem = "windows"]
effectively hides the console window.It would be nice if there were some config for rust-script or an automated way to detect scripts containing this line and ensure that rust-script itself doesn't create a console window to run the application.
I feel this is important when running automated scripts from Task Scheduler or other scripts. And I think rust-script shouldn't necessarily be popping up a new console window each time when it's undesired that it does so. Since new windows are Focus grabbing and can interfere with other things you may be doing, it would be nice to have an official way around this.
The text was updated successfully, but these errors were encountered: