1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#![allow(non_camel_case_types, non_snake_case)]
use crate::co;
use crate::decl::*;
use crate::guard::*;
use crate::kernel::{ffi, privs::*};
use crate::prelude::*;
handle! { HSTD;
/// Handle to a
/// [standard device](https://learn.microsoft.com/en-us/windows/console/getstdhandle).
/// Originally just a `HANDLE`.
}
impl HSTD {
/// [`FlushConsoleInputBuffer`](https://learn.microsoft.com/en-us/windows/console/flushconsoleinputbuffer)
/// function.
pub fn FlushConsoleInputBuffer(&self) -> SysResult<()> {
BoolRet(unsafe { ffi::FlushConsoleInputBuffer(self.ptr()) }).to_sysresult()
}
/// [`GetConsoleMode`](https://learn.microsoft.com/en-us/windows/console/getconsolemode)
/// function.
#[must_use]
pub fn GetConsoleMode(&self) -> SysResult<co::CONSOLE> {
let mut mode = co::CONSOLE::default();
BoolRet(unsafe { ffi::GetConsoleMode(self.ptr(), mode.as_mut()) })
.to_sysresult()
.map(|_| mode)
}
/// [`GetStdHandle`](https://learn.microsoft.com/en-us/windows/console/getstdhandle)
/// function.
#[must_use]
pub fn GetStdHandle(std_handle: co::STD_HANDLE) -> SysResult<CloseHandleGuard<HSTD>> {
unsafe {
match HSTD::from_ptr(ffi::GetStdHandle(std_handle.raw())) {
HSTD::INVALID => Err(GetLastError()),
handle => Ok(CloseHandleGuard::new(handle)),
}
}
}
/// [`ReadConsole`](https://learn.microsoft.com/en-us/windows/console/readconsole)
/// function.
///
/// Returns the number of chars actually written.
///
/// # Examples
///
/// ```no_run
/// use winsafe::{self as w, prelude::*, co};
///
/// let hstd = w::HSTD::GetStdHandle(co::STD_HANDLE::INPUT)?;
///
/// let mut buffer = w::WString::new_alloc_buf(2048);
/// hstd.ReadConsole(&mut buffer, None)?;
///
/// let text = buffer.to_string();
/// # w::SysResult::Ok(())
/// ```
#[must_use]
pub fn ReadConsole(
&self,
buffer: &mut WString,
input_control: Option<&CONSOLE_READCONSOLE_CONTROL>,
) -> SysResult<u32> {
let mut num_read = 0u32;
BoolRet(unsafe {
ffi::ReadConsoleW(
self.ptr(),
buffer.as_mut_ptr() as _,
buffer.buf_len() as _,
&mut num_read,
pcvoid_or_null(input_control),
)
})
.to_sysresult()
.map(|_| num_read)
}
/// [`ReadFile`](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile)
/// function.
///
/// Returns the number of bytes read.
///
/// Note that asynchronous reading – which use the
/// [`OVERLAPPED`](crate::OVERLAPPED) struct – is not currently supported by
/// this method, because the buffer must remain untouched until the async
/// operation is complete, thus making the method unsound.
pub fn ReadFile(&self, buffer: &mut [u8]) -> SysResult<u32> {
unsafe { HFILE::from_ptr(self.ptr()) }.ReadFile(buffer)
}
/// [`SetConsoleMode`](https://learn.microsoft.com/en-us/windows/console/setconsolemode)
/// function.
pub fn SetConsoleMode(&self, mode: co::CONSOLE) -> SysResult<()> {
BoolRet(unsafe { ffi::SetConsoleMode(self.ptr(), mode.raw()) }).to_sysresult()
}
/// [`WriteConsole`](https://learn.microsoft.com/en-us/windows/console/writeconsole)
/// function.
///
/// Returns the number of chars actually written.
pub fn WriteConsole(&self, text: &str) -> SysResult<u32> {
let buf = WString::from_str(text);
let mut num_written = 0u32;
BoolRet(unsafe {
ffi::WriteConsoleW(
self.ptr(),
buf.as_ptr() as _,
buf.str_len() as _,
&mut num_written,
std::ptr::null_mut(),
)
})
.to_sysresult()
.map(|_| num_written)
}
/// [`WriteFile`](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile)
/// function.
///
/// Returns the number of bytes written.
///
/// Note that asynchronous writing – which use the
/// [`OVERLAPPED`](crate::OVERLAPPED) struct – is not currently supported by
/// this method, because the buffer must remain untouched until the async
/// operation is complete, thus making the method unsound.
pub fn WriteFile(&self, data: &[u8]) -> SysResult<u32> {
unsafe { HFILE::from_ptr(self.ptr()) }.WriteFile(data)
}
}