Skip to content

Instantly share code, notes, and snippets.

@ROki1988
Last active July 18, 2016 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ROki1988/af1a02e8f511d4b920f3e908c0f08b09 to your computer and use it in GitHub Desktop.
Save ROki1988/af1a02e8f511d4b920f3e908c0f08b09 to your computer and use it in GitHub Desktop.
rust_sample
extern crate winapi;
extern crate user32;
fn main() {
println!("{}", get_foreground_window_title())
}
fn get_foreground_window_title() -> String {
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
unsafe {
let whdl = user32::GetForegroundWindow();
let length = user32::GetWindowTextLengthW(whdl);
let mut title = vec![ 0u16; (length+1) as usize ];
user32::GetWindowTextW(whdl, title.as_mut_ptr(), length + 1);
OsString::from_wide(&title).to_string_lossy().into_owned()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment