34 个版本

0.13.3 2024年2月27日
0.13.1 2023年6月2日
0.12.1 2023年3月18日
0.11.4 2022年12月2日
0.4.4 2019年6月11日

#6 in Windows APIs

Download history 16499/week @ 2024-04-16 16372/week @ 2024-04-23 14116/week @ 2024-04-30 16090/week @ 2024-05-07 16796/week @ 2024-05-14 12411/week @ 2024-05-21 10676/week @ 2024-05-28 9741/week @ 2024-06-04 9724/week @ 2024-06-11 10782/week @ 2024-06-18 11265/week @ 2024-06-25 10906/week @ 2024-07-02 13053/week @ 2024-07-09 13855/week @ 2024-07-16 14055/week @ 2024-07-23 14279/week @ 2024-07-30

每月下载量 57,680
用于 65 个 crate (15 个直接使用)

MIT/Apache

185KB
3.5K SLoC

wmi

checkcrates.io docs.rs

WMI (Windows Management Instrumentation) 的 rust crate。

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

示例

查询可以反序列化为自由形式的 HashMapstruct

#![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(())
}

chronotime

如果你更喜欢使用 time crate 而不是默认的 chrono,将 wmi 包含在内

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

并使用 WMIOffsetDateTime 包装器代替 WMIDateTime 包装器。

异步查询

WMI 支持异步查询,例如使用 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(())
}

许可证

wmi crate 受以下任一许可证的许可

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)

由您选择。

依赖项

~0–36MB
~538K SLoC