A detailed introduction to the registry key values that have disappeared based on restart _c language

Source: Internet
Author: User
Today encountered a bug, toss a half-day to solve, share to everyone.
Bug Description
One developer calls the following code to create a registry key value:
Copy Code code as follows:

HKEY hkey;
if (:: RegOpenKeyEx (HKEY_CURRENT_USER, Demoregkey, 0, Key_write, &hkey)!= error_success)
{
RegCreateKeyEx (HKEY_CURRENT_USER, Demoregkey, 0, NULL, reg_option_volatile
, key_all_access, NULL, &hkey, NULL);
}

After execution, the key value is successfully seen in Registry Editor, followed by the code from which to get the value, and so on.
After submitting the test, it was found that after the system was restarted, the registry keys created by the above program were gone, causing the code to read the key values behind the error.
Why?
Then I tweaked the code and found that the following code had been restarted with a 2 error code.
Copy Code code as follows:

LONG Lret;
Lret =:: RegOpenKeyEx (HKEY_CURRENT_USER, Demoregkey, 0, Key_write, &hkey);

The value of executing this code Lret is 2. Error code 2 means that the system cannot find the specified file. For a registry function, the corresponding key does not exist.
After debugging, it was found that the code to create the registry was executed successfully and can be seen in the registry. Also, call the code that reads the key value, and there is no problem. But once the computer is restarted, the path does not exist, and there are no key values stored in it.
It seems that some of the programs deleted my key value, so I found all the code that deleted the key value, and I didn't find it.
Remove all programs before restarting. But after the reboot, the damn key value still doesn't appear.
It appears to have been deleted by the system or other programs. Is my key value the same as the key value of some programs? OK, this time with the GUID to do the key, or reboot after No.
Wow, that's crazy.
At this point, I looked back and looked at all the code that created the registry, because my program must have caused the key value to disappear.
I noticed the strange parameters of Reg_option_volatile.
After MSDN, I finally found the reason that this parameter is caused by.
Reason
Reg_option_volatile This parameter means that the registry key value created is in memory and will not be saved to the appropriate registry file.
English is as follows:
Copy Code code as follows:

All registry keys are created as volatile, and the information are stored in memory and isn't preserved when the Correspon Ding registry hive is unloaded. For HKEY_LOCAL_MACHINE, this occurs the OS are shut down. The Regsavekey function does not save volatile registry keys. This flag is ignored for the keys that already exist.

So, of course, these key values are gone after the reboot.
Solutions
Very simple, use Reg_option_non_volatileCan
Copy Code code as follows:

RegCreateKeyEx (HKEY_CURRENT_USER, Demoregkey, 0, NULL, reg_option_non_volatile
, key_all_access, NULL, &hkey, NULL);

This can be used on the test. A reboot, the previously created key value is gone.

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.