Write a Rust wrapper for the Win32 API function GetWindowRect. Please use best practices…
- User
Write a Rust wrapper for the Win32 API function
GetWindowRect. Please use best practices while doing so. Assistant
Here's a Rust wrapper for the
GetWindowRectfunction using best practices:use std::os::windows::ffi::OsStrExt; use std::ffi::OsStr; use winapi::um::winnt::LPCWSTR; use winapi::um::winuser::{GetWindowRect, GetWindowRectW, RECT}; use winapi::shared::minwindef::{LPARAM, LONG}; pub struct WindowRect { pub left: LONG, pub top: LONG, pub right: LONG, pub bottom: LONG, } pub fn get_window_rect(hwnd: i32) -> Result<WindowRect, std::io::Error> { let window_rect = std::mem::zeroed(); unsafe { if GetWindowRectW(hwnd as _, &mut window_rect as _ as _) == 0 { let error_code = std::io::Error::from_raw_os_error( std::intrinsics::transmute::<_, i32>(GetLastError()), ); return Err(error_code); } } Ok(window_rect) } pub fn get_window_rect_with_params(hwnd: i32, lprc: Option<&mut RECT>, result_buffer: LPARAM) -> Result<u32, std::io::Error> { unsafe { let result = GetWindowRectW(hwnd as _, lprc.unwrap_or(&mut RECT {} as _) as _, result_buffer); if result == 0 { let error_code = std::io::Error::from_raw_os_error( std::intrinsics::transmute::<_, i32>(GetLastError()), ); return Err(error_code); } Ok(result) } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.