Last active
July 24, 2019 19:21
-
-
Save MaulingMonkey/8fe24b1e6e2b589d8b5e4e47150efb47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] extern crate lazy_static; | |
use winapi::shared::minwindef::*; | |
use winapi::um::consoleapi::*; | |
use winapi::um::winnt::*; | |
use std::fs::*; | |
use std::io::Error; | |
use std::os::windows::io::*; | |
pub unsafe trait AsConsoleHandle { | |
fn as_console_handle(&self) -> HANDLE; | |
} | |
struct ConsoleHandle(File); | |
lazy_static! { | |
static ref CONIN : Result<ConsoleHandle, Error> = File::open("CONIN$").map(|f| ConsoleHandle(f)); | |
static ref CONOUT : Result<ConsoleHandle, Error> = OpenOptions::new().read(true).write(true).open("CONOUT$").map(|f| ConsoleHandle(f)); | |
} | |
impl ConsoleHandle { | |
pub fn conin() -> Result<&'static ConsoleHandle, &'static Error> { CONIN .as_ref() } | |
pub fn conout() -> Result<&'static ConsoleHandle, &'static Error> { CONOUT.as_ref() } | |
} | |
unsafe impl AsConsoleHandle for ConsoleHandle { | |
fn as_console_handle(&self) -> HANDLE { self.0.as_raw_handle() } | |
} | |
pub fn get_console_mode(console: &impl AsConsoleHandle) -> Result<DWORD, Error> { | |
let console = console.as_console_handle(); | |
let mut mode = 0; | |
match unsafe { GetConsoleMode(console, &mut mode) } { | |
FALSE => Err(Error::last_os_error()), | |
_ => Ok(mode), | |
} | |
} | |
fn main() { | |
dbg!(get_console_mode(ConsoleHandle::conin() .unwrap())); // Ok(0x1F7) | |
dbg!(get_console_mode(ConsoleHandle::conout().unwrap())); // Ok( 0x7) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment