use callcomapi::prelude::with_com;
use windows::Win32::System::Com::{CLSCTX_INPROC_SERVER, CoCreateInstance};
use windows::Win32::System::Wmi::{IWbemLocator, WbemLocator};
use windows::core::BSTR;
#[with_com]
fn main() {
println!("Using #[with_com] macro to auto-handle COM initialization...");
unsafe {
let result = call_wmi_sample();
match result {
Ok(_) => println!("Successfully called COM API!"),
Err(e) => eprintln!("Call failed: {}", e),
}
}
println!("COM will be automatically uninitialized when the function exits.");
}
unsafe fn call_wmi_sample() -> windows::core::Result<()> {
unsafe {
let locator: IWbemLocator = CoCreateInstance(&WbemLocator, None, CLSCTX_INPROC_SERVER)?;
let _service = locator.ConnectServer(
&BSTR::from("ROOT\\CIMV2"),
&BSTR::new(),
&BSTR::new(),
&BSTR::new(),
0,
&BSTR::new(),
None,
)?;
println!("Successfully connected to WMI service (ROOT\\CIMV2).");
Ok(())
}
}