Windows API 具有GetWindowTextW
将字符串读入您提供的缓冲区的功能。如何提供这样的缓冲区,然后在 Rust 中从中读取字符串?
PS 与此问题相比,Calling the GetUserName WinAPI function with a mutable string 不会填充字符串, 此问题一般侧重于 Windows 上的字符串读取,而不是特定问题。此外,这个问题有望通过关键字轻松搜索到,回答所要求的内容。
Windows API 具有GetWindowTextW
将字符串读入您提供的缓冲区的功能。如何提供这样的缓冲区,然后在 Rust 中从中读取字符串?
PS 与此问题相比,Calling the GetUserName WinAPI function with a mutable string 不会填充字符串, 此问题一般侧重于 Windows 上的字符串读取,而不是特定问题。此外,这个问题有望通过关键字轻松搜索到,回答所要求的内容。
例子:
#[cfg(target_os = "windows")]
fn get_window_name(max_size: i32) -> String {
let mut vec = Vec::with_capacity(max_size as usize);
unsafe {
let hwnd = user32::GetForegroundWindow();
let err_code = user32::GetWindowTextW(hwnd, vec.as_mut_ptr(), max_size);
assert!(err_code != 0);
assert!(vec.capacity() >= max_size as usize);
vec.set_len(max_size as usize);
};
String::from_utf16(&vec).unwrap()
}
使用可变字符串调用 GetUserName WinAPI 函数的解决方案不会填充字符串