Skip to content

Commit

Permalink
change ~ increase windows early version compatibility
Browse files Browse the repository at this point in the history
- use `CreateFileW` (usable in WinXP+) instead of `CreateFile2` (only usable in Win8+)
  • Loading branch information
rivy committed Jul 20, 2019
1 parent ed28592 commit a4e42b7
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ pub fn enable_ansi_support() -> Result<(), u32> {
use std::ptr::null_mut;
use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode};
use winapi::um::errhandlingapi::GetLastError;
use winapi::um::fileapi::{CreateFile2, OPEN_EXISTING};
use winapi::um::fileapi::{CreateFileW, OPEN_EXISTING};
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
use winapi::um::winnt::{FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE};

const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;

unsafe {
// ref: https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfile2
// Using `CreateFile2("CONOUT$", ...)` to retrieve the console handle works correctly even if STDOUT and/or STDERR are redirected
// ref: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
// Using `CreateFileW("CONOUT$", ...)` to retrieve the console handle works correctly even if STDOUT and/or STDERR are redirected
let console_out_name: Vec<u16> = OsStr::new("CONOUT$").encode_wide().chain(once(0)).collect();
let console_handle = CreateFile2(
let console_handle = CreateFileW(
console_out_name.as_ptr(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
null_mut(),
OPEN_EXISTING,
0,
null_mut(),
);
if console_handle == INVALID_HANDLE_VALUE
Expand Down

0 comments on commit a4e42b7

Please sign in to comment.