WindowsApi

Trait WindowsApi 

pub trait WindowsApi: Send + Sync {
Show 35 methods // Required methods fn set_console_title(&self, title: &str) -> Result<()>; 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<()>; fn set_console_text_attribute( &self, attributes: CONSOLE_CHARACTER_ATTRIBUTES, ) -> Result<()>; fn get_console_screen_buffer_info( &self, ) -> Result<CONSOLE_SCREEN_BUFFER_INFO>; fn fill_console_output_attribute( &self, attribute: u16, length: u32, coord: COORD, ) -> Result<u32>; fn scroll_console_screen_buffer( &self, scroll_rect: SMALL_RECT, scroll_target: COORD, fill_char: CHAR_INFO, ) -> Result<()>; fn set_console_cursor_position(&self, position: COORD) -> Result<()>; fn get_std_handle(&self, handle_type: STD_HANDLE) -> Result<HANDLE>; fn read_console_input(&self, buffer: &mut [INPUT_RECORD]) -> Result<u32>; fn set_console_border_color(&self, color: &COLORREF) -> Result<()>; fn write_console_input( &self, buffer: &[INPUT_RECORD], number_written: &mut u32, ) -> Result<()>; fn get_last_error(&self) -> u32; fn generate_console_ctrl_event( &self, ctrl_event: u32, process_group_id: u32, ) -> Result<()>; fn get_stdout_handle(&self) -> Result<HANDLE>; fn get_console_screen_buffer_info_with_handle( &self, handle: HANDLE, ) -> Result<CONSOLE_SCREEN_BUFFER_INFO>; fn create_process_raw( &self, application: &str, command_line: PWSTR, startup_info: &mut STARTUPINFOW, process_info: &mut PROCESS_INFORMATION, ) -> Result<()>; fn get_window_handle_for_process(&self, process_id: u32) -> HWND; fn get_console_window(&self) -> HWND; fn get_foreground_window(&self) -> HWND; fn set_foreground_window(&self, hwnd: HWND) -> Result<()>; fn get_console_mode(&self, handle: HANDLE) -> Result<CONSOLE_MODE>; fn set_console_mode(&self, handle: HANDLE, mode: CONSOLE_MODE) -> Result<()>; fn get_exit_code(&self, handle: HANDLE) -> Result<u32>; fn move_window( &self, hwnd: HWND, x: i32, y: i32, width: i32, height: i32, repaint: bool, ) -> Result<()>; fn get_window_placement(&self, hwnd: HWND) -> Result<WINDOWPLACEMENT>; fn show_window(&self, hwnd: HWND, cmd_show: SHOW_WINDOW_CMD) -> Result<bool>; fn focus_window_with_automation(&self, hwnd: HWND) -> Result<()>; fn is_window(&self, hwnd: HWND) -> bool; fn open_process( &self, access: u32, inherit: bool, process_id: u32, ) -> Result<HANDLE>; fn initialize_com_library(&self, coinit: COINIT) -> Result<()>; fn get_system_metrics(&self, index: SYSTEM_METRICS_INDEX) -> i32; fn set_process_dpi_awareness( &self, value: PROCESS_DPI_AWARENESS, ) -> Result<()>; // Provided method fn create_process_with_args( &self, application: &str, args: Vec<String>, ) -> Option<PROCESS_INFORMATION> { ... }
}
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§

fn set_console_title(&self, title: &str) -> Result<()>

Sets the console window title.

§Arguments
  • title - The string to be set as window title
§Returns

Result indicating success or failure of the operation

fn get_console_title(&self, buffer: &mut [u16]) -> i32

Gets the console window title as UTF-16 buffer.

§Arguments
  • buffer - Mutable buffer to store the UTF-16 encoded title
§Returns

Number of characters copied to the buffer

fn get_os_version(&self) -> String

Gets OS version string.

§Returns

String representation of the OS version

fn arrange_console(&self, x: i32, y: i32, width: i32, height: i32) -> Result<()>

Arranges the console window position and size.

§Arguments
  • x - The x coordinate to move the window to
  • y - The y coordinate to move the window to
  • width - The width in pixels to resize the window to
  • height - The height in pixels to resize the window to
§Returns

Result indicating success or failure of the operation

fn set_console_text_attribute( &self, attributes: CONSOLE_CHARACTER_ATTRIBUTES, ) -> Result<()>

Sets console text attribute.

§Arguments
  • attributes - Console character attributes to set
§Returns

Result indicating success or failure of the operation

fn get_console_screen_buffer_info(&self) -> Result<CONSOLE_SCREEN_BUFFER_INFO>

Gets console screen buffer info.

§Returns

Console screen buffer information or error

fn fill_console_output_attribute( &self, attribute: u16, length: u32, coord: COORD, ) -> Result<u32>

Fills console output with specified attribute.

§Arguments
  • attribute - Attribute to fill with
  • length - Number of characters to fill
  • coord - Starting coordinate
§Returns

Number of characters actually filled or error

fn scroll_console_screen_buffer( &self, scroll_rect: SMALL_RECT, scroll_target: COORD, fill_char: CHAR_INFO, ) -> Result<()>

Scrolls console screen buffer.

§Arguments
  • scroll_rect - Rectangle to scroll
  • scroll_target - Target coordinate for scrolling
  • fill_char - Character to fill empty space with
§Returns

Result indicating success or failure of the operation

fn set_console_cursor_position(&self, position: COORD) -> Result<()>

Sets console cursor position.

§Arguments
  • position - New cursor position
§Returns

Result indicating success or failure of the operation

fn get_std_handle(&self, handle_type: STD_HANDLE) -> Result<HANDLE>

Gets standard handle.

§Arguments
  • handle_type - Type of standard handle to retrieve
§Returns

Handle to the requested standard device or error

fn read_console_input(&self, buffer: &mut [INPUT_RECORD]) -> Result<u32>

Reads console input.

§Arguments
  • buffer - Buffer to store input records
§Returns

Number of records read or error

fn set_console_border_color(&self, color: &COLORREF) -> Result<()>

Sets DWM window attribute for border color.

§Arguments
  • color - Color to set as border color
§Returns

Result indicating success or failure of the operation

fn write_console_input( &self, buffer: &[INPUT_RECORD], number_written: &mut u32, ) -> Result<()>

Writes input records to the console input buffer.

§Arguments
  • buffer - Input records to write
  • number_written - Mutable reference to store number of records written
§Returns

Result indicating success or failure of the operation

fn get_last_error(&self) -> u32

Gets the last Windows error code.

§Returns

The last error code from Windows API

fn generate_console_ctrl_event( &self, ctrl_event: u32, process_group_id: u32, ) -> Result<()>

Generates a console control event.

§Arguments
  • ctrl_event - Control event type to generate
  • process_group_id - Process group ID to send event to
§Returns

Result indicating success or failure of the operation

fn get_stdout_handle(&self) -> Result<HANDLE>

Get standard output handle

§Returns

Handle to standard output or error

fn get_console_screen_buffer_info_with_handle( &self, handle: HANDLE, ) -> Result<CONSOLE_SCREEN_BUFFER_INFO>

Get console screen buffer information

§Arguments
  • handle - Handle to console screen buffer
§Returns

Console screen buffer information or error

fn create_process_raw( &self, application: &str, command_line: PWSTR, startup_info: &mut STARTUPINFOW, process_info: &mut PROCESS_INFORMATION, ) -> Result<()>

Low-level process creation API call

§Arguments
  • application - Application name
  • command_line - Command line string as PWSTR
  • startup_info - Startup information structure
  • process_info - Process information structure to fill
§Returns

Result indicating success or failure of the operation

fn get_window_handle_for_process(&self, process_id: u32) -> HWND

Get window handle for process ID

§Arguments
  • process_id - Process ID to find window for
§Returns

Window handle for the process

fn get_console_window(&self) -> HWND

Gets the console window handle.

§Returns

Handle to the console window

fn get_foreground_window(&self) -> HWND

Gets the foreground window handle.

§Returns

Handle to the foreground window

fn set_foreground_window(&self, hwnd: HWND) -> Result<()>

Sets the foreground window.

§Arguments
  • hwnd - Handle to the window to set as foreground
§Returns

Result indicating success or failure of the operation

fn get_console_mode(&self, handle: HANDLE) -> Result<CONSOLE_MODE>

Gets console mode for the specified handle.

§Arguments
  • handle - Handle to the console input buffer
§Returns

Console mode or error

fn set_console_mode(&self, handle: HANDLE, mode: CONSOLE_MODE) -> Result<()>

Sets console mode for the specified handle.

§Arguments
  • handle - Handle to the console input buffer
  • mode - Console mode to set
§Returns

Result indicating success or failure of the operation

fn get_exit_code(&self, handle: HANDLE) -> Result<u32>

Gets the exit code of the specified process.

§Arguments
  • handle - Handle to the process
§Returns

Exit code or error

fn move_window( &self, hwnd: HWND, x: i32, y: i32, width: i32, height: i32, repaint: bool, ) -> Result<()>

Moves and resizes a window.

§Arguments
  • hwnd - Handle to the window
  • x - New x position
  • y - New y position
  • width - New width
  • height - New height
  • repaint - Whether to repaint the window
§Returns

Result indicating success or failure of the operation

fn get_window_placement(&self, hwnd: HWND) -> Result<WINDOWPLACEMENT>

Gets window placement information.

§Arguments
  • hwnd - Handle to the window
§Returns

Window placement information or error

fn show_window(&self, hwnd: HWND, cmd_show: SHOW_WINDOW_CMD) -> Result<bool>

Shows a window in the specified state.

§Arguments
  • hwnd - Handle to the window
  • cmd_show - Show command
§Returns

Result indicating success or failure of the operation

fn focus_window_with_automation(&self, hwnd: HWND) -> Result<()>

Focuses a window using UI Automation.

§Arguments
  • hwnd - Handle to the window to focus
§Returns

Result indicating success or failure of the operation

fn is_window(&self, hwnd: HWND) -> bool

Checks if a window handle is valid.

§Arguments
  • hwnd - Handle to the window to check
§Returns

True if the window is valid, false otherwise

fn open_process( &self, access: u32, inherit: bool, process_id: u32, ) -> Result<HANDLE>

Opens a process with the specified access rights.

§Arguments
  • access - Access rights for the process handle
  • inherit - Whether the handle can be inherited
  • process_id - Process ID to open
§Returns

Process handle or error

fn initialize_com_library(&self, coinit: COINIT) -> Result<()>

Initializes the COM library for use by the calling thread.

§Arguments
  • coinit - Initialization options for the COM library
§Returns

Result indicating success or failure of the operation

fn get_system_metrics(&self, index: SYSTEM_METRICS_INDEX) -> i32

Gets system metrics information.

§Arguments
  • index - System metric index to retrieve
§Returns

The requested system metric value

fn set_process_dpi_awareness(&self, value: PROCESS_DPI_AWARENESS) -> Result<()>

Sets the process DPI awareness.

§Arguments
  • value - DPI awareness value to set
§Returns

Result indicating success or failure of the operation

Provided Methods§

fn create_process_with_args( &self, application: &str, args: Vec<String>, ) -> Option<PROCESS_INFORMATION>

Create a new process

§Arguments
  • application - Application name including file extension
  • args - List of arguments to the application
§Returns

Process information if successful, None otherwise

Implementors§