Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete_subkey_all() does not delete itself when subkey's path is empty. #14

Merged
merged 2 commits into from
Nov 12, 2017
Merged
Changes from 1 commit
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
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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? I've just checked and it looks like RegDeleteTreeW behaves the same way with NULL pointer and pointer to an empty string in second argument. It deletes all subkeys and values in both cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"NULL pointer " and empty sting get the same result. The reason I add these codes is MSDN told me to to do that. BTY this avoid mem alloc in to_utf16().

}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