Skip to content

Commit

Permalink
Added ability to get CPU Specs
Browse files Browse the repository at this point in the history
Also bumped version # and added build # to build workflow.
  • Loading branch information
neeleshpoli committed Sep 8, 2023
1 parent cde1ae4 commit 2161ef8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ jobs:
- name: Get Build Artifact
uses: actions/upload-artifact@v3
with:
name: sys-probe
name: sys-probe-build-${{ github.run_number }}
path: target/x86_64-pc-windows-msvc/release/sys-probe.exe
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sys-probe"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
17 changes: 17 additions & 0 deletions src/hardware/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mod processor;
pub mod windows {
use super::processor;

#[derive(Debug)]
pub struct HardwareInfo {
processor_info: processor::windows::ProcessorInfo,
}

pub fn get_hardware_info() -> Result<HardwareInfo, windows::core::Error> {
Ok(
HardwareInfo {
processor_info: processor::windows::get_processor_info()?,
}
)
}
}
44 changes: 44 additions & 0 deletions src/hardware/processor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
pub mod windows {
use crate::utils::windows::get_wmi;

#[derive(Debug)]
pub struct ProcessorInfo {
name: String,
cores: u32,
threads: u32,
current_clock_speed: u32,
current_voltage: Option<u16>,
max_clock_speed: u32,
processor_socket: String,
load_percentage: u16,
voltage_caps: Option<String>,
}

pub fn get_processor_info() -> Result<ProcessorInfo, windows::core::Error> {
let processor_info = get_wmi("Win32_Processor", "Name, NumberOfCores, ThreadCount, CurrentClockSpeed, CurrentVoltage, MaxClockSpeed, SocketDesignation, LoadPercentage, VoltageCaps")?;

Ok(
ProcessorInfo {
name: processor_info.get("Name").unwrap().to_string(),
cores: processor_info.get("NumberOfCores").unwrap().to_string().parse().unwrap(),
threads: processor_info.get("ThreadCount").unwrap().to_string().parse().unwrap(),
current_clock_speed: processor_info.get("CurrentClockSpeed").unwrap().to_string().parse().unwrap(),
current_voltage: {
let value:u16 = processor_info.get("CurrentVoltage").unwrap().parse().unwrap();
if (value & 0x0080) != 0 {
Some((value & 0x7f) * 10)
} else {
None
}
},
max_clock_speed: processor_info.get("MaxClockSpeed").unwrap().to_string().parse().unwrap(),
processor_socket: processor_info.get("SocketDesignation").unwrap().to_string(),
load_percentage: processor_info.get("LoadPercentage").unwrap().to_string().parse().unwrap(),
voltage_caps: match processor_info.get("VoltageCaps").unwrap().as_str() {
"" => None,
value => Some(value.to_string()),
},
}
)
}
}
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ use windows::Win32::System::Com::{CoInitializeEx, CoInitializeSecurity, COINIT_M
#[cfg(target_os = "windows")]
use crate::basic_info::windows::get_basic_info;

mod hardware;

#[cfg(target_os = "windows")]
fn main() -> windows::core::Result<()> {
// Initialize secutriy for WMI

use crate::{utils::windows::get_wmi, hardware::windows::get_hardware_info};
unsafe {
CoInitializeEx(None, COINIT_MULTITHREADED)?;
CoInitializeSecurity(None, -1, None, None, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, None, EOAC_NONE, None)?;
}

// Get basic info and print it
println!("{:#?}", get_basic_info());
println!("{:#?}", get_hardware_info());

Ok(())
}

0 comments on commit 2161ef8

Please sign in to comment.