重启电脑创建的注册表键值丢失原因
REG_OPTION_VOLATILE 这个参数的意思是创建的注册表键值都位于内存中,不会保存到相应的注册表文件中。
英文如下:
All registry keys are created as volatile, and the information is stored in memory and is not preserved when the corresponding registry hive is unloaded. For HKEY_LOCAL_MACHINE, this occurs when the OS is shut down. TheRegSaveKey function does not save volatile registry keys. This flag is ignored for keys that already exist.
所以,重启后这些键值当然就没有了。
解决办法
很简单,使用REG_OPTION_NON_VOLATILE 即可
RegCreateKeyEx(HKEY_CURRENT_USER, DemoRegKey, 0, NULL, REG_OPTION_NON_VOLATILE , KEY_ALL_ACCESS , NULL , &hKey, NULL);
REG_OPTION_VOLATILE可以用在测试上。一重启,之前创建的键值都没了。
而若想重启之后注册表键值也仍然保留的话就用REG_OPTION_NON_VOLATILE。
REG_OPTION_NON_VOLATILE:
该宏是RegCreateKeyEx函数的参数可选项。
LONG RegCreateKeyEx( HKEY hKey,
LPCWSTR lpSubKey,
DWORD Reserved,
LPWSTR lpClass,
DWORD dwOptions,
REGSAM samDesired,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
PHKEY phkResult,
LPDWORD lpdwDisposition );
看看MSDN对它的说明:
Default setting. All registry keys are created as non-volatile and the information stored in memory is preserved when the OS is restarted. The RegSaveKey function saves keys that are non-volatile.
翻译:默认设置。 所有注册表项 创建 作为非易失性 和 操作系统 重新启动 时保留 在内存 中存储的信息 。 RegSaveKey 功能 键 是 非易失性 保存 。
意思是说如果参数 dwOptions 选择REG_OPTION_NON_VOLATILE,则通过RegCreateKeyEx创建的注册表是永久的,不会因为计算机的重启而消失。相反参数选择REG_OPTION_VOLATILE,则表明创建的注册表是临时的,计算机重启后该注册表不存在。
Value | Description |
---|---|
REG_OPTION_NON_VOLATILE | Default setting. All registry keys are created as non-volatile and the information stored in memory is preserved when the OS is restarted. The RegSaveKey function saves keys that are non-volatile. |
REG_OPTION_VOLATILE | All registry keys are created as volatile, and the information is stored in memory and is not preserved when the corresponding registry hive is unloaded. For HKEY_LOCAL_MACHINE, this occurs when the OS is shut down. The RegSaveKey function does not save volatile registry keys. This flag is ignored for keys that already exist. |