使用C#.net挂载其他用户配置单元

时间:2013-06-17 13:23:54

标签: c# windows registry hive

我正在编写一个应用程序,它将为每个选定的用户编写一些注册表项。

我想知道是否有一种正确的方法来安装另一个用户的配置单元来写入。

目前,我正在使用“REG LOAD”安装每个蜂巢。它很实用,但很麻烦。

任何想法?

提前感谢您的回答。

干杯。

---编辑19.06.2013 ---

好的,感谢您的帮助,我能够调用该函数,但未经授权安装注册表。

我认为这是一个缺少的权限,并强制它在管理员中运行。

我仍然收到0x522错误,这意味着,根据MSDN,我没有权利安装配置单元。

我在网上搜索并发现了不同的解释和可能性,但我还是无法安装蜂巢。

我是C#developpement和Windows API的新手......

这是我试图理解并在我的测试中使用的代码。

我错过了什么吗?

提前感谢您的回答。

namespace mountregistry2
{


    public partial class Form1 : Form
    {

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

        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
            public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess,
            ref int tokenhandle);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
            public static extern int GetCurrentProcess();

        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
            public static extern int LookupPrivilegeValue(string lpsystemname, string lpname,
            [MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
            public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs,
            [MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGES Newstate, int bufferlength,
            int PreivousState, int Returnlength);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern int RegLoadKey(uint hKey, string lpSubKey, string lpFile);

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


        public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
        public const int TOKEN_QUERY = 0x00000008;
        public const int SE_PRIVILEGE_ENABLED = 0x00000002;
        public const string SE_RESTORE_NAME = "SeRestorePrivilege";
        public const string SE_BACKUP_NAME = "SeBackupPrivilege";
        public const uint HKEY_USERS = 0x80000003;
        public string shortname;
        bool unloaded = false;

        private void testmountregistry()
        {
            int token = 0;
            int retval = 0;

            TOKEN_PRIVILEGES TokenPrivileges1 = new TOKEN_PRIVILEGES();
            TOKEN_PRIVILEGES TokenPrivileges2 = new TOKEN_PRIVILEGES();
            LUID RestoreLuid = new LUID();
            LUID BackupLuid = new LUID();

            retval = GetCurrentProcess();
            MessageBox.Show(retval.ToString("X")); //returns FFFFFFFF, which should work

            retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
            MessageBox.Show(retval.ToString("X"));//RETURNS 1

            retval = LookupPrivilegeValue(null, SE_RESTORE_NAME, ref RestoreLuid);
            MessageBox.Show(retval.ToString("X"));//Returns 1

            retval = LookupPrivilegeValue(null, SE_BACKUP_NAME, ref BackupLuid);
            MessageBox.Show(retval.ToString("X"));//Returns 1

            TokenPrivileges1.PrivilegeCount = 1;
            TokenPrivileges1.Attributes = SE_PRIVILEGE_ENABLED;
            TokenPrivileges1.Luid = RestoreLuid;

            TokenPrivileges2.PrivilegeCount = 1;
            TokenPrivileges2.Attributes = SE_PRIVILEGE_ENABLED;
            TokenPrivileges2.Luid = BackupLuid;

            retval = AdjustTokenPrivileges(token, 0, ref TokenPrivileges1, 1024, 0, 0);
            MessageBox.Show(retval.ToString("X"));//Returns 1

            retval = AdjustTokenPrivileges(token, 0, ref TokenPrivileges2, 1024, 0, 0);
            MessageBox.Show(retval.ToString("X"));//Returns 1

            uint hkey_users = 0x80000003;
            int interror = RegLoadKey(hkey_users, "test", @"C:\Users\Public\NTUSER.DAT");
            MessageBox.Show(interror.ToString("X"));//Return 0x522

            return;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用平台调用来调用RegLoadKey API method

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