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

我想知道是否有适当的方法来挂载另一个用户的配置单元以在其中写入。

目前,我正在使用“REG LOAD”来安装每个配置单元。它的功能,但困惑。

任何的想法 ?

预先感谢您的回答。

干杯。

-编辑19.06.2013-

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

我坚称这是一项缺失的特权,并强制其在admin中运行。

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

我在网上搜索后发现了不同的解释和可能性,但仍然无法安装该配置单元。

我在C#开发和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;
        }
    }
}

最佳答案

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

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

关于c# - 使用C#.net挂载其他用户配置单元,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17148552/

10-12 15:29