I'm trying to get the names of all the windows using
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();
}
I found some solutions:
But I get something like this:
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"
Obviously, all the names are wrong.
How could I get the string from ptr
?