Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#.net装载其他用户配置单元_C#_Windows_Registry_Hive - Fatal编程技术网

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

使用C#.net装载其他用户配置单元,c#,windows,registry,hive,C#,Windows,Registry,Hive,我正在编写一个应用程序,它将为每个选定的用户编写一些注册表项 我想知道是否有一种合适的方法来挂载另一个用户的配置单元来写入它 目前,我正在使用“REG LOAD”安装每个蜂巢。它很实用,但很凌乱 有什么想法吗 提前感谢您的回答 干杯 ---2013年6月19日编辑--- 好的,谢谢你的帮助,我可以调用该函数,但未经授权装载注册表 我认为这是一个缺失的特权,并强制它在admin中运行 我仍然收到一个0x522错误,这意味着,根据MSDN,我没有安装蜂箱的权利 我搜索了网络,发现了不同的解释和可能性

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

我想知道是否有一种合适的方法来挂载另一个用户的配置单元来写入它

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

有什么想法吗

提前感谢您的回答

干杯

---2013年6月19日编辑---

好的,谢谢你的帮助,我可以调用该函数,但未经授权装载注册表

我认为这是一个缺失的特权,并强制它在admin中运行

我仍然收到一个0x522错误,这意味着,根据MSDN,我没有安装蜂箱的权利

我搜索了网络,发现了不同的解释和可能性,但我仍然无法安装蜂箱

我对C#development和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;
        }
    }
}
您可以使用平台调用

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