Skip to content
Merged
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
11 changes: 7 additions & 4 deletions crates/host_env/src/winreg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,22 +422,25 @@ pub fn query_default_value(

pub fn query_value_bytes(
hkey: Registry::HKEY,
wide_name: &widestring::WideCStr,
wide_name: Option<&widestring::WideCStr>,
) -> Result<(Vec<u8>, u32), u32> {
let mut buf_size: u32 = 0;
let res = unsafe {
query_value_ex(
hkey,
Some(wide_name),
wide_name,
core::ptr::null_mut(),
core::ptr::null_mut(),
&mut buf_size,
)
};
if res == Foundation::ERROR_MORE_DATA || buf_size == 0 {
if res == Foundation::ERROR_MORE_DATA {
buf_size = 256;
} else if res != 0 {
return Err(res);
} else if buf_size == 0 {
// Keep a nonzero buffer so a value that grows between calls can retry.
buf_size = 256;
}

let mut ret_buf = vec![0u8; buf_size as usize];
Expand All @@ -448,7 +451,7 @@ pub fn query_value_bytes(
let res = unsafe {
query_value_ex(
hkey,
Some(wide_name),
wide_name,
&mut typ,
ret_buf.as_mut_ptr(),
&mut ret_size,
Expand Down
24 changes: 21 additions & 3 deletions crates/vm/src/stdlib/winreg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,10 +641,28 @@ mod winreg {
}

#[pyfunction]
fn QueryValueEx(key: HKEYArg, name: String, vm: &VirtualMachine) -> PyResult<PyRef<PyTuple>> {
fn QueryValueEx(
key: HKEYArg,
name: Option<PyRef<PyStr>>,
vm: &VirtualMachine,
) -> PyResult<PyRef<PyTuple>> {
let hkey = key.0;
let wide_name = WideCString::from_str(name).map_err(|e| e.to_pyexception(vm))?;
let (ret_buf, typ) = host_winreg::query_value_bytes(hkey, &wide_name)
let wide_name = name
.as_ref()
.map(|name| {
name.as_wtf8()
.to_wide_cstring()
.map_err(|_| nul_char_error(vm))
})
.transpose()?;
vm.audit("winreg.QueryValue", || {
let name: PyObjectRef = wide_name.as_ref().map_or_else(
|| vm.ctx.none(),
|name| vm.ctx.new_str(Wtf8Buf::from_wide(name.as_slice())).into(),
);
(hkey as isize, vm.ctx.none(), name)
})?;
let (ret_buf, typ) = host_winreg::query_value_bytes(hkey, wide_name.as_deref())
.map_err(|err| os_error_from_windows_code(vm, err as i32))?;
let obj = reg_to_py(vm, &ret_buf, typ)?;
// Return tuple (value, type)
Expand Down
Loading