Skip to content

Commit

Permalink
delete_subkey_all() does not delete itself when subkey's path is empty.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaasuoliweng committed Oct 5, 2017
1 parent e5cda21 commit f0ecaa1
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ impl RegKey {
EnumValues{key: self, index: 0}
}

/// Delete key. Cannot delete if it has subkeys.
/// Will delete itself if `path` is an empty string.
/// Delete key.Key names are not case sensitive.
/// Cannot delete if it has subkeys.
/// Use `delete_subkey_all` for that.
///
/// # Examples
Expand All @@ -460,7 +460,7 @@ impl RegKey {
match unsafe {
advapi32::RegDeleteKeyW(
self.hkey,
c_path.as_ptr(),
c_path.as_ptr(), //This parameter cannot be NULL.
) as DWORD
} {
0 => Ok(()),
Expand All @@ -475,11 +475,11 @@ impl RegKey {
match unsafe {
advapi32::RegDeleteKeyTransactedW(
self.hkey,
c_path.as_ptr(),
c_path.as_ptr(), //The value of this parameter cannot be NULL.
0,
0,
t.handle,
ptr::null_mut(),
ptr::null_mut(),
) as DWORD
} {
0 => Ok(()),
Expand All @@ -489,6 +489,7 @@ impl RegKey {

/// Recursively delete subkey with all its subkeys and values.
/// Will delete itself if `path` is an empty string.
/// If `path` is an empty string, the subkeys and values of this key are deleted.
///
/// # Examples
///
Expand All @@ -499,11 +500,18 @@ impl RegKey {
/// .delete_subkey_all("Software\\MyProduct").unwrap();
/// ```
pub fn delete_subkey_all<P: AsRef<OsStr>>(&self, path: P) -> io::Result<()> {
let c_path = to_utf16(path);
let c_path;
let path_ptr;
if path.as_ref().is_empty(){
path_ptr = ptr::null();
}else{
c_path = to_utf16(path);
path_ptr = c_path.as_ptr();
}
match unsafe{
advapi32::RegDeleteTreeW(
self.hkey,
c_path.as_ptr(),
path_ptr,//If this parameter is NULL, the subkeys and values of this key are deleted.
) as DWORD
} {
0 => Ok(()),
Expand Down

0 comments on commit f0ecaa1

Please sign in to comment.