Skip to content

Commit

Permalink
Add WMI sample (#2041)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Sep 20, 2022
1 parent a8cf611 commit ba4566c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Expand Up @@ -125,6 +125,7 @@ jobs:
cargo clippy -p sample_simple &&
cargo clippy -p sample_spellchecker &&
cargo clippy -p sample_uiautomation &&
cargo clippy -p sample_wmi &&
cargo clippy -p sample_xml &&
cargo clippy -p windows_aarch64_gnullvm &&
cargo clippy -p windows_aarch64_msvc &&
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Expand Up @@ -108,6 +108,7 @@ jobs:
cargo test --target ${{ matrix.target }} -p sample_simple &&
cargo test --target ${{ matrix.target }} -p sample_spellchecker &&
cargo test --target ${{ matrix.target }} -p sample_uiautomation &&
cargo test --target ${{ matrix.target }} -p sample_wmi &&
cargo test --target ${{ matrix.target }} -p sample_xml &&
cargo test --target ${{ matrix.target }} -p windows_aarch64_gnullvm &&
cargo test --target ${{ matrix.target }} -p windows_aarch64_msvc &&
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/windows/src/core/strings/bstr.rs
Expand Up @@ -5,7 +5,7 @@ use bindings::*;
pub struct BSTR(*const u16);

impl BSTR {
pub fn new() -> Self {
pub const fn new() -> Self {
Self(core::ptr::null_mut())
}

Expand Down
15 changes: 15 additions & 0 deletions crates/samples/wmi/Cargo.toml
@@ -0,0 +1,15 @@
[package]
name = "sample_wmi"
version = "0.0.0"
edition = "2018"

[dependencies.windows]
path = "../../libs/windows"
features = [
"Win32_Foundation",
"Win32_System_Com",
"Win32_System_Ole",
"Win32_System_Wmi",
"Win32_System_Rpc",
"Win32_Security",
]
36 changes: 36 additions & 0 deletions crates/samples/wmi/src/main.rs
@@ -0,0 +1,36 @@
use windows::{core::*, Win32::Security::*, Win32::System::Com::*, Win32::System::Ole::*, Win32::System::Wmi::*};

fn main() -> Result<()> {
unsafe {
CoInitializeEx(None, COINIT_MULTITHREADED)?;

CoInitializeSecurity(PSECURITY_DESCRIPTOR::default(), -1, None, None, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, None, EOAC_NONE, None)?;

let locator: IWbemLocator = CoCreateInstance(&WbemLocator, None, CLSCTX_INPROC_SERVER)?;

let server = locator.ConnectServer(&BSTR::from("root\\cimv2"), &BSTR::new(), &BSTR::new(), &BSTR::new(), 0, &BSTR::new(), None)?;

// TODO: workaround for https://github.com/microsoft/win32metadata/issues/1265
let query = server.ExecQuery(&BSTR::from("WQL"), &BSTR::from("select Caption from Win32_LogicalDisk"), WBEM_FLAG_FORWARD_ONLY.0 | WBEM_FLAG_RETURN_IMMEDIATELY.0, None)?;

loop {
let mut row = [None; 1];
let mut returned = 0;
// TODO: workaround for https://github.com/microsoft/win32metadata/issues/1266
query.Next(-1, &mut row, &mut returned).ok()?;

if let Some(row) = &row[0] {
let mut value = Default::default();
row.Get(w!("Caption"), 0, &mut value, std::ptr::null_mut(), std::ptr::null_mut())?;
println!("{}", VarFormat(&value, None, 0, 0, 0)?);

// TODO: workaround for https://github.com/microsoft/windows-rs/issues/539
VariantClear(&mut value)?;
} else {
break;
}
}

Ok(())
}
}

0 comments on commit ba4566c

Please sign in to comment.