1

1.) 如何从 C# 为注册表加载、编辑和保存二进制 Hive 文件?

我找到了这个 Win32 api。 http://msdn.microsoft.com/en-us/library/ee210770%28VS.85%29.aspx

这个人分享了将二进制 Hive 文件的内容转储为文本的代码。 http://www.codeproject.com/KB/recipes/RegistryDumper.aspx

2.) 除了操作 Hive 文件之外,我还搜索了一种在运行时使用 C# 将 Hive 文件加载到注册表中的方法(类似于 regedit 中许多文件上的 Load Hive 和 Unload Hive 命令)

/谢谢

4

4 回答 4

1

您是否查看过 Microsoft.Win32 中的 Registry 和 RegistryKey 类?

http://msdn.microsoft.com/en-us/library/microsoft.win32.aspx

听起来您可能需要创建自己的表示来读取配置单元文件并排队或立即进行相应的注册表更改。同样,您需要将自己的转换器写回磁盘。

于 2011-04-25T19:35:00.493 回答
1

下面的文章解释了如何在不使用 WinAPI (advapi32.dll) 的情况下分析注册表文件。在这种特殊情况下,这个人正在使用 Mono:

http://volatile-minds.blogspot.com/2011/01/analyzing-windows-nt-registry-without.html

using (FileStream fs = File.OpenRead (path)) {
 var data = new byte[checked((int)fs.Length)];
 int i = 0;
 int read;

 using (var ms = new MemoryStream (checked((int)fs.Length))) {

  while ((read = fs.Read (data, 0, data.Length)) > 0) {
   ms.Write (data, 0, read);
   i += read;
  }

  byte[] hive = ms.ToArray ();
  char[] cList = new char[fs.Length];

  i = 0;
  foreach (byte b in hive)
   cList[i++] = (char)b;

         string d = new string (cList);


  int all = 0;

  foreach (Match mx in lf.Matches (d)) { //you can change out the regex you want here.
   byte[] bb = new byte[mx.Value.Length];
   char[] cb = new char[mx.Value.Length];

   for (int k = 0; k < mx.Value.Length; k++) {
    bb[k] = (byte)mx.Value[k];
    cb[k] = (char)bb[k];

   }

   all++;

   //Console.WriteLine (new string (cb));
  }

  Console.WriteLine (all.ToString ());
  all = 0;
 }
}
于 2012-02-02T14:40:50.597 回答
0

这是 9 岁,但我认为这可以帮助其他人。我写了这个类,它允许你做这样的事情:

Hive.AcquirePrivileges() // Acquires the privileges necessary for loading the hive
Hive myregistryhive = Hive.LoadFromFile("hivepathhere") // Loads the hive
// use myregistryhive.RootKey (a RegistryKey), read and/or write to it and its sub keys
myregistryhive.SaveAndUnload() // Unloads the hive
Hive.ReturnPrivileges() // De-elevate back to normal privileges.

类的代码:

class Hive
{
    [DllImport("advapi32.dll", SetLastError = true)]
    static extern int RegLoadKey(IntPtr hKey, string lpSubKey, string lpFile);

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern int RegSaveKey(IntPtr hKey, string lpFile, uint securityAttrPtr = 0);

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern int RegUnLoadKey(IntPtr hKey, string lpSubKey);

    [DllImport("ntdll.dll", SetLastError = true)]
    static extern IntPtr RtlAdjustPrivilege(int Privilege, bool bEnablePrivilege, bool IsThreadPrivilege, out bool PreviousValue);

    [DllImport("advapi32.dll")]
    static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, ref UInt64 lpLuid);

    [DllImport("advapi32.dll")]
    static extern bool LookupPrivilegeValue(IntPtr lpSystemName, string lpName, ref UInt64 lpLuid);

    private RegistryKey parentKey;
    private string name;
    private string originalPath;
    public RegistryKey RootKey;

    private Hive() { }

    public static Hive LoadFromFile(string Path)
    {
        Hive result = new Hive();

        result.parentKey = RegistryKey.OpenBaseKey(RegistryHive.Users, RegistryView.Default);
        result.name = Guid.NewGuid().ToString();
        result.originalPath = Path;
        IntPtr parentHandle = result.parentKey.Handle.DangerousGetHandle();
        RegLoadKey(parentHandle, result.name, Path);
        //Console.WriteLine(Marshal.GetLastWin32Error());
        result.RootKey = result.parentKey.OpenSubKey(result.name, true);
        return result;
    }
    public static void AcquirePrivileges()
    {
        ulong luid = 0;
        bool throwaway;
        LookupPrivilegeValue(IntPtr.Zero, "SeRestorePrivilege", ref luid);
        RtlAdjustPrivilege((int)luid, true, false, out throwaway);
        LookupPrivilegeValue(IntPtr.Zero, "SeBackupPrivilege", ref luid);
        RtlAdjustPrivilege((int)luid, true, false, out throwaway);
    }
    public static void ReturnPrivileges()
    {
        ulong luid = 0;
        bool throwaway;
        LookupPrivilegeValue(IntPtr.Zero, "SeRestorePrivilege", ref luid);
        RtlAdjustPrivilege((int)luid, false, false, out throwaway);
        LookupPrivilegeValue(IntPtr.Zero, "SeBackupPrivilege", ref luid);
        RtlAdjustPrivilege((int)luid, false, false, out throwaway);
    }
    public void SaveAndUnload()
    {
        RootKey.Close();
        RegUnLoadKey(parentKey.Handle.DangerousGetHandle(), name);
        parentKey.Close();
    }
}

编辑:请注意,这需要管理员权限。

于 2020-06-14T16:09:38.067 回答
-2

请参阅:https ://github.com/brandonprry/volatile_reader

它使用 GTK 接口读取 C# 中的离线配置单元。虽然还没有写支持。

于 2012-11-05T19:30:52.003 回答