16 releases (7 breaking)

new 0.8.1 Apr 8, 2021
0.8.0 Mar 10, 2021
0.7.0 Nov 7, 2020
0.5.0 Jul 21, 2020
0.4.4 Jun 11, 2019

#10 in Windows APIs

Download history 85/week @ 2020-12-22 41/week @ 2020-12-29 97/week @ 2021-01-05 136/week @ 2021-01-12 167/week @ 2021-01-19 171/week @ 2021-01-26 159/week @ 2021-02-02 146/week @ 2021-02-09 240/week @ 2021-02-16 129/week @ 2021-02-23 133/week @ 2021-03-02 161/week @ 2021-03-09 113/week @ 2021-03-16 172/week @ 2021-03-23 418/week @ 2021-03-30 555/week @ 2021-04-06

839 downloads per month

MIT/Apache

120KB
2.5K SLoC

wmi

Build Status crates.io docs.rs

WMI (Windows Management Instrumentation) crate for rust.

# Cargo.toml
[dependencies]
wmi = "0.8"

Examples

Queries can be deserialized into a free-form HashMap or a struct:

#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use serde::Deserialize;
use wmi::{COMLibrary, Variant, WMIConnection, WMIDateTime};
use std::collections::HashMap;

fn main() -> Result<(), Box<dyn std::error::Error>>  {
    let com_con = COMLibrary::new()?;
    let wmi_con = WMIConnection::new(com_con.into())?;
    
    let results: Vec<HashMap<String, Variant>> = wmi_con.raw_query("SELECT * FROM Win32_OperatingSystem")?;
    
    for os in results {
        println!("{:#?}", os);
    }
    
    #[derive(Deserialize, Debug)]
    struct Win32_OperatingSystem {
        Caption: String,
        Name: String,
        CurrentTimeZone: i16,
        Debug: bool,
        EncryptionLevel: u32,
        ForegroundApplicationBoost: u8,
        LastBootUpTime: WMIDateTime,
    }
    
    let results: Vec<Win32_OperatingSystem> = wmi_con.query()?;
    
    for os in results {
        println!("{:#?}", os);
    }
    
    Ok(())
}

Async Queries

WMI supports async queries, with methods like ExecAsyncQuery.

This crate provides async methods under the async-query flag:

# Cargo.toml
[dependencies]
wmi = { version = "0.8", features = ["async-query"] }

The methods become available on WMIConnection

#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use serde::Deserialize;
use wmi::{COMLibrary, Variant, WMIConnection, WMIDateTime};
use std::collections::HashMap;
use futures::executor::block_on;

fn main() -> Result<(), Box<dyn std::error::Error>>  {
    let com_con = COMLibrary::new()?;
    let wmi_con = WMIConnection::new(com_con.into())?;
    
    block_on(exec_async_query(&wmi_con))?;
    
    Ok(())
}

async fn exec_async_query(wmi_con: &WMIConnection) -> Result<(), Box<dyn std::error::Error>> {
    let results: Vec<HashMap<String, Variant>> = 
        wmi_con.async_raw_query("SELECT * FROM Win32_OperatingSystem").await?;

    for os in results {
        println!("{:#?}", os);
    }
    
    #[derive(Deserialize, Debug)]
    struct Win32_OperatingSystem {
        Caption: String,
        Name: String,
        CurrentTimeZone: i16,
        Debug: bool,
        EncryptionLevel: u32,
        ForegroundApplicationBoost: u8,
        LastBootUpTime: WMIDateTime,
    }
    
    let results: Vec<Win32_OperatingSystem> = wmi_con.async_query().await?;
    
    for os in results {
        println!("{:#?}", os);
    }
    
    Ok(())
}

License

The wmi crate is licensed under either of

Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

Dependencies

~0–2MB
~38K SLoC