Skip to content

Commit

Permalink
Use a singleton for COMLibrary to fix issue #139 (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
FantasyTeddy authored Mar 17, 2023
1 parent 35bcc99 commit 3ef1f8f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ num_cpus = "1.13.1"

[target.'cfg(target_os = "windows")'.dependencies]
local-ip-address = "0.4.4"
wmi = "0.11.0"
wmi = "0.12.0"
winreg = "0.10.1"
windows = { version = "0.39.0", features = [
"Win32_Foundation",
Expand Down
13 changes: 11 additions & 2 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
use std::path::PathBuf;
use winreg::enums::*;
use winreg::RegKey;
use wmi::WMIResult;
use wmi::{COMLibrary, Variant, WMIConnection};

use windows::{
Expand Down Expand Up @@ -148,6 +149,15 @@ impl WindowsMemoryReadout {
}
}

thread_local! {
static COM_LIB: COMLibrary = COMLibrary::new().unwrap();
}

fn wmi_connection() -> WMIResult<WMIConnection> {
let com_lib = COM_LIB.with(|com| *com);
WMIConnection::new(com_lib)
}

pub struct WindowsGeneralReadout;

impl GeneralReadout for WindowsGeneralReadout {
Expand Down Expand Up @@ -310,8 +320,7 @@ impl GeneralReadout for WindowsGeneralReadout {
}

fn os_name(&self) -> Result<String, ReadoutError> {
let com_con = COMLibrary::new()?;
let wmi_con = WMIConnection::new(com_con)?;
let wmi_con = wmi_connection()?;

let results: Vec<HashMap<String, Variant>> =
wmi_con.raw_query("SELECT Caption FROM Win32_OperatingSystem")?;
Expand Down

0 comments on commit 3ef1f8f

Please sign in to comment.