1 Cargo.toml
[package] name = "hellowinapi" version = "0.1.0" edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] windows = "0.19" [build-dependencies] windows = "0.19"
2 build.rs
fn main() { windows::build! { Windows::Win32:: {UI::WindowsAndMessaging::{ EnumWindows, GetWindowTextW, }, {Storage::FileSystem::GetLogicalDriveStringsW,} } }; }
3 src/main.rs
mod bindings { windows::include_bindings!(); } use {bindings::Windows::Win32::{UI::WindowsAndMessaging::{EnumWindows,GetWindowTextW,}, Foundation::{LPARAM,HWND,BOOL,PWSTR,} }}; fn main() -> windows::Result<()> { unsafe { EnumWindows(Some(enum_window), LPARAM(0)).ok() } } extern "system" fn enum_window(window: HWND, _: LPARAM) -> BOOL { unsafe { let mut text: [u16; 512] = [0; 512]; let len = GetWindowTextW(window, PWSTR(text.as_mut_ptr()), text.len() as i32); let text = String::from_utf16_lossy(&text[..len as usize]); if !text.is_empty() { println!("{}", text); } return true.into(); } }
参考:https://hub.fastgit.org/microsoft/windows-samples-rs