c++备份与恢复注册表-错误记录

来源:互联网 发布:二叉树的遍历算法c 编辑:程序博客网 时间:2024/06/02 04:34

在学习用c++进行注册表备份与恢复时。

参考网上资料进行学习。

http://www.cnblogs.com/john-h/p/5886870.html

备份与恢复均要申请权限。

void RegistryFunctionLib_class::Get_SE_BACKUP_NAME_Power(){HANDLE   hToken = NULL;LUID sedebugnameValue;TOKEN_PRIVILEGES   tkp;if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)){        //error}if (!LookupPrivilegeValue(NULL, SE_BACKUP_NAME, &sedebugnameValue)){//error}tkp.PrivilegeCount = 1;tkp.Privileges[0].Luid = sedebugnameValue;tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL)){//error}CloseHandle(hToken);}
以管理员权限运行程序后备份成功,而在进行恢复时却始终不成功。

于是去msdn官网查询函数RegRestoreKey,发现标识位并非是true或false。

 _In_ DWORD   dwFlags

dwFlags [in]

The flags that indicate how the key or keys are to be restored. This parameter can be one of the following values.

ValueMeaning
REG_FORCE_RESTORE
0x00000008L

If specified, the restore operation is executed even if open handles exist at or beneath the location in the registry hierarchy to which thehKey parameter points.

REG_WHOLE_HIVE_VOLATILE
0x00000001L

If specified, a new, volatile (memory only) set of registry information, or hive, is created. If REG_WHOLE_HIVE_VOLATILE is specified, the key identified by thehKey parameter must be either theHKEY_USERS orHKEY_LOCAL_MACHINE value.

REG_FORCE_RESTORE=永久恢复,REG_WHOLE_HIVE_VOLATILE=临时恢复。


设置后依旧不成功,阅读msdn文档介绍发现,进行恢复时需要将SE_BACKUP_NAME替换为SE_RESTORE_NAME,在

if (!LookupPrivilegeValue(NULL, SE_BACKUP_NAME,&sedebugnameValue))中


The calling process must have the SE_RESTORE_NAME and SE_BACKUP_NAME privileges on the computer in which the registry resides. For more information, see Running with Special Privileges.


附上msdn链接https://msdn.microsoft.com/en-us/library/windows/desktop/ms724915(v=vs.85).aspx

原创粉丝点击