About regdeletekey, the registry key cannot be deleted.

Source: Internet
Author: User

Recently, I think I have a lot of ideas, so I have been doing things. Because I have no experience, the progress is slow, but every time I see my own achievements, I will be very excited. This feeling is wonderful. In fact, I love one thing, you will feel that you are enjoying the results of doing this, rather than playing with you, and your interest is the motivation for learning. Okay.

Recently, when creating a program, you need to delete a registry key that contains a subitem. I have no experience writing this API, so I have taken a lot of detours. I forgive a cainiao for making a comment here.

The first function I thought of was regdeletekey. Do you think it would be hard for me to delete a registry? As a result, regcreatkey is used to open the item where the subitem to be deleted is registered and deleted with regdeletekey. However, the problem is that there is no problem in compiling and running, and it is successful, however, the Registry is not deleted, so I asked a question in the forum. After a few days, no one could give me a question. There were a lot of replies and a lot of points, haha. I accidentally saw a permission question about regdeletekey, So I thought there was a common problem with the API-permission, which I had encountered many times, that is, I could never learn from it. Okay, I changed the permission, used the regopenkeyex function, and granted the key_all_access permission to open the handle in the parameter (this can be done by yourself on msdn). I think there must be no problem? Compile and run ...... The results are still the same. At this time, it is quite depressing. Some friends on the Forum asked me to use the getlasterror function to check the cause of the error. I tried to return 0, that is, the result is running, and someone said to restart it, registry Modification usually needs to be restarted. I restarted again and the result is still the same. Why is the subitem not deleted? After thinking for a long time, I suspect that the function itself is -- regdeletekey. When I look online, I find that the Internet is talking about the storm. Some say that this function deletes the subitem and all the subitems under this subitem (a typical example is the API function encyclopedia), and some say, the registry key with subkeys cannot be deleted by the regdeletekey function. The subkey to be deleted must not have subkeys. to delete a key and all its subkeys, You need to enumerate the subkeys and delete them individually. to delete keys recursively, useRegdeletetreeOrShdeletekeyFunction. The general idea is that the regdeletekey function cannot delete a registry key containing subkeys. If you want to delete such items, you must enumerate them before deleting them. You can also use the regdeletetree or shdeletekey function. (I am not good at English. I 'd like to explain it to some extent ). The regdeletekey function cannot delete the items of the belt. No wonder it has never been deleted. The regdeletekey function can only be compiled on platforms above Vista, and I can only traverse and delete them one by one.

So I replied to my post online again. Hey hey, the problem nobody can solve. In fact, I know that many people know this simple problem. It's just a little stingy and I don't want to tell you a joke. I accidentally saw a brother's problem, which was the same as mine. Haha, I hope you can see my blog.

If you look at a small problem, I am confused. In fact, we say that no matter what we do, we need to be careful and start from a small perspective, especially when you are a rookie, you cannot give up on it because it is very small, or else he will give up on you when it is important.

In the end, I hope that cainiao like me can learn well and work well.

Appendix:

Delete the registry key of the entry (the regdeletekey function is not used)

Bool nosafemodle (void)

{

Hkey;

DWORD count;

Lptstr data1 = _ T ("system // CurrentControlSet // control // safeboot // minimal ");

Lptstr data2 = _ T ("system // CurrentControlSet // control // safeboot // network ");

/* If (regcreatekey (HKEY_CURRENT_USER, Data, & hkey) = error_success)

{

Regdeletekey (hkey, _ T ("minimal "));

Regdeletekey (hkey, _ T ("network "));

}*/

If (regopenkeyex (HKEY_LOCAL_MACHINE, data1, 0, key_all_access, & hkey) = error_success)

{

If (regqueryinfokey (hkey, null, & count, null) = error_success)

{

DWORD dwindex = 0;

Char szname [256] = {0 };

For (; dwindex <count; dwindex ++)

{

If (regenumkey (hkey, dwindex, szname, 257) = error_success)

{

Regdeletekey (hkey, (const char *) szname );

}

}

}

}

If (regopenkeyex (HKEY_LOCAL_MACHINE, data2, 0, key_all_access, & hkey) = error_success)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.