Question : C++ Exporting Registry Branch

Good-day;

I am having some troubles exporting a registry branch in C++.  The code below will export a branch, however it is in an unusable format, it looks to be a binary file...

What I am trying to accomplish is the end result of the saved registry file to be in the same format as if I selected "File -> Export" in the Registry Editor.

Thank you for your help and time;
Kind regards
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
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);
}

Answer : C++ Exporting Registry Branch

Sorry, have been away from my email for a while....

Yes, you can use RegSaveKeyEx and RegLoadKey. That IS what they are intended for, it is just that the result of RegSaveKeyEx is not in the format that you were after in your question as it was originally stated.
Random Solutions  
 
programming4us programming4us