Skip to content

Commit a85c037

Browse files
Copilotkennykerr
andauthored
Add support for volatile registry keys in OpenOptions (#3632)
Co-authored-by: Kenny Kerr <kenny@kennykerr.ca>
1 parent 6705c15 commit a85c037

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

crates/libs/registry/src/bindings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub const REG_EXPAND_SZ: REG_VALUE_TYPE = 2u32;
7272
pub const REG_MULTI_SZ: REG_VALUE_TYPE = 7u32;
7373
pub type REG_OPEN_CREATE_OPTIONS = u32;
7474
pub const REG_OPTION_NON_VOLATILE: REG_OPEN_CREATE_OPTIONS = 0u32;
75+
pub const REG_OPTION_VOLATILE: REG_OPEN_CREATE_OPTIONS = 1u32;
7576
pub const REG_QWORD: REG_VALUE_TYPE = 11u32;
7677
pub type REG_SAM_FLAGS = u32;
7778
pub const REG_SZ: REG_VALUE_TYPE = 1u32;

crates/libs/registry/src/open_options.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub struct OpenOptions<'a> {
77
access: u32,
88
create: bool,
99
transaction: Option<&'a Transaction>,
10+
options: u32,
1011
}
1112

1213
impl<'a> OpenOptions<'a> {
@@ -16,6 +17,7 @@ impl<'a> OpenOptions<'a> {
1617
access: 0,
1718
create: false,
1819
transaction: None,
20+
options: REG_OPTION_NON_VOLATILE,
1921
}
2022
}
2123

@@ -49,6 +51,12 @@ impl<'a> OpenOptions<'a> {
4951
self
5052
}
5153

54+
/// Sets the option to create a volatile registry key that is not preserved when the system restarts.
55+
pub fn volatile(&mut self) -> &mut Self {
56+
self.options |= REG_OPTION_VOLATILE;
57+
self
58+
}
59+
5260
/// Opens a registry key with the options provided by `self`.
5361
pub fn open<T: AsRef<str>>(&self, path: T) -> Result<Key> {
5462
let mut handle = null_mut();
@@ -61,7 +69,7 @@ impl<'a> OpenOptions<'a> {
6169
pcwstr(path).as_ptr(),
6270
0,
6371
null(),
64-
REG_OPTION_NON_VOLATILE,
72+
self.options,
6573
self.access,
6674
null(),
6775
&mut handle,
@@ -86,7 +94,7 @@ impl<'a> OpenOptions<'a> {
8694
pcwstr(path).as_ptr(),
8795
0,
8896
null(),
89-
REG_OPTION_NON_VOLATILE,
97+
self.options,
9098
self.access,
9199
null(),
92100
&mut handle,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use windows_registry::*;
2+
3+
#[test]
4+
fn volatile_key() {
5+
let test_key = "software\\windows-rs\\tests\\volatile";
6+
_ = CURRENT_USER.remove_tree(test_key);
7+
8+
// Create a volatile key - this key will not persist when the system restarts
9+
let key = CURRENT_USER
10+
.options()
11+
.create()
12+
.volatile()
13+
.read()
14+
.write()
15+
.open(test_key)
16+
.unwrap();
17+
18+
// Write some data to the volatile key
19+
key.set_u64("test_value", 12345u64).unwrap();
20+
21+
// Verify we can read the data
22+
assert_eq!(key.get_u64("test_value").unwrap(), 12345u64);
23+
}

crates/tools/bindings/src/registry.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
REG_EXPAND_SZ
2525
REG_MULTI_SZ
2626
REG_OPTION_NON_VOLATILE
27+
REG_OPTION_VOLATILE
2728
REG_QWORD
2829
REG_SZ
2930
RegCloseKey

0 commit comments

Comments
 (0)