Trait WindowsApi
pub trait WindowsApi: Send + Sync {
Show 37 methods
// Required methods
fn set_console_title(&self, title: &str) -> Result<(), Error>;
fn get_console_title(&self, buffer: &mut [u16]) -> i32;
fn get_os_version(&self) -> String;
fn arrange_console(
&self,
x: i32,
y: i32,
width: i32,
height: i32,
) -> Result<(), Error>;
fn set_console_text_attribute(
&self,
attributes: CONSOLE_CHARACTER_ATTRIBUTES,
) -> Result<(), Error>;
fn get_console_screen_buffer_info(
&self,
) -> Result<CONSOLE_SCREEN_BUFFER_INFO, Error>;
fn fill_console_output_attribute(
&self,
attribute: u16,
length: u32,
coord: COORD,
) -> Result<u32, Error>;
fn scroll_console_screen_buffer(
&self,
scroll_rect: SMALL_RECT,
scroll_target: COORD,
fill_char: CHAR_INFO,
) -> Result<(), Error>;
fn set_console_cursor_position(&self, position: COORD) -> Result<(), Error>;
fn get_std_handle(&self, handle_type: STD_HANDLE) -> Result<HANDLE, Error>;
fn read_console_input(
&self,
buffer: &mut [INPUT_RECORD],
) -> Result<u32, Error>;
fn set_console_border_color(&self, color: &COLORREF) -> Result<(), Error>;
fn invalidate_console_window(&self) -> Result<(), Error>;
fn write_console_input(
&self,
buffer: &[INPUT_RECORD],
number_written: &mut u32,
) -> Result<(), Error>;
fn get_last_error(&self) -> u32;
fn interrupt_console_process_group(&self) -> Result<(), Error>;
fn install_console_ctrl_handler(&self) -> Result<(), Error>;
fn get_stdout_handle(&self) -> Result<HANDLE, Error>;
fn get_console_attached_process_count(&self) -> u32;
fn create_process_raw_wide(
&self,
application_wide: &[u16],
command_line: PWSTR,
startup_info: &mut STARTUPINFOW,
process_info: &mut PROCESS_INFORMATION,
) -> Result<(), Error>;
fn create_process_raw(
&self,
application: &str,
command_line: PWSTR,
startup_info: &mut STARTUPINFOW,
process_info: &mut PROCESS_INFORMATION,
) -> Result<(), Error>;
fn get_window_handle_for_process(&self, process_id: u32) -> HWND;
fn get_console_window(&self) -> HWND;
fn get_foreground_window(&self) -> HWND;
fn bring_window_to_top(
&self,
hwnd: HWND,
with_keyboard_focus: bool,
) -> Result<(), Error>;
fn get_console_mode(&self, handle: HANDLE) -> Result<CONSOLE_MODE, Error>;
fn set_console_mode(
&self,
handle: HANDLE,
mode: CONSOLE_MODE,
) -> Result<(), Error>;
fn get_exit_code(&self, handle: HANDLE) -> Result<u32, Error>;
fn move_window(
&self,
hwnd: HWND,
x: i32,
y: i32,
width: i32,
height: i32,
repaint: bool,
) -> Result<(), Error>;
fn get_window_placement(&self, hwnd: HWND) -> Result<WINDOWPLACEMENT, Error>;
fn show_window(
&self,
hwnd: HWND,
cmd_show: SHOW_WINDOW_CMD,
) -> Result<bool, Error>;
fn is_window(&self, hwnd: HWND) -> bool;
fn open_process(
&self,
access: u32,
inherit: bool,
process_id: u32,
) -> Result<HANDLE, Error>;
fn get_system_metrics(&self, index: SYSTEM_METRICS_INDEX) -> i32;
fn set_process_dpi_awareness(
&self,
value: PROCESS_DPI_AWARENESS,
) -> Result<(), Error>;
// Provided methods
fn create_process_with_args(
&self,
application: &str,
args: Vec<String>,
with_keyboard_focus: bool,
) -> Option<PROCESS_INFORMATION> { ... }
fn create_process_with_os_args(
&self,
application: &OsStr,
args: &[OsString],
with_keyboard_focus: bool,
) -> Result<PROCESS_INFORMATION, Error> { ... }
}Expand description
Trait for Windows API operations to enable mocking in tests.
This trait abstracts Windows API calls to allow for unit testing without actual system interaction. All console and system operations should go through this trait.
Required Methods§
Sourcefn set_console_title(&self, title: &str) -> Result<(), Error>
fn set_console_title(&self, title: &str) -> Result<(), Error>
Sourcefn get_console_title(&self, buffer: &mut [u16]) -> i32
fn get_console_title(&self, buffer: &mut [u16]) -> i32
Sourcefn get_os_version(&self) -> String
fn get_os_version(&self) -> String
Sourcefn arrange_console(
&self,
x: i32,
y: i32,
width: i32,
height: i32,
) -> Result<(), Error>
fn arrange_console( &self, x: i32, y: i32, width: i32, height: i32, ) -> Result<(), Error>
Arranges the console window position and size.
§Arguments
x- The x coordinate to move the window toy- The y coordinate to move the window towidth- The width in pixels to resize the window toheight- The height in pixels to resize the window to
§Returns
Result indicating success or failure of the operation
Sourcefn set_console_text_attribute(
&self,
attributes: CONSOLE_CHARACTER_ATTRIBUTES,
) -> Result<(), Error>
fn set_console_text_attribute( &self, attributes: CONSOLE_CHARACTER_ATTRIBUTES, ) -> Result<(), Error>
Sourcefn get_console_screen_buffer_info(
&self,
) -> Result<CONSOLE_SCREEN_BUFFER_INFO, Error>
fn get_console_screen_buffer_info( &self, ) -> Result<CONSOLE_SCREEN_BUFFER_INFO, Error>
Sourcefn fill_console_output_attribute(
&self,
attribute: u16,
length: u32,
coord: COORD,
) -> Result<u32, Error>
fn fill_console_output_attribute( &self, attribute: u16, length: u32, coord: COORD, ) -> Result<u32, Error>
Sourcefn scroll_console_screen_buffer(
&self,
scroll_rect: SMALL_RECT,
scroll_target: COORD,
fill_char: CHAR_INFO,
) -> Result<(), Error>
fn scroll_console_screen_buffer( &self, scroll_rect: SMALL_RECT, scroll_target: COORD, fill_char: CHAR_INFO, ) -> Result<(), Error>
Sourcefn set_console_cursor_position(&self, position: COORD) -> Result<(), Error>
fn set_console_cursor_position(&self, position: COORD) -> Result<(), Error>
Sourcefn get_std_handle(&self, handle_type: STD_HANDLE) -> Result<HANDLE, Error>
fn get_std_handle(&self, handle_type: STD_HANDLE) -> Result<HANDLE, Error>
Sourcefn read_console_input(&self, buffer: &mut [INPUT_RECORD]) -> Result<u32, Error>
fn read_console_input(&self, buffer: &mut [INPUT_RECORD]) -> Result<u32, Error>
Sourcefn set_console_border_color(&self, color: &COLORREF) -> Result<(), Error>
fn set_console_border_color(&self, color: &COLORREF) -> Result<(), Error>
Sourcefn invalidate_console_window(&self) -> Result<(), Error>
fn invalidate_console_window(&self) -> Result<(), Error>
Marks the entire console window client area as needing a redraw.
Used to nudge the legacy Win10 conhost into repainting from its own buffer state after bulk attribute changes that the renderer otherwise leaves stale on the trailing row/column.
§Returns
Result indicating success or failure of the operation
Sourcefn write_console_input(
&self,
buffer: &[INPUT_RECORD],
number_written: &mut u32,
) -> Result<(), Error>
fn write_console_input( &self, buffer: &[INPUT_RECORD], number_written: &mut u32, ) -> Result<(), Error>
Sourcefn get_last_error(&self) -> u32
fn get_last_error(&self) -> u32
Sourcefn interrupt_console_process_group(&self) -> Result<(), Error>
fn interrupt_console_process_group(&self) -> Result<(), Error>
Interrupt every process attached to the caller’s console.
Sends CTRL_C_EVENT to process group 0 so a Ctrl+C relayed from the
daemon reaches the child exactly as a focused Ctrl+C would.
CTRL_BREAK_EVENT is not interchangeable: many programs treat only
Ctrl+C as the interrupt (e.g. ping stops on Ctrl+C but merely prints
statistics on Ctrl+Break). The caller shields itself with the handler
from Self::install_console_ctrl_handler, since group 0 signals it too.
https://learn.microsoft.com/en-us/windows/console/generateconsolectrlevent
§Returns
Result indicating success or failure of the operation
Sourcefn install_console_ctrl_handler(&self) -> Result<(), Error>
fn install_console_ctrl_handler(&self) -> Result<(), Error>
Install a console control handler that shields this process from CTRL+C and CTRL+Break.
The handler reports both CTRL_C_EVENT and CTRL_BREAK_EVENT as
handled, so neither user-typed nor group-relayed signals terminate the
calling process; other control events keep their default handling.
§Returns
Result indicating success or failure of the operation
Sourcefn get_stdout_handle(&self) -> Result<HANDLE, Error>
fn get_stdout_handle(&self) -> Result<HANDLE, Error>
Sourcefn get_console_attached_process_count(&self) -> u32
fn get_console_attached_process_count(&self) -> u32
Sourcefn create_process_raw_wide(
&self,
application_wide: &[u16],
command_line: PWSTR,
startup_info: &mut STARTUPINFOW,
process_info: &mut PROCESS_INFORMATION,
) -> Result<(), Error>
fn create_process_raw_wide( &self, application_wide: &[u16], command_line: PWSTR, startup_info: &mut STARTUPINFOW, process_info: &mut PROCESS_INFORMATION, ) -> Result<(), Error>
Low-level CreateProcessW call accepting an already-wide,
null-terminated application path.
§Arguments
application_wide- UTF-16, null-terminated application path.command_line- Mutable UTF-16 command line asPWSTR.startup_info- Startup information structure.process_info- Output process information.
§Returns
Result indicating success or failure of the operation.
Sourcefn create_process_raw(
&self,
application: &str,
command_line: PWSTR,
startup_info: &mut STARTUPINFOW,
process_info: &mut PROCESS_INFORMATION,
) -> Result<(), Error>
fn create_process_raw( &self, application: &str, command_line: PWSTR, startup_info: &mut STARTUPINFOW, process_info: &mut PROCESS_INFORMATION, ) -> Result<(), Error>
Sourcefn get_window_handle_for_process(&self, process_id: u32) -> HWND
fn get_window_handle_for_process(&self, process_id: u32) -> HWND
Sourcefn get_console_window(&self) -> HWND
fn get_console_window(&self) -> HWND
Sourcefn get_foreground_window(&self) -> HWND
fn get_foreground_window(&self) -> HWND
Sourcefn bring_window_to_top(
&self,
hwnd: HWND,
with_keyboard_focus: bool,
) -> Result<(), Error>
fn bring_window_to_top( &self, hwnd: HWND, with_keyboard_focus: bool, ) -> Result<(), Error>
Bring hwnd to the top of the z-order.
When with_keyboard_focus is true the window is also activated and
receives keyboard focus. When false the window is raised without
activation, avoiding the taskbar flash that would happen if the
window is not the current input target.
§Arguments
hwnd- Handle to the window to raise.with_keyboard_focus- Whether to activate the window and give it keyboard focus.
§Returns
Result indicating success or failure of the operation
Sourcefn get_console_mode(&self, handle: HANDLE) -> Result<CONSOLE_MODE, Error>
fn get_console_mode(&self, handle: HANDLE) -> Result<CONSOLE_MODE, Error>
Sourcefn set_console_mode(
&self,
handle: HANDLE,
mode: CONSOLE_MODE,
) -> Result<(), Error>
fn set_console_mode( &self, handle: HANDLE, mode: CONSOLE_MODE, ) -> Result<(), Error>
Sourcefn get_exit_code(&self, handle: HANDLE) -> Result<u32, Error>
fn get_exit_code(&self, handle: HANDLE) -> Result<u32, Error>
Sourcefn move_window(
&self,
hwnd: HWND,
x: i32,
y: i32,
width: i32,
height: i32,
repaint: bool,
) -> Result<(), Error>
fn move_window( &self, hwnd: HWND, x: i32, y: i32, width: i32, height: i32, repaint: bool, ) -> Result<(), Error>
Sourcefn get_window_placement(&self, hwnd: HWND) -> Result<WINDOWPLACEMENT, Error>
fn get_window_placement(&self, hwnd: HWND) -> Result<WINDOWPLACEMENT, Error>
Sourcefn show_window(
&self,
hwnd: HWND,
cmd_show: SHOW_WINDOW_CMD,
) -> Result<bool, Error>
fn show_window( &self, hwnd: HWND, cmd_show: SHOW_WINDOW_CMD, ) -> Result<bool, Error>
Sourcefn open_process(
&self,
access: u32,
inherit: bool,
process_id: u32,
) -> Result<HANDLE, Error>
fn open_process( &self, access: u32, inherit: bool, process_id: u32, ) -> Result<HANDLE, Error>
Sourcefn get_system_metrics(&self, index: SYSTEM_METRICS_INDEX) -> i32
fn get_system_metrics(&self, index: SYSTEM_METRICS_INDEX) -> i32
Provided Methods§
Sourcefn create_process_with_args(
&self,
application: &str,
args: Vec<String>,
with_keyboard_focus: bool,
) -> Option<PROCESS_INFORMATION>
fn create_process_with_args( &self, application: &str, args: Vec<String>, with_keyboard_focus: bool, ) -> Option<PROCESS_INFORMATION>
Create a new process attached to its own console window.
When with_keyboard_focus is false the new console window is
shown without activation (STARTF_USESHOWWINDOW +
SW_SHOWNOACTIVATE), so the caller retains keyboard focus.
Used when the daemon spawns client consoles - otherwise the
last-spawned client wins the foreground and Windows refuses to
let the daemon steal it back.
§Arguments
application- Application name including file extensionargs- List of arguments to the applicationwith_keyboard_focus- Whether the new console window should take foreground focus when it appears.
§Returns
Process information if successful, None otherwise
Sourcefn create_process_with_os_args(
&self,
application: &OsStr,
args: &[OsString],
with_keyboard_focus: bool,
) -> Result<PROCESS_INFORMATION, Error>
fn create_process_with_os_args( &self, application: &OsStr, args: &[OsString], with_keyboard_focus: bool, ) -> Result<PROCESS_INFORMATION, Error>
Create a new process from OsStr/OsString inputs without lossy
UTF-8 conversion.
Mirrors Self::create_process_with_args but preserves the
platform-native UTF-16 representation of application and args.
Use this from code that hands paths or user-supplied arguments
straight to the spawner (e.g. the platform-trait
ProcessSpawner::spawn) - non-UTF-8 sequences are passed through
to CreateProcessW unmodified instead of being replaced with
U+FFFD.
§Arguments
application- Application path or name.args- Arguments to the application.with_keyboard_focus- Whether the new console window should take foreground focus when it appears.
§Returns
Process information on success, or the originating
[windows::core::Error] from CreateProcessW.