Skip to content

Commit

Permalink
chore: use win32utils for accessing registry
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Noah committed Jul 29, 2024
1 parent 8041cf8 commit ffcf206
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 77 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ serde = { version = "1.0.203", features = ["derive"] }
serde-xml-rs = "0.6.0"

[target.'cfg(windows)'.dependencies]
win32utils = "0.1.0"
win32utils = "0.2.0"

[target.'cfg(windows)'.dependencies.windows]
version = "0.58.0"
features = [
"Win32_Foundation",
"Win32_Security",
"Win32_Storage_FileSystem",
"Win32_System_Console",
"Win32_System_Registry",
"Win32_UI_WindowsAndMessaging",
]

Expand Down
81 changes: 9 additions & 72 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,44 +220,18 @@ fn hide_console_window() {

#[cfg(target_os = "windows")]
fn add_to_startup() {
use windows::{
core::PCWSTR,
Win32::{
Foundation::ERROR_FILE_NOT_FOUND,
System::Registry::{RegCloseKey, RegCreateKeyExW, RegOpenKeyExW, RegQueryValueExW, RegSetValueExW, HKEY_CURRENT_USER, KEY_READ, KEY_WRITE, REG_OPTION_NON_VOLATILE, REG_SZ},
},
};

fn to_utf16(s: &str) -> Vec<u16> {
s.encode_utf16().chain(std::iter::once(0)).collect()
}

fn v16_to_v8(v: &[u16]) -> Vec<u8> {
unsafe { std::slice::from_raw_parts(v.as_ptr() as *const u8, v.len() * 2).to_vec() }
}

let subkey = r#"Software\Microsoft\Windows\CurrentVersion\Run"#;
let value = "Steam Screenshot Organizer";
let name = "Steam Screenshot Organizer";

// check if the program is already in startup

let mut key = HKEY_CURRENT_USER;

let result = unsafe { RegOpenKeyExW(HKEY_CURRENT_USER, PCWSTR::from_raw(to_utf16(subkey).as_ptr()), 0, KEY_READ, &mut key) };

if result.is_err() {
eprintln!("Failed to open registry key");
return;
}

let result = unsafe { RegQueryValueExW(key, PCWSTR::from_raw(to_utf16(value).as_ptr()), None, None, None, None) };

if result != ERROR_FILE_NOT_FOUND {
unsafe {
let _ = RegCloseKey(key);
match win32utils::registry::exists(win32utils::registry::HKEY::CurrentUser, subkey, name) {
Ok(true) => return,
Ok(false) => {}
Err(error) => {
eprintln!("Failed to check registry key: {}", error.to_string());
return;
}

return;
}

match win32utils::dialog(
Expand All @@ -270,47 +244,10 @@ fn add_to_startup() {
_ => return,
}

let mut key = HKEY_CURRENT_USER;
let path = std::env::current_exe().unwrap();

let result = unsafe {
RegCreateKeyExW(
HKEY_CURRENT_USER,
PCWSTR::from_raw(to_utf16(subkey).as_ptr()),
0,
None,
REG_OPTION_NON_VOLATILE,
KEY_WRITE,
None,
&mut key,
None,
)
};

if result.is_err() {
eprintln!("Failed to create registry key");
return;
}

let result = unsafe {
RegSetValueExW(
key,
PCWSTR::from_raw(to_utf16(value).as_ptr()),
0,
REG_SZ,
Some(&v16_to_v8(&to_utf16(&path.to_string_lossy()))),
)
};

if result.is_err() {
eprintln!("Failed to set registry key");
return;
}

let result = unsafe { RegCloseKey(key) };

if result.is_err() {
eprintln!("Failed to close registry key");
if win32utils::registry::write_string(win32utils::registry::HKEY::CurrentUser, &subkey, &name, path.to_string_lossy()).is_err() {
eprintln!("Failed to write registry key");
}
}

Expand Down

0 comments on commit ffcf206

Please sign in to comment.