Exporting registry key to a reg file?

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

I've had a look around and AFAICT, the Microsoft.Win32.RegistryKey class
doesn't have any members that write the registry to a reg file. So, as I see
it, the options are:

1. Use Microsoft.Win32.RegistryKey to parse each value and write each to a
reg file manually. This is a bit of a pain as I have to write all the code
to keep the syntax of the resultant reg file correct.
2. Shell out and use regedit /e to export the file. Not ideal because when I
do this, I believe that a separate thread is used, so my app carries on
working whilst the registry is still being exported (noticeable for large
exports). OK, I could monitor all this, but again, this seems like really
hard work for such a simple job!

3. Use "advapi32.dll". This is my preferred option (assuming I'm right about
point 1). However, I'm getting errors when running the code:

[DllImport("advapi32.dll", EntryPoint="RegSaveKey")]
public static extern int RegSaveKey(IntPtr hKey, string lpFile, int
lpSecurityAttributes);
IntPtr hKey;
IntPtr hHkcu = new IntPtr((int)RegistryHive.CurrentUser);
int ret = Win32.RegOpenKey(hHkcu, registrykey, out hKey);
if(ret == Win32.SUCCESS)
{
Console.WriteLine("Return code: " + RegSaveKey(hHkcu, @"C:\regbackup.reg",
0).ToString());
}

This compiles fine, and it appears to be trying to do what it's supposed to
do. E.g. if I ensure that the file already exists, I get return code (from
RegOpenKey) 183, which is what the documentation to RegSaveKey says I should
get (file already exists, roughly). So, if I then make sure that the file
doesn't exist, instead of getting return code 0, I get 1314, which seems to
be a permission problem? However, I am an administrator.

If I do the same from the command prompt using regedit, it exports fine.

Any idea?

Cheers
Mark
 
Mark,

The MSDN docs say you need the SE_BACKUP_NAME privilege enabled. Even
if you're an admin you may have to enable this explicitly with
AdjustTokenPrivileges and related functions first.



Mattias
 
Mattias Sjögren said:
Mark,

The MSDN docs say you need the SE_BACKUP_NAME privilege enabled. Even
if you're an admin you may have to enable this explicitly with
AdjustTokenPrivileges and related functions first.

Indeed it does - I didn't spot that. However, it still doesn't work:

1. The SE_BACKUP_NAME privilege maps to SeBackupPrivilege, which is
displayed as "Backup files and directories" in the gp editor. This user
rights has two members: Administrators and Backup Operators. I am already a
member of the former, so I should already have this privilege.

2. I did write some code to add in the privelege anyway, using
OpenProcessToken, LookupPrivilegeValue and AdjustTokenPrivileges. The extra
code ran fine without errors, but I still get error 1314, when running
RegSaveKey.

Any further ideas?

Cheers
Mark
 
Mark said:
After much messing around with permissions, RegOpenKeyEx (KEY_ALL_ACCESS),
NTFS premissions, you name it, I tried it... I was still getting error
1314. I guess that it's just not possible to use RegSaveKey from a managed
environment? So... I went for the clunky option 2 (shell out to
regedit.exe)! Works fine.

Update:

Thought you might want to know - I found where I was going wrong in code.
Nothing to do with RegSaveKey or any of the other dllimports. Simple error
in the TOKEN_PRIVILEGES struct. I had:

[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{
public LUID Luid;
public int Attributes;
public int PrivilegeCount;
}

It should have been:

[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{
public int PrivilegeCount;
public LUID Luid;
public int Attributes;
}

D'oh! [Slap forehead]

[snip]
 
Back
Top