Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add functions to change options
  • Loading branch information
Adrien-Bodineau committed Feb 29, 2024
commit 8818cb5329a7cf54e288980dbcca8589dc30a57e
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "winreg"
edition = "2018"
version = "0.52.0"
version = "0.53.0"
authors = ["Igor Shaula <gentoo90@gmail.com>"]
license = "MIT"
description = "Rust bindings to MS Windows Registry API"
Expand Down
124 changes: 124 additions & 0 deletions src/reg_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,38 @@ impl RegKey {
}
}

/// 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>> {
/// # use windows_sys::Win32::System::Registry::REG_OPTION_CREATE_LINK;
/// let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
/// hklm.open_subkey_with_options_flags("SOFTWARE\\Microsoft", REG_OPTION_CREATE_LINK, KEY_READ)?;
/// # Ok(())
/// # }
/// ```
pub fn open_subkey_with_options_flags<P: AsRef<OsStr>>(
&self,
path: P,
options: Registry::REG_OPEN_CREATE_OPTIONS,
perms: Registry::REG_SAM_FLAGS,
) -> io::Result<RegKey> {
let c_path = to_utf16(path);
let mut new_hkey: HKEY = 0;
match unsafe {
Registry::RegOpenKeyExW(self.hkey, c_path.as_ptr(), options, perms, &mut new_hkey)
} {
0 => Ok(RegKey { hkey: new_hkey }),
err => werr!(err),
}
}

/// Part of `transactions` feature.
#[cfg(feature = "transactions")]
pub fn open_subkey_transacted<P: AsRef<OsStr>>(
Expand Down Expand Up @@ -212,6 +244,33 @@ impl RegKey {
}
}

/// Part of `transactions` feature.
#[cfg(feature = "transactions")]
pub fn open_subkey_transacted_with_options_flags<P: AsRef<OsStr>>(
&self,
path: P,
t: &Transaction,
options: Registry::REG_OPEN_CREATE_OPTIONS,
perms: Registry::REG_SAM_FLAGS,
) -> io::Result<RegKey> {
let c_path = to_utf16(path);
let mut new_hkey: HKEY = 0;
match unsafe {
Registry::RegOpenKeyTransactedW(
self.hkey,
c_path.as_ptr(),
options,
perms,
&mut new_hkey,
t.handle,
ptr::null_mut(),
)
} {
0 => Ok(RegKey { hkey: new_hkey }),
err => werr!(err),
}
}

/// Create subkey (and all missing parent keys)
/// and open it with `KEY_ALL_ACCESS` permissions.
/// Will just open key if it already exists.
Expand Down Expand Up @@ -270,6 +329,36 @@ impl RegKey {
}
}

pub fn create_subkey_with_options_flags<P: AsRef<OsStr>>(
&self,
path: P,
options: Registry::REG_OPEN_CREATE_OPTIONS,
perms: Registry::REG_SAM_FLAGS,
) -> io::Result<(RegKey, RegDisposition)> {
let c_path = to_utf16(path);
let mut new_hkey: HKEY = 0;
let mut disp_buf: u32 = 0;
match unsafe {
Registry::RegCreateKeyExW(
self.hkey,
c_path.as_ptr(),
0,
ptr::null_mut(),
options,
perms,
ptr::null_mut(),
&mut new_hkey,
&mut disp_buf,
)
} {
0 => {
let disp: RegDisposition = unsafe { transmute(disp_buf as u8) };
Ok((RegKey { hkey: new_hkey }, disp))
}
err => werr!(err),
}
}

/// Part of `transactions` feature.
#[cfg(feature = "transactions")]
pub fn create_subkey_transacted<P: AsRef<OsStr>>(
Expand Down Expand Up @@ -314,6 +403,41 @@ impl RegKey {
}
}

/// Part of `transactions` feature.
#[cfg(feature = "transactions")]
pub fn create_subkey_transacted_with_options_flags<P: AsRef<OsStr>>(
&self,
path: P,
t: &Transaction,
options: Registry::REG_OPEN_CREATE_OPTIONS,
perms: Registry::REG_SAM_FLAGS,
) -> io::Result<(RegKey, RegDisposition)> {
let c_path = to_utf16(path);
let mut new_hkey: HKEY = 0;
let mut disp_buf: u32 = 0;
match unsafe {
Registry::RegCreateKeyTransactedW(
self.hkey,
c_path.as_ptr(),
0,
ptr::null_mut(),
options,
perms,
ptr::null_mut(),
&mut new_hkey,
&mut disp_buf,
t.handle,
ptr::null_mut(),
)
} {
0 => {
let disp: RegDisposition = unsafe { transmute(disp_buf as u8) };
Ok((RegKey { hkey: new_hkey }, disp))
}
err => werr!(err),
}
}

/// Rename a subkey
///
/// # Examples
Expand Down