Пример #1
0
        /// <summary>
        /// this method is use for the 32appliction to get the 64bit regestkey
        /// </summary>
        /// <param name="parentKeyName"></param>
        /// <param name="subKeyName"></param>
        /// <param name="keyName"></param>
        /// <returns></returns>
        public static string Get64BitRegistryKey(RegistryHive hive, string keyName, string valueName, RegistryView view)
        {
            Microsoft.Win32.SafeHandles.SafeRegistryHandle handle = new Microsoft.Win32.SafeHandles.SafeRegistryHandle(GetHiveHandle(hive), true);
            RegistryKey subkey = RegistryKey.FromHandle(handle, view).OpenSubKey(keyName);
            RegistryKey key    = RegistryKey.FromHandle(subkey.Handle, view);

            return(key.GetValue(valueName).ToString());
        }
Пример #2
0
        private static RegistryKey PointerToRegistryKey(IntPtr hKey, bool writable, bool ownsHandle)
        {
#if NET35
            //Get the constructor for the SafeRegistryHandle
            var safeRegistryHandleType        = typeof(Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle");
            var safeRegistryHandleConstructor = safeRegistryHandleType.GetConstructor(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic, null, new[] { typeof(IntPtr), typeof(bool) }, null); // .NET < 4
            if (safeRegistryHandleConstructor == null)
            {
                safeRegistryHandleConstructor = safeRegistryHandleType.GetConstructor(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, new[] { typeof(IntPtr), typeof(bool) }, null); // .NET >= 4
            }
            //Invoke the constructor, getting us a SafeRegistryHandle
            var safeHandle = safeRegistryHandleConstructor.Invoke(new object[] { hKey, ownsHandle });

            //Get the RegistryKey constructor
            var net3Constructor = typeof(RegistryKey).GetConstructor(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic, null, new[] { safeRegistryHandleType, typeof(bool) }, null);
            var net4Constructor = typeof(RegistryKey).GetConstructor(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic, null, new[] { typeof(IntPtr), typeof(bool), typeof(bool), typeof(bool), typeof(bool) }, null);

            //Invoke the constructor, getting us a RegistryKey
            object key;
            if (net3Constructor != null)
            {
                key = net3Constructor.Invoke(new object[] { safeHandle, writable });
            }
            else if (net4Constructor != null)
            {
                key = net4Constructor.Invoke(new object[] { hKey, writable, false, false, false });
            }
            else
            {
                var keyFromHandleMethod = typeof(RegistryKey).GetMethod("FromHandle", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public, null, new[] { safeRegistryHandleType }, null);
                key = keyFromHandleMethod.Invoke(null, new object[] { safeHandle });
            }
            var field = typeof(RegistryKey).GetField("keyName", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            if (field != null)
            {
                field.SetValue(key, String.Empty);
            }
            RegistryKey resultKey = (RegistryKey)key;
#else
            //Get the BindingFlags for public contructors
            System.Reflection.BindingFlags publicConstructors = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public; // NOTE: this has been changed since .NET 3.5 (was 'private').
            //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 constructor info for our object
            System.Reflection.ConstructorInfo safeRegistryHandleCtorInfo = safeRegistryHandleType.GetConstructor(
                publicConstructors, null, safeRegistryHandleCtorTypes, null);
            //Get the SafeRegistryHandle
            Microsoft.Win32.SafeHandles.SafeRegistryHandle safeHandle = (Microsoft.Win32.SafeHandles.SafeRegistryHandle)safeRegistryHandleCtorInfo.Invoke(new object[] { hKey, ownsHandle });
            //Get the RegistryKey
            RegistryKey resultKey = RegistryKey.FromHandle(safeHandle); // ONLY possible under .NET 4.0! In .NET 3.5 there is no .FromHandle Method available!
#endif
            //return the resulting key
            return(resultKey);
        }
Пример #3
0
        //============================================================
        // <T>强转注册表信息操作64系统<T>
        //
        // @param type 类型
        // @param sunKeyName 首节点
        //============================================================
        public static FRegisterPath Sync(ERegister type, string path)
        {
            // 构建对象
            FRegisterPath registerPath = new FRegisterPath();

            if (IsRunningOn64Bit)
            {
                RegistryKey localMachineKey = Registry.LocalMachine;
                RegistryKey pathKey         = localMachineKey.CreateSubKey(path);
                registerPath.RootNode = pathKey;
                return(registerPath);
            }
            // 存储路径
            registerPath.Path = path;
            int KEY_QUERY_VALUE = (0x0001);
            int KEY_WOW64_64KEY = (0x0100);
            int KEY_ALL_WOW64   = (KEY_QUERY_VALUE | KEY_WOW64_64KEY);
            // 将Windows注册表主键名转化成为不带正负号的整形句柄(与平台是32或者64位有关)
            UIntPtr hKey = TransferKeyName(type);
            // 声明将要获取Key值的句柄
            IntPtr pHKey = IntPtr.Zero;
            // 关闭文件系统转向
            IntPtr oldWOW64State = new IntPtr();

            if (Wow64DisableWow64FsRedirection(ref oldWOW64State))
            {
                // 获得操作Key值的句柄
                RegOpenKeyEx(hKey, path, 0, KEY_ALL_WOW64, out pHKey);
                Microsoft.Win32.SafeHandles.SafeRegistryHandle safeHandle = new Microsoft.Win32.SafeHandles.SafeRegistryHandle(pHKey, true);
                RegistryKey key = RegistryKey.FromHandle(safeHandle);
                registerPath.RootNode = key;
                // 关闭注册表转向(禁止特定项的注册表反射)
                RegDisableReflectionKey(pHKey);
                // 打开注册表转向(开启特定项的注册表反射)
                RegEnableReflectionKey(pHKey);
            }
            // 打开文件系统转向
            Wow64RevertWow64FsRedirection(oldWOW64State);
            return(registerPath);
        }
Пример #4
0
 public static Microsoft.Win32.RegistryKey FromHandle(Microsoft.Win32.SafeHandles.SafeRegistryHandle handle, Microsoft.Win32.RegistryView view)
 {
     return(default(Microsoft.Win32.RegistryKey));
 }
Пример #5
0
 public IRegistryKey FromHandle(Microsoft.Win32.SafeHandles.SafeRegistryHandle handle, Microsoft.Win32.RegistryView view)
 {
     return(RegistryKey.Wrap(Microsoft.Win32.RegistryKey.FromHandle(handle, view)));
 }
Пример #6
0
 public static Microsoft.Win32.RegistryKey FromHandle(Microsoft.Win32.SafeHandles.SafeRegistryHandle handle, Microsoft.Win32.RegistryView view)
 {
     throw null;
 }
        public static RegistryKey FromHandle(Microsoft.Win32.SafeHandles.SafeRegistryHandle handle)
        {
            Contract.Ensures(Contract.Result <Microsoft.Win32.RegistryKey>() != null);

            return(default(RegistryKey));
        }
Пример #8
0
 public static Microsoft.Win32.RegistryKey FromHandle(Microsoft.Win32.SafeHandles.SafeRegistryHandle handle) => throw null;