How in c++ builder export registry to *.reg file?
I'm use reg->SaveKey("Software", "D:\1.reg"). But getting empty file, without data.
void开发者_StackOverflow中文版 __fastcall TForm1::Button2Click(TObject *Sender)
{
TRegistry *reg=new TRegistry(KEY_READ);
reg->RootKey=HKEY_LOCAL_MACHINE;
reg->OpenKey("Software",0);;
reg->SaveKey("Software","D:\\1.reg");
delete reg;
}
SaveKey
is a loose wrapper around RegSaveKey()
, the documentation of which states:
The calling process must have the
SE_BACKUP_NAME
privilege enabled. For more information, see Running with Special Privileges.
User tokens do not normally have the SE_BACKUP_NAME
privilege enabled. In order to meet this requirement you need to:
- Run as administrator.
- Add the
SE_BACKUP_NAME
privilege to your user token.
The other requirement you must adhere to is that the output file must not exist before you call SaveKey
.
See this EDN article for C++ code illustrating the method.
Next variant worked!
void __fastcall TForm1::Button2Click(TObject *Sender)
{
TRegistry *reg=new TRegistry(KEY_READ);
HANDLE ProcessToken;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &ProcessToken))
{
SetPrivilege(ProcessToken, SE_BACKUP_NAME, TRUE);
TRegistry *reg=new TRegistry(KEY_READ);
reg->RootKey=HKEY_LOCAL_MACHINE;
reg->SaveKey("Software","D:\\1.reg");
delete reg;
}
}
精彩评论