winapi - 如何将“宽”字符串读入缓冲区?
问题描述
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 函数的解决方案不会填充字符串
推荐阅读
- c# - 将数据插入 SQL 后显示消息未显示
- go - 在多行 Fyne Entry 小部件中捕获 Enter(更一般地,调用“父类”)
- javascript - getBoundingClientRect() 在 angular8 中没有给出函数错误
- python - 如何实现 django 模型到 graphene-django 类型的自定义映射?
- spring-boot - 如何在 Spring Cloud Data Flow 中注册 Spring 批处理
- javascript - Nodejs 导出聚合引发语法错误
- javascript - 我该如何延迟javascript执行?
- codenameone - 代号一个事件侦听器,用于再次在前台重新启动应用程序?
- sql - 将值设置为常量时应该添加 WHERE 吗?
- powershell - 如何使用 Powershell 向本地 Outlook 联系人添加证书?