Lines
0 %
Functions
Branches
100 %
//! Helper functions for handling the Windows console from a GUI context.
//!
//! Windows subsystem applications must explicitly attach to an existing console
//! before stdio works, and if not available, create their own if they wish to
//! print anything.
//! These functions enable that, primarily for the purposes of displaying Rust
//! panics.
use std::error::Error;
use std::result::Result;
#[cfg(windows)]
use winapi::um::consoleapi::AllocConsole;
use winapi::um::wincon::AttachConsole;
use winapi::um::wincon::FreeConsole;
use winapi::um::wincon::GetConsoleWindow;
use winapi::um::wincon::ATTACH_PARENT_PROCESS;
pub struct Console {
attached: bool,
}
/// Initialises the console. On Windows this either attaches to the
pub fn init() -> Result<Console, Box<dyn Error>> {
unsafe {
// Check if we're attached to an existing Windows console
if GetConsoleWindow().is_null() {
// Try to attach to an existing Windows console.
//
// It's normally a no-brainer to call this - it just makes println! and friends
// work as expected, without cluttering the screen with a console in the general
// case.
if AttachConsole(ATTACH_PARENT_PROCESS) == 0 {
// Try to attach to a console, and if not, allocate ourselves a new one.
if AllocConsole() != 0 {
Ok(Console { attached: false })
} else {
Err("Failed to attach to a console, and to create one".into())
// We attached to an existing console.
Ok(Console { attached: true })
// The program was started with a console attached.
#[cfg(not(windows))]
{
Ok(Console {})
impl Drop for Console {
fn drop(&mut self) {
// Free the allocated console, when it was not attached.
if !self.attached {
unsafe { FreeConsole() };