Skip to content

Commit

Permalink
Add RegKey methods for creating/opening subkeys with custom options
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien-Bodineau authored and gentoo90 committed Jan 5, 2025
1 parent d8f6721 commit bbf3d7f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
pub use winapi::um::winnt::{
KEY_ALL_ACCESS, KEY_CREATE_LINK, KEY_CREATE_SUB_KEY, KEY_ENUMERATE_SUB_KEYS, KEY_EXECUTE,
KEY_NOTIFY, KEY_QUERY_VALUE, KEY_READ, KEY_SET_VALUE, KEY_WOW64_32KEY, KEY_WOW64_64KEY,
KEY_WOW64_RES, KEY_WRITE,
KEY_WOW64_RES, KEY_WRITE, REG_OPTION_BACKUP_RESTORE, REG_OPTION_CREATE_LINK,
REG_OPTION_DONT_VIRTUALIZE, REG_OPTION_NON_VOLATILE, REG_OPTION_OPEN_LINK, REG_OPTION_RESERVED,
REG_OPTION_VOLATILE,
};
pub use winapi::um::winreg::{
HKEY_CLASSES_ROOT, HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_CURRENT_USER_LOCAL_SETTINGS,
Expand Down
66 changes: 62 additions & 4 deletions src/reg_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,36 @@ impl RegKey {
&self,
path: P,
perms: winapi_reg::REGSAM,
) -> io::Result<RegKey> {
self.open_subkey_with_options_flags(path, 0, perms)
}

/// Open subkey with desired permissions and options.
/// Will open another handle to itself if `path` is an empty string.
///
/// # Examples
///
/// ```no_run
/// # use std::error::Error;
/// # use winreg::RegKey;
/// # use winreg::enums::*;
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
/// hklm.open_subkey_with_options_flags("SOFTWARE\\LinkKey", REG_OPTION_OPEN_LINK, KEY_READ)?;
/// # Ok(())
/// # }
/// ```
pub fn open_subkey_with_options_flags<P: AsRef<OsStr>>(
&self,
path: P,
options: DWORD,
perms: winapi_reg::REGSAM,
) -> io::Result<RegKey> {
let c_path = to_utf16(path);
let mut new_hkey: HKEY = ptr::null_mut();
match unsafe {
winapi_reg::RegOpenKeyExW(self.hkey, c_path.as_ptr(), 0, perms, &mut new_hkey) as DWORD
winapi_reg::RegOpenKeyExW(self.hkey, c_path.as_ptr(), options, perms, &mut new_hkey)

Check warning on line 199 in src/reg_key.rs

View workflow job for this annotation

GitHub Actions / CI (stable, x86_64-pc-windows-msvc)

current MSRV (Minimum Supported Rust Version) is `1.34.0` but this item is stable since `1.37.0`
as DWORD
} {
0 => Ok(RegKey { hkey: new_hkey }),
err => werr!(err),
Expand All @@ -196,14 +221,26 @@ impl RegKey {
path: P,
t: &Transaction,
perms: winapi_reg::REGSAM,
) -> io::Result<RegKey> {
self.open_subkey_transacted_with_options_flags(path, t, 0, perms)
}

/// Part of `transactions` feature.
#[cfg(feature = "transactions")]
pub fn open_subkey_transacted_with_options_flags<P: AsRef<OsStr>>(
&self,
path: P,
t: &Transaction,
options: DWORD,
perms: winapi_reg::REGSAM,
) -> io::Result<RegKey> {
let c_path = to_utf16(path);
let mut new_hkey: HKEY = ptr::null_mut();
match unsafe {
winapi_reg::RegOpenKeyTransactedW(
self.hkey,
c_path.as_ptr(),
0,
options,
perms,
&mut new_hkey,
t.handle,
Expand Down Expand Up @@ -248,6 +285,15 @@ impl RegKey {
&self,
path: P,
perms: winapi_reg::REGSAM,
) -> io::Result<(RegKey, RegDisposition)> {
self.create_subkey_with_options_flags(path, REG_OPTION_NON_VOLATILE, perms)
}

pub fn create_subkey_with_options_flags<P: AsRef<OsStr>>(
&self,
path: P,
options: DWORD,
perms: winapi_reg::REGSAM,
) -> io::Result<(RegKey, RegDisposition)> {
let c_path = to_utf16(path);
let mut new_hkey: HKEY = ptr::null_mut();
Expand All @@ -258,7 +304,7 @@ impl RegKey {
c_path.as_ptr(),
0,
ptr::null_mut(),
winnt::REG_OPTION_NON_VOLATILE,
options,
perms,
ptr::null_mut(),
&mut new_hkey,
Expand Down Expand Up @@ -290,6 +336,18 @@ impl RegKey {
path: P,
t: &Transaction,
perms: winapi_reg::REGSAM,
) -> io::Result<(RegKey, RegDisposition)> {
self.create_subkey_transacted_with_options_flags(path, t, REG_OPTION_NON_VOLATILE, perms)
}

/// Part of `transactions` feature.
#[cfg(feature = "transactions")]
pub fn create_subkey_transacted_with_options_flags<P: AsRef<OsStr>>(
&self,
path: P,
t: &Transaction,
options: DWORD,
perms: winapi_reg::REGSAM,
) -> io::Result<(RegKey, RegDisposition)> {
let c_path = to_utf16(path);
let mut new_hkey: HKEY = ptr::null_mut();
Expand All @@ -300,7 +358,7 @@ impl RegKey {
c_path.as_ptr(),
0,
ptr::null_mut(),
winnt::REG_OPTION_NON_VOLATILE,
options,
perms,
ptr::null_mut(),
&mut new_hkey,
Expand Down

0 comments on commit bbf3d7f

Please sign in to comment.