Skip to content

Commit ba4566c

Browse files
authored
Add WMI sample (#2041)
1 parent a8cf611 commit ba4566c

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ jobs:
125125
cargo clippy -p sample_simple &&
126126
cargo clippy -p sample_spellchecker &&
127127
cargo clippy -p sample_uiautomation &&
128+
cargo clippy -p sample_wmi &&
128129
cargo clippy -p sample_xml &&
129130
cargo clippy -p windows_aarch64_gnullvm &&
130131
cargo clippy -p windows_aarch64_msvc &&

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ jobs:
108108
cargo test --target ${{ matrix.target }} -p sample_simple &&
109109
cargo test --target ${{ matrix.target }} -p sample_spellchecker &&
110110
cargo test --target ${{ matrix.target }} -p sample_uiautomation &&
111+
cargo test --target ${{ matrix.target }} -p sample_wmi &&
111112
cargo test --target ${{ matrix.target }} -p sample_xml &&
112113
cargo test --target ${{ matrix.target }} -p windows_aarch64_gnullvm &&
113114
cargo test --target ${{ matrix.target }} -p windows_aarch64_msvc &&

crates/libs/windows/src/core/strings/bstr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bindings::*;
55
pub struct BSTR(*const u16);
66

77
impl BSTR {
8-
pub fn new() -> Self {
8+
pub const fn new() -> Self {
99
Self(core::ptr::null_mut())
1010
}
1111

crates/samples/wmi/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "sample_wmi"
3+
version = "0.0.0"
4+
edition = "2018"
5+
6+
[dependencies.windows]
7+
path = "../../libs/windows"
8+
features = [
9+
"Win32_Foundation",
10+
"Win32_System_Com",
11+
"Win32_System_Ole",
12+
"Win32_System_Wmi",
13+
"Win32_System_Rpc",
14+
"Win32_Security",
15+
]

crates/samples/wmi/src/main.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use windows::{core::*, Win32::Security::*, Win32::System::Com::*, Win32::System::Ole::*, Win32::System::Wmi::*};
2+
3+
fn main() -> Result<()> {
4+
unsafe {
5+
CoInitializeEx(None, COINIT_MULTITHREADED)?;
6+
7+
CoInitializeSecurity(PSECURITY_DESCRIPTOR::default(), -1, None, None, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, None, EOAC_NONE, None)?;
8+
9+
let locator: IWbemLocator = CoCreateInstance(&WbemLocator, None, CLSCTX_INPROC_SERVER)?;
10+
11+
let server = locator.ConnectServer(&BSTR::from("root\\cimv2"), &BSTR::new(), &BSTR::new(), &BSTR::new(), 0, &BSTR::new(), None)?;
12+
13+
// TODO: workaround for https://github.com/microsoft/win32metadata/issues/1265
14+
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)?;
15+
16+
loop {
17+
let mut row = [None; 1];
18+
let mut returned = 0;
19+
// TODO: workaround for https://github.com/microsoft/win32metadata/issues/1266
20+
query.Next(-1, &mut row, &mut returned).ok()?;
21+
22+
if let Some(row) = &row[0] {
23+
let mut value = Default::default();
24+
row.Get(w!("Caption"), 0, &mut value, std::ptr::null_mut(), std::ptr::null_mut())?;
25+
println!("{}", VarFormat(&value, None, 0, 0, 0)?);
26+
27+
// TODO: workaround for https://github.com/microsoft/windows-rs/issues/539
28+
VariantClear(&mut value)?;
29+
} else {
30+
break;
31+
}
32+
}
33+
34+
Ok(())
35+
}
36+
}

0 commit comments

Comments
 (0)