Skip to content

Commit

Permalink
feat: 使用window注册表替换sysinfo (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
cn-kali-team authored Dec 14, 2024
1 parent c7d47ee commit ab1906b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ vendored = ["dbus/vendored"]
[dependencies]
image = "0.25"
log = "0.4"
sysinfo = "0.33"
thiserror = "2.0"

[target.'cfg(target_os = "macos")'.dependencies]
Expand All @@ -33,6 +32,7 @@ windows = { version = "0.58", features = [
"Win32_Storage_Xps",
"Win32_System_Threading",
"Win32_System_ProcessStatus",
"Win32_System_Registry",
"Win32_Storage_FileSystem",
"Win32_Graphics_Dxgi",
"Win32_Graphics_Direct3D",
Expand Down
27 changes: 20 additions & 7 deletions src/windows/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use image::RgbaImage;
use sysinfo::System;
use windows::core::w;
use windows::Win32::System::Registry::{RegGetValueW, HKEY_LOCAL_MACHINE, RRF_RT_REG_DWORD};
use windows::Win32::Foundation::GetLastError;

use crate::{error::XCapResult, XCapError};
Expand All @@ -15,12 +16,24 @@ pub(super) fn wide_string_to_string(wide_string: &[u16]) -> XCapResult<String> {
}

pub(super) fn get_os_major_version() -> u8 {
System::os_version()
.map(|os_version| {
let strs: Vec<&str> = os_version.split(' ').collect();
strs[0].parse::<u8>().unwrap_or(0)
})
.unwrap_or(0)
unsafe {
let mut buf_len: u32 = 4;
let mut buf = [0u8; 4];
let err = RegGetValueW(
HKEY_LOCAL_MACHINE,
w!(r"SOFTWARE\Microsoft\Windows NT\CurrentVersion"),
w!("CurrentMajorVersionNumber"),
RRF_RT_REG_DWORD,
None,
Some(buf.as_mut_ptr().cast()),
Some(&mut buf_len),
);
if err.is_ok() {
u32::from_le_bytes(buf) as u8
} else {
0
}
}
}

pub(super) fn log_last_error<T: ToString>(label: T) {
Expand Down

0 comments on commit ab1906b

Please sign in to comment.