Skip to content

Commit

Permalink
refactor: 重构windows版本读取方式 (#174)
Browse files Browse the repository at this point in the history
* refactor: 重构windows版本读取方式

* chore: 修改devcontainer

---------

Co-authored-by: nashaofu <[email protected]>
  • Loading branch information
nashaofu and nashaofu authored Dec 14, 2024
1 parent ab1906b commit 5ab3195
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
7 changes: 5 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// }
// ]

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [6080, 5901],

Expand All @@ -33,9 +36,9 @@
"customizations": {
"vscode": {
"extensions": [
"wengerk.highlight-bad-chars",
"streetsidesoftware.code-spell-checker",
"EditorConfig.EditorConfig",
"wengerk.highlight-bad-chars",
"editorconfig.editorconfig",
"tamasfe.even-better-toml",
"rust-lang.rust-analyzer"
]
Expand Down
48 changes: 36 additions & 12 deletions src/windows/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use image::RgbaImage;
use windows::core::w;
use windows::Win32::System::Registry::{RegGetValueW, HKEY_LOCAL_MACHINE, RRF_RT_REG_DWORD};
use windows::Win32::Foundation::GetLastError;
use windows::{
core::w,
Win32::{
Foundation::GetLastError,
System::Registry::{RegGetValueW, HKEY_LOCAL_MACHINE, RRF_RT_REG_SZ},
},
};

use crate::{error::XCapResult, XCapError};

Expand All @@ -15,24 +19,44 @@ pub(super) fn wide_string_to_string(wide_string: &[u16]) -> XCapResult<String> {
Ok(string)
}

pub(super) fn get_os_major_version() -> u8 {
pub(super) fn get_build_number() -> u32 {
unsafe {
let mut buf_len: u32 = 4;
let mut buf = [0u8; 4];
let mut buf_len: u32 = 2048;
let mut buf: Vec<u16> = Vec::with_capacity(buf_len as usize);

let err = RegGetValueW(
HKEY_LOCAL_MACHINE,
w!(r"SOFTWARE\Microsoft\Windows NT\CurrentVersion"),
w!("CurrentMajorVersionNumber"),
RRF_RT_REG_DWORD,
w!("CurrentBuildNumber"),
RRF_RT_REG_SZ,
None,
Some(buf.as_mut_ptr().cast()),
Some(&mut buf_len),
);
if err.is_ok() {
u32::from_le_bytes(buf) as u8
} else {
0

if err.is_err() {
return 0;
}

buf.set_len(buf_len as usize);

let build_version = wide_string_to_string(&buf).unwrap_or_default();

build_version.parse().unwrap_or(0)
}
}

pub(super) fn get_os_major_version() -> u8 {
let build_number = get_build_number();
// https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
if build_number >= 22000 {
11
} else if build_number >= 10240 {
10
} else if build_number >= 9200 {
8
} else {
7
}
}

Expand Down

0 comments on commit 5ab3195

Please sign in to comment.