windows_registry/
open_options.rs

1use super::*;
2
3/// Options and flags used to configure how a registry key is opened.
4#[derive(Debug)]
5pub struct OpenOptions<'a> {
6    parent: &'a Key,
7    access: u32,
8    create: bool,
9    transaction: Option<&'a Transaction>,
10    options: u32,
11}
12
13impl<'a> OpenOptions<'a> {
14    pub(crate) fn new(parent: &'a Key) -> Self {
15        Self {
16            parent,
17            access: 0,
18            create: false,
19            transaction: None,
20            options: REG_OPTION_NON_VOLATILE,
21        }
22    }
23
24    /// Sets the option for read access.
25    pub fn read(&mut self) -> &mut Self {
26        self.access |= KEY_READ;
27        self
28    }
29
30    /// Sets the option for write access.
31    pub fn write(&mut self) -> &mut Self {
32        self.access |= KEY_WRITE;
33        self
34    }
35
36    /// Sets additional access rights.
37    pub fn access(&mut self, access: u32) -> &mut Self {
38        self.access |= access;
39        self
40    }
41
42    /// Sets the option to create a new registry key, or open it if it already exists.
43    pub fn create(&mut self) -> &mut Self {
44        self.create = true;
45        self
46    }
47
48    /// Associate the registry key with a transaction.
49    pub fn transaction(&mut self, transaction: &'a Transaction) -> &mut Self {
50        self.transaction = Some(transaction);
51        self
52    }
53
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
60    /// Opens a registry key with the options provided by `self`.
61    pub fn open<T: AsRef<str>>(&self, path: T) -> Result<Key> {
62        let mut handle = null_mut();
63
64        let result = unsafe {
65            if let Some(transaction) = self.transaction {
66                if self.create {
67                    RegCreateKeyTransactedW(
68                        self.parent.0,
69                        pcwstr(path).as_ptr(),
70                        0,
71                        null(),
72                        self.options,
73                        self.access,
74                        null(),
75                        &mut handle,
76                        null_mut(),
77                        transaction.0,
78                        null(),
79                    )
80                } else {
81                    RegOpenKeyTransactedW(
82                        self.parent.0,
83                        pcwstr(path).as_ptr(),
84                        0,
85                        self.access,
86                        &mut handle,
87                        transaction.0,
88                        null(),
89                    )
90                }
91            } else if self.create {
92                RegCreateKeyExW(
93                    self.parent.0,
94                    pcwstr(path).as_ptr(),
95                    0,
96                    null(),
97                    self.options,
98                    self.access,
99                    null(),
100                    &mut handle,
101                    null_mut(),
102                )
103            } else {
104                RegOpenKeyExW(
105                    self.parent.0,
106                    pcwstr(path).as_ptr(),
107                    0,
108                    self.access,
109                    &mut handle,
110                )
111            }
112        };
113
114        win32_error(result).map(|_| Key(handle))
115    }
116}