Skip to content

Instantly share code, notes, and snippets.

@kennykerr
Created October 25, 2022 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennykerr/aa630879280744062a1e953b94e44013 to your computer and use it in GitHub Desktop.
Save kennykerr/aa630879280744062a1e953b94e44013 to your computer and use it in GitHub Desktop.
use windows::{core::*, Win32::System::Performance::*};
fn main() {
unsafe {
let mut query = 0;
PdhOpenQueryW(None, 0, &mut query);
let mut counter = 0;
PdhAddCounterW(
query,
w!("\\Processor(0)\\% Processor Time"),
0,
&mut counter,
);
loop {
std::thread::sleep(std::time::Duration::new(1, 0));
PdhCollectQueryData(query);
let mut value = Default::default();
if 0 == PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, None, &mut value) {
println!("{:.2}", value.Anonymous.doubleValue);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment