| 1 |
use std::{ |
| 2 |
alloc::{self, Layout}, |
| 3 |
ffi::OsStr, |
| 4 |
io::ErrorKind, |
| 5 |
mem, |
| 6 |
ptr::{null, null_mut}, |
| 7 |
}; |
| 8 |
|
| 9 |
use windows_sys::{ |
| 10 |
core::GUID, |
| 11 |
Win32::{ |
| 12 |
Foundation::{ERROR_SUCCESS, S_OK}, |
| 13 |
System::{ |
| 14 |
Com::IIDFromString, |
| 15 |
Registry::{RegCloseKey, RegQueryValueExW, HKEY, REG_MULTI_SZ, REG_SZ}, |
| 16 |
}, |
| 17 |
}, |
| 18 |
}; |
| 19 |
|
| 20 |
use crate::Error; |
| 21 |
|
| 22 |
use super::util::WCString; |
| 23 |
|
| 24 |
pub struct RegKey(HKEY); |
| 25 |
|
| 26 |
impl RegKey { |
| 27 |
pub unsafe fn new(k: HKEY) -> RegKey { |
| 28 |
RegKey(k) |
| 29 |
} |
| 30 |
|
| 31 |
pub fn query_value_guid(&self, value_name: &str) -> Result<GUID, Error> { |
| 32 |
unsafe { |
| 33 |
let value_name: WCString = OsStr::new(value_name).into(); |
| 34 |
let mut ty = 0; |
| 35 |
let mut size = 0; |
| 36 |
|
| 37 |
|
| 38 |
let r = RegQueryValueExW( |
| 39 |
self.0, |
| 40 |
value_name.as_ptr(), |
| 41 |
null_mut(), |
| 42 |
&mut ty, |
| 43 |
null_mut(), |
| 44 |
&mut size, |
| 45 |
); |
| 46 |
|
| 47 |
if r != ERROR_SUCCESS { |
| 48 |
return Err(Error::from_raw_os_error(r as i32)); |
| 49 |
} |
| 50 |
|
| 51 |
if ty != REG_MULTI_SZ && ty != REG_SZ { |
| 52 |
return Err(Error::new( |
| 53 |
ErrorKind::InvalidInput, |
| 54 |
"registry value type not string", |
| 55 |
)); |
| 56 |
} |
| 57 |
|
| 58 |
let layout = Layout::from_size_align(size as usize, mem::align_of::<u16>()).unwrap(); |
| 59 |
|
| 60 |
let buf = alloc::alloc(layout); |
| 61 |
|
| 62 |
let r = RegQueryValueExW(self.0, value_name.as_ptr(), null(), &mut ty, buf, &mut size); |
| 63 |
|
| 64 |
if r != ERROR_SUCCESS { |
| 65 |
alloc::dealloc(buf, layout); |
| 66 |
return Err(Error::from_raw_os_error(r as i32)); |
| 67 |
} |
| 68 |
|
| 69 |
let mut guid = GUID::from_u128(0); |
| 70 |
let r = IIDFromString(buf as *mut u16, &mut guid); |
| 71 |
|
| 72 |
alloc::dealloc(buf, layout); |
| 73 |
|
| 74 |
if r == S_OK { |
| 75 |
Ok(guid) |
| 76 |
} else { |
| 77 |
Err(Error::new(ErrorKind::InvalidData, "invalid UUID")) |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
impl Drop for RegKey { |
| 84 |
fn drop(&mut self) { |
| 85 |
unsafe { |
| 86 |
RegCloseKey(self.0); |
| 87 |
} |
| 88 |
} |
| 89 |
} |