Skip to content

Instantly share code, notes, and snippets.

@kennykerr
Created July 29, 2021 14:59
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 kennykerr/5eaf5d576ba5030be77c03d16adcf53b to your computer and use it in GitHub Desktop.
Save kennykerr/5eaf5d576ba5030be77c03d16adcf53b to your computer and use it in GitHub Desktop.
use Windows::Win32::System::Com::*;
use Windows::Win32::UI::Accessibility::*;
use Windows::Win32::UI::WindowsAndMessaging::*;
use Windows::UI::UIAutomation::*;
fn main() -> Result<()> {
unsafe {
CoInitializeEx(std::ptr::null_mut(), COINIT_MULTITHREADED)?;
let window = FindWindowA(None, "Calculator");
// Start with COM API
let automation: IUIAutomation = CoCreateInstance(&CUIAutomation, None, CLSCTX_ALL)?;
let element: IUIAutomationElement = automation.ElementFromHandle(window)?;
// Use COM API
let name = element.get_CurrentName()?;
println!("window name: {}", name);
// Query for WinRT API (will fail on earlier versions of Windows)
let element: AutomationElement = element.cast()?;
// Use WinRT API
println!("file name: {}", element.ExecutableFileName()?);
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment