开发者

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:

  1. Run as administrator.
  2. 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;
}



}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜