-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract regedit mod (#100)
- Loading branch information
Showing
3 changed files
with
90 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use anyhow::{anyhow, bail, Result}; | ||
use windows::core::PCWSTR; | ||
use windows::Win32::Foundation::ERROR_FILE_NOT_FOUND; | ||
use windows::Win32::System::Registry::{ | ||
RegCloseKey, RegDeleteValueW, RegGetValueW, RegOpenKeyExW, RegSetValueExW, HKEY, | ||
HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_SZ, REG_VALUE_TYPE, RRF_RT_REG_SZ, | ||
}; | ||
|
||
pub struct RegKey { | ||
hkey: HKEY, | ||
} | ||
|
||
impl RegKey { | ||
pub fn new_hkcu(subkey: PCWSTR) -> Result<RegKey> { | ||
let mut hkey = HKEY::default(); | ||
unsafe { | ||
RegOpenKeyExW( | ||
HKEY_CURRENT_USER, | ||
subkey, | ||
0, | ||
KEY_ALL_ACCESS, | ||
&mut hkey as *mut _, | ||
) | ||
} | ||
.ok() | ||
.map_err(|err| anyhow!("Fail to open reg key, {:?}", err))?; | ||
Ok(RegKey { hkey }) | ||
} | ||
|
||
pub fn get_value(&self, name: PCWSTR) -> Result<Option<Vec<u16>>> { | ||
let mut buffer = [0u16; 1024]; | ||
let mut size: u32 = (1024 * std::mem::size_of_val(&buffer[0])) as u32; | ||
let mut kind: REG_VALUE_TYPE = Default::default(); | ||
let ret = unsafe { | ||
RegGetValueW( | ||
self.hkey, | ||
None, | ||
name, | ||
RRF_RT_REG_SZ, | ||
Some(&mut kind), | ||
Some(buffer.as_mut_ptr() as *mut _), | ||
Some(&mut size), | ||
) | ||
}; | ||
if ret.is_err() { | ||
if ret == ERROR_FILE_NOT_FOUND { | ||
return Ok(None); | ||
} | ||
bail!( | ||
"Fail to get reg value, {:?}", | ||
windows::core::Error::from(ret) | ||
); | ||
} | ||
let len = (size as usize - 1) / 2; | ||
Ok(Some(buffer[..len].to_vec())) | ||
} | ||
|
||
pub fn set_value(&self, name: PCWSTR, value: &[u8]) -> Result<()> { | ||
unsafe { RegSetValueExW(self.hkey, name, 0, REG_SZ, Some(value)) } | ||
.ok() | ||
.map_err(|err| anyhow!("Fail to write reg value, {:?}", err))?; | ||
Ok(()) | ||
} | ||
|
||
pub fn delete_value(&self, name: PCWSTR) -> Result<()> { | ||
unsafe { RegDeleteValueW(self.hkey, name) } | ||
.ok() | ||
.map_err(|err| anyhow!("Failed to delete reg value, {:?}", err))?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Drop for RegKey { | ||
fn drop(&mut self) { | ||
let _ = unsafe { RegCloseKey(self.hkey) }; | ||
} | ||
} |