Thanks chrisd. I appreciate it. I've added the backticks
I tried your code and now I don't get an error but I don't get the expected output when trying to get the window text using SendMessageW with WM_GETTEXT.
The result looks correct as in the length of the string, but the the buffer is empty.
I can get the window title though when using GetWindowTextW.
I know the window handles are correct as I'm using winspy to verify.
Anyway this is the complete code:
extern crate winapi;
use winapi::{
shared::{
minwindef::{
LPARAM,
TRUE,
BOOL
},
windef::HWND,
},
um::{
winuser::{
EnumChildWindows,
FindWindowA,
GetWindowTextW,
GetWindowTextLengthW,
WM_GETTEXTLENGTH,
WM_GETTEXT,
SendMessageW
},
},
};
use std::ffi::CString;
fn main() {
let window_name = CString::new("*Untitled - Notepad").unwrap();
unsafe {
let parent_handle = FindWindowA(std::ptr::null_mut(), window_name.as_ptr());
let title_length = GetWindowTextLengthW(parent_handle) + 1;
let mut title_buffer = Vec::<u16>::with_capacity(title_length as usize);
let title_result = GetWindowTextW(parent_handle, title_buffer.as_mut_ptr(), title_length);
title_buffer.set_len((title_result) as usize);
let title_string = String::from_utf16_lossy(&title_buffer);
let mut temp_buffer = Vec::<u16>::with_capacity(title_length as usize);
let temp_result = SendMessageW(parent_handle, WM_GETTEXT, title_length as usize, temp_buffer.as_mut_ptr() as LPARAM);
let temp_string = String::from_utf16_lossy(&temp_buffer);
println!("Parent Handle: {:?}\n\tTitle Length: {:?}\n\tTitle Buffer: {:?}\n\tTitle Result: {:?}\n\tTitle String: {:?}\n\t\tTemp Buffer: {:?}\n\t\tTemp Result: {:?}\n\t\tTemp String: {:?}",&parent_handle, title_length, title_buffer, title_result, title_string, temp_buffer, temp_result, temp_string);
EnumChildWindows(parent_handle, Some(enumerate_callback), 0isize);
}
}
unsafe extern "system" fn enumerate_callback(hwnd: HWND, _: LPARAM) -> BOOL {
let text_length = SendMessageW(hwnd, WM_GETTEXTLENGTH, 0, 0) + 1;
let mut text_buffer = Vec::<u16>::with_capacity(text_length as usize);
let text_result = SendMessageW(hwnd, WM_GETTEXT, text_length as usize, text_buffer.as_mut_ptr() as LPARAM);
let text_string = String::from_utf16_lossy(&text_buffer);
println!("Child Handle: {:?}\n\tText Length: {:?}\n\tText Buffer: {:?}\n\tText Result: {:?}\n\tText String: {:?}",&hwnd, text_length, text_buffer, text_result, text_string);
return TRUE;
}