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
每月下载量 57,680
用于 65 个 crate (15 个直接使用)
185KB
3.5K SLoC
wmi
WMI (Windows Management Instrumentation) 的 rust crate。
# Cargo.toml
[dependencies]
wmi = "0.13"
示例
查询可以反序列化为自由形式的 HashMap
或 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
与 time
如果你更喜欢使用 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