Skip to content

Commit

Permalink
Add from_raw to windows-registry (microsoft#3034)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored and mati865 committed Jun 15, 2024
1 parent 31a4ec4 commit 8f65cc9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion crates/libs/core/src/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ impl<T: Free> Owned<T> {
/// Takes ownership of the handle.
///
/// # Safety
/// The handle must be owned by the caller and safe to free.
///
/// The handle must be owned by the caller and safe to free.
pub unsafe fn new(x: T) -> Self {
Self(x)
}
Expand Down
10 changes: 10 additions & 0 deletions crates/libs/registry/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ impl Key {
win32_error(result).map(|_| Self(handle))
}

/// Constructs a registry key from an existing handle.
///
/// # Safety
///
/// This function takes ownership of the handle.
/// The handle must be owned by the caller and safe to free with `RegCloseKey`.
pub unsafe fn from_raw(handle: isize) -> Self {
Self(handle)
}

/// Returns the underlying registry key handle.
pub fn as_raw(&self) -> isize {
self.0
Expand Down
6 changes: 5 additions & 1 deletion crates/tests/registry/tests/sys_interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ fn sys_interop() -> Result<()> {
key.set_u32("2", 2)?;
key.set_u32("3", 3)?;

let raw: HKEY = key.as_raw();
std::mem::forget(key);
let owned = unsafe { Key::from_raw(raw) };

let mut count = 0;

unsafe {
RegQueryInfoKeyW(
key.as_raw(),
owned.as_raw(),
std::ptr::null_mut(),
std::ptr::null_mut(),
std::ptr::null_mut(),
Expand Down
6 changes: 5 additions & 1 deletion crates/tests/registry/tests/windows_interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ fn windows_interop() -> Result<()> {
key.set_u32("2", 2)?;
key.set_u32("3", 3)?;

let raw = HKEY(key.as_raw());
std::mem::forget(key);
let owned = unsafe { Key::from_raw(raw.0) };

let mut count = 0;

unsafe {
RegQueryInfoKeyW(
HKEY(key.as_raw()),
HKEY(owned.as_raw()),
PWSTR::null(),
None,
None,
Expand Down

0 comments on commit 8f65cc9

Please sign in to comment.