0

我在访问模拟用户的 HKEY_CURRENT_USER 注册表项时遇到问题。

经过一番研究,我目前正在使用 Win32 Api RegOpenCurrentUser。由于 P/Invoke 网站中没有文档,我已从我在此 MSDN 页面上找到的内容中检索到此签名:

[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int RegOpenCurrentUser(int samDesired, out IntPtr phkResult);

我这样使用它:

public class LocalRegistryAccess
{
    [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
    public static extern int RegOpenCurrentUser(int samDesired, out IntPtr phkResult);

    public enum RegistrySecurity
    {
        KEY_ALL_ACCESS = 0xF003F,
        KEY_CREATE_LINK = 0x0020,
        KEY_CREATE_SUB_KEY = 0x0004,
        KEY_ENUMERATE_SUB_KEYS = 0x0008,
        KEY_EXECUTE = 0x20019,
        KEY_NOTIFY = 0x0010,
        KEY_QUERY_VALUE = 0x0001,
        KEY_READ = 0x20019,
        KEY_SET_VALUE = 0x0002,
        KEY_WOW64_32KEY = 0x0200,
        KEY_WOW64_64KEY = 0x0100,
        KEY_WRITE = 0x20006,
    }

    public IntPtr GetImpersonatedUserRegistryHandle(RegistrySecurity _access)
    {
        IntPtr safeHandle = new IntPtr();
        int result = RegOpenCurrentUser((int)_access, out safeHandle);

        return safeHandle;
    }

    public RegistryKey _pointerToRegistryKey(IntPtr hKey, bool writable, bool ownsHandle)
    {
        //Get the BindingFlags for private contructors
        System.Reflection.BindingFlags privateConstructors = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;

        //Get the Type for the SafeRegistryHandle
        Type safeRegistryHandleType = typeof(Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle");

        //Get the array of types matching the args of the ctor we want
        Type[] safeRegistryHandleCtorTypes = new Type[] { typeof(IntPtr), typeof(bool) };

        //Get the constructorinfo for our object
        System.Reflection.ConstructorInfo safeRegistryHandleCtorInfo = safeRegistryHandleType.GetConstructor(privateConstructors, null, safeRegistryHandleCtorTypes, null);

        //Invoke the constructor, getting us a SafeRegistryHandle
        Object safeHandle = safeRegistryHandleCtorInfo.Invoke(new Object[] { hKey, ownsHandle });

        //Get the type of a RegistryKey
        Type registryKeyType = typeof(RegistryKey);

        //Get the array of types matching the args of the ctor we want
        Type[] registryKeyConstructorTypes = new Type[] { safeRegistryHandleType, typeof(bool) };

        //Get the constructorinfo for our object
        System.Reflection.ConstructorInfo registryKeyCtorInfo = registryKeyType.GetConstructor(privateConstructors, null, registryKeyConstructorTypes, null);

        //Invoke the constructor, getting us a RegistryKey
        RegistryKey resultKey = (RegistryKey)registryKeyCtorInfo.Invoke(new Object[] { safeHandle, writable });

        //return the resulting key
        return resultKey;
    }
}

public class UserManagement
{
    LocalRegistryAccess regAccess = new LocalRegistryAccess();

    using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(token))
    {
        IntPtr localRegistryHandle = regAccess.GetImpersonatedUserRegistryHandle(LocalRegistryAccess.RegistrySecurity.KEY_ALL_ACCESS);
        using(RegistryKey localRegistry = regAccess._pointerToRegistryKey(localRegistryHandle, true, true))
        {
            // Manage HKCU for impersonated user
        }
    }
}

此代码似乎在 Windows 7 上正常工作,但在 Windows XP Embedded 中不起作用:我的方法GetImpersonatedUserRegistryHandle总是返回一个空句柄。

任何想法?为了使此代码与 Windows XP Embedded 兼容,我是否遗漏了一些说明?

4

0 回答 0