我正在尝试使用
获取所有窗口的名称。extern crate winapi;
// --- std ---
use std::ffi::CStr;
// --- external ---
use winapi::{
shared::{minwindef::LPARAM, windef::HWND__},
um::winuser::{EnumWindows, GetWindowTextA},
};
fn find_window() {
extern "system" fn enum_windows_proc(hwnd: *mut HWND__, _l_param: LPARAM) -> i32 {
let ptr = &mut 0;
unsafe {
GetWindowTextA(hwnd, ptr as *mut i8, 1024);
println!("hwnd: {:?}, name: {:?}", hwnd, CStr::from_ptr(ptr));
}
1
}
unsafe {
EnumWindows(Some(enum_windows_proc), 0);
}
}
fn main() {
find_window();
}
我找到了一些解决方案:
但是我得到这样的东西:
hwnd: 0x100d6, name: ""
hwnd: 0x10250, name: "B \xa3\xd6T\xf7\x7f\x00\x00\x03\x00\x00\x00"
hwnd: 0x10260, name: ""
hwnd: 0x1028a, name: "N \xa3\xd6T\xf7\x7f\x00\x00\x03\x00\x00\x00\x00"
hwnd: 0x10288, name: ""
hwnd: 0x706f8, name: "T \xa3\xd6T\xf7\x7f"
hwnd: 0x1011c, name: ""
hwnd: 0x10510, name: "W \xa3\xd6T\xf7"
hwnd: 0x2037c, name: ""
hwnd: 0x30742, name: "C \xa3\xd6T\xf7\x7f\x00\x00\x03\x00\x00\x00\x00\x00"
hwnd: 0x12019c, name: "M \xa3\xd6T\xf7\x7f\x00\x00\x03\x00\x00\x00\x00"
hwnd: 0x90754, name: "C \xa3\xd6T\xf7\x7f\x00\x00\x03\x00\x00\x00\x00\x00"
hwnd: 0x1004e4, name: "M \xa3\xd6T\xf7\x7f\x00\x00\x03\x00\x00\x00\x00"
很显然,所有名称都是错误的。
如何从ptr
中获取字符串?