38 releases

new 0.14.2 Dec 4, 2024
0.14.1 Nov 26, 2024
0.14.0 Sep 9, 2024
0.13.3 Feb 27, 2024
0.4.4 Jun 11, 2019

#4 in Windows APIs

Download history 11718/week @ 2024-08-21 11234/week @ 2024-08-28 11159/week @ 2024-09-04 9671/week @ 2024-09-11 7937/week @ 2024-09-18 10492/week @ 2024-09-25 8597/week @ 2024-10-02 9760/week @ 2024-10-09 11091/week @ 2024-10-16 11635/week @ 2024-10-23 11495/week @ 2024-10-30 11598/week @ 2024-11-06 13364/week @ 2024-11-13 12901/week @ 2024-11-20 11608/week @ 2024-11-27 11997/week @ 2024-12-04

52,181 downloads per month
Used in 78 crates (21 directly)

MIT/Apache

190KB
3.5K SLoC

wmi

check crates.io docs.rs

WMI (Windows Management Instrumentation) crate for rust.

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

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(())
}

chrono vs time

If you prefer to use the time crate instead of the default chrono, include wmi as

[dependencies]
wmi-rs = { version = "*", default-features = false, features = ["time"] }

and use the WMIOffsetDateTime wrapper instead of the WMIDateTime wrapper.

Async Queries

WMI supports async queries, with methods like ExecAsyncQuery.

#![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 https://www.apache.org/licenses/LICENSE-2.0)
MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)

at your option.

Dependencies

~0–36MB
~540K SLoC