int _tmain(int argc, _TCHAR* argv[])
{
backupReg("Software\\MyProgram", "c:\\reg.reg");
//MyProgram contains subkeys and values...
return 0;
}
BOOL backupReg(string subreg, string savepath)
{
HKEY keyHandle;
LRESULT result = RegOpenKeyExA(HKEY_CURRENT_USER, subreg.c_str(),0L,KEY_ALL_ACCESS,&keyHandle);
if(result != ERROR_SUCCESS)
{
MessageBoxA(0, "Couldn't open", "", 0);
return false;
}
SetPrivilege(SE_BACKUP_NAME,TRUE);
SetPrivilege(SE_RESTORE_NAME,TRUE);
// both ret true
result = RegSaveKeyExA(keyHandle, savepath.c_str(), NULL, REG_LATEST_FORMAT);
if(result != ERROR_SUCCESS)
{
MessageBoxA(0, "Couldn't save", "", 0);
return false;
}
RegCloseKey(keyHandle);
return true;
}
//Set Privilege
BOOL SetPrivilege(LPCTSTR lpszPrivilege, BOOL bEnablePrivilege)
{
TOKEN_PRIVILEGES tp;
LUID luid;
HANDLE hToken;
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
if ( !LookupPrivilegeValue(NULL, lpszPrivilege, &luid) )
return FALSE;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
return ( (GetLastError()!=ERROR_SUCCESS)?FALSE:TRUE);
}
|