Ejemplo n.º 1
0
        private static RegistryKey OpenSubKey(RegistryKey hkey, string subKeyName, RegWow64Options options)
        {
            //Sanity check
            if (hkey == null || RegistryKeyHandle(hkey) == IntPtr.Zero)
            {
                return(null);
            }

            //Set rights
            int rights = (int)RegistryRights.ReadKey;


            //Call the native function
            int subKeyHandle, result = RegOpenKeyEx(RegistryKeyHandle(hkey), subKeyName, 0, rights | (int)options, out subKeyHandle);

            //If we had an error, return null
            if (result != 0)
            {
                return(null);
            }

            RegistryKey subKey = PointerToRegistryKey((IntPtr)subKeyHandle, false, false);

            return(subKey);
        }
        /// <summary>
        /// Open a registry key using the Wow64 node instead of the default 32-bit node.
        /// </summary>
        /// <param name="parentKey">Parent key to the key to be opened.</param>
        /// <param name="subKeyName">Name of the key to be opened</param>
        /// <param name="writable">Whether or not this key is writable</param>
        /// <param name="options">32-bit node or 64-bit node</param>
        /// <returns></returns>
        static RegistryKey OpenSubKey( RegistryKey parentKey, string subKeyName, bool writable, RegWow64Options options )
        {
            //Sanity check
            if ( parentKey == null || GetRegistryKeyHandle( parentKey ) == IntPtr.Zero )
            {
                return null;
            }

            // Set rights
            var rights = (int)RegistryRights.ReadKey;
            if ( writable )
            {
                rights = (int) RegistryRights.WriteKey;
            }

            // Call the native function
            int subKeyHandle, result = RegOpenKeyEx( GetRegistryKeyHandle( parentKey ), subKeyName, 0, rights | (int)options, out subKeyHandle );

            // If we error return null
            if ( result != 0 )
            {
                return null;
            }

            // Get the key represented by the pointer returned by RegOpenKeyEx
            var subKey = PointerToRegistryKey( (IntPtr)subKeyHandle, writable, false, options );
            return subKey;
        }
Ejemplo n.º 3
0
        static RegistryKey OpenSubKey(RegistryKey parentKey, string subKeyName, bool writable, RegWow64Options regOptions)
        {
            int rights = (int)131097;

            if (writable)
                rights = (int)131078;

            Type keyType = typeof(RegistryKey);

            FieldInfo fieldInfo = keyType.GetField("hkey", BindingFlags.NonPublic | BindingFlags.Instance);

            IntPtr keyHandle = ((SafeHandle)fieldInfo.GetValue(parentKey)).DangerousGetHandle();

            if (parentKey == null || keyHandle == IntPtr.Zero)
                return null;

            int subKeyHandle, result = RegOpenKeyEx(keyHandle, subKeyName, 0, rights | (int)regOptions, out subKeyHandle);

            if (result != 0)
                return null;

            IntPtr hKey = (IntPtr)subKeyHandle;

            Type safeHandleType = typeof(SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle");
            Type[] safeHandleConstructorTypes = new Type[] { typeof(IntPtr), typeof(bool) };

            Type[] keyConstructorTypes = new Type[] { safeHandleType, typeof(bool) };

            ConstructorInfo safeHandleConstructorInfo = safeHandleType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, safeHandleConstructorTypes, null);

            Object[] keyAndOwns = new Object[] { hKey, false };

            Object[] invokeParameters = new Object[] { safeHandleConstructorInfo.Invoke(keyAndOwns), writable };

            ConstructorInfo keyConstructorInfo = keyType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, keyConstructorTypes, null);

            return (RegistryKey)keyConstructorInfo.Invoke(invokeParameters);
        }
Ejemplo n.º 4
0
        private static RegistryKey _openSubKey(RegistryKey parentKey, string subKeyName, bool writable, RegWow64Options options, out IntPtr subKeyHandle)
        {
            //Sanity check
            if (parentKey == null || _getRegistryKeyHandle(parentKey) == IntPtr.Zero)
            {
                subKeyHandle = IntPtr.Zero;
                return(null);
            }

            //Set rights
            int rights = (int)RegistryRights.ReadKey;

            if (writable)
            {
                rights = (int)RegistryRights.WriteKey;
            }

            //Call the native function >.<
            int x, result = RegOpenKeyEx(_getRegistryKeyHandle(parentKey), subKeyName, 0, rights | (int)options, out x);

            subKeyHandle = (IntPtr)result;

            //If we errored, throw an exception
            if (result != 0)
            {
                throw new System.ComponentModel.Win32Exception("Exception encountered opening registry key.");
            }

            //Get the key represented by the pointer returned by RegOpenKeyEx
            RegistryKey subKey = _pointerToRegistryKey(subKeyHandle, writable, false);

            return(subKey);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Open a registry key using the Wow64 node instead of the default 32-bit node.
        /// </summary>
        /// <param name="parentKey">Parent key to the key to be opened.</param>
        /// <param name="subKeyName">Name of the key to be opened</param>
        /// <param name="writable">Whether or not this key is writable</param>
        /// <param name="options">32-bit node or 64-bit node</param>
        /// <returns></returns>
        static RegistryKey OpenSubKey(RegistryKey parentKey, string subKeyName, bool writable, RegWow64Options options)
        {
            //Sanity check
            if (parentKey == null || GetRegistryKeyHandle(parentKey) == IntPtr.Zero)
            {
                return(null);
            }

            // Set rights
            var rights = (int)RegistryRights.ReadKey;

            if (writable)
            {
                rights = (int)RegistryRights.WriteKey;
            }

            // Call the native function
            int subKeyHandle, result = RegOpenKeyEx(GetRegistryKeyHandle(parentKey), subKeyName, 0, rights | (int)options, out subKeyHandle);

            // If we error return null
            if (result != 0)
            {
                return(null);
            }

            // Get the key represented by the pointer returned by RegOpenKeyEx
            var subKey = PointerToRegistryKey((IntPtr)subKeyHandle, writable, false, options);

            return(subKey);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Get a registry key from a pointer.
        /// </summary>
        /// <param name="hKey">Pointer to the registry key</param>
        /// <param name="writable">Whether or not the key is writable.</param>
        /// <param name="ownsHandle">Whether or not we own the handle.</param>
        /// <returns>Registry key pointed to by the given pointer.</returns>
        static RegistryKey PointerToRegistryKey(IntPtr hKey, bool writable, bool ownsHandle, RegWow64Options options)
        {
            // Get the BindingFlags for private and public constructors
            const BindingFlags constructorFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;

            // Get the Type for the SafeRegistryHandle
            var 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
            var safeRegistryHandleCtorTypes = new Type[] { typeof(IntPtr), typeof(bool) };

            //G et the constructor info for our object
            var safeRegistryHandleCtorInfo = safeRegistryHandleType.GetConstructor(constructorFlags, null, safeRegistryHandleCtorTypes, null);
            // Invoke the constructor, getting us a SafeRegistryHandle
            var safeHandle = safeRegistryHandleCtorInfo.Invoke(new Object[] { hKey, ownsHandle });

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

            // Get the array of types matching the args of the ctor we want.
            // .NET 4: The RegistryKey constructor (private) has changed in .NET4, so we need to adjust for this.
            //         As it uses a type not in lower versions of .NET, we can't explicitly reference it as we
            //         need to compile in lower versions.
            var registryKeyConstructorTypes = Environment.Version.Major == 4
                ? new Type[] { safeRegistryHandleType, typeof(bool), Type.GetType("Microsoft.Win32.RegistryView") }     // .NET 4 constructor version
                : new Type[] { safeRegistryHandleType, typeof(bool) };                                                  // .NET 3.5 and below

            // Get the constructor info for our object
            var registryKeyCtorInfo = registryKeyType.GetConstructor(constructorFlags, null, registryKeyConstructorTypes, null);

            // Invoke the constructor, getting us a RegistryKey
            if (Environment.Version.Major == 4)
            {
                // .NET 4: Need to create the right enum type without explicit reference to it so that we
                //         can compile in .NET 3.5.
                var rvType  = Type.GetType("Microsoft.Win32.RegistryView");
                var enumVal = Enum.Parse(rvType, "Default", true);
                switch (options)
                {
                case RegWow64Options.KEY_WOW64_32KEY:
                    enumVal = Enum.Parse(rvType, "Registry32", true);
                    break;

                case RegWow64Options.KEY_WOW64_64KEY:
                    enumVal = Enum.Parse(rvType, "Registry64", true);
                    break;
                }

                return((RegistryKey)registryKeyCtorInfo.Invoke(new Object[] { safeHandle, writable, enumVal }));
            }
            return((RegistryKey)registryKeyCtorInfo.Invoke(new Object[] { safeHandle, writable }));
        }
        /// <summary>
        /// Get a registry key from a pointer.
        /// </summary>
        /// <param name="hKey">Pointer to the registry key</param>
        /// <param name="writable">Whether or not the key is writable.</param>
        /// <param name="ownsHandle">Whether or not we own the handle.</param>
        /// <returns>Registry key pointed to by the given pointer.</returns>
        static RegistryKey PointerToRegistryKey( IntPtr hKey, bool writable, bool ownsHandle, RegWow64Options options )
        {
            // Get the BindingFlags for private and public constructors
            const BindingFlags constructorFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;

            // Get the Type for the SafeRegistryHandle
            var 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
            var safeRegistryHandleCtorTypes = new Type[] { typeof( IntPtr ), typeof( bool ) };

            //G et the constructor info for our object
            var safeRegistryHandleCtorInfo = safeRegistryHandleType.GetConstructor( constructorFlags, null, safeRegistryHandleCtorTypes, null );
            // Invoke the constructor, getting us a SafeRegistryHandle
            var safeHandle = safeRegistryHandleCtorInfo.Invoke( new Object[] { hKey, ownsHandle } );

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

            // Get the array of types matching the args of the ctor we want.
            // .NET 4: The RegistryKey constructor (private) has changed in .NET4, so we need to adjust for this.
            //         As it uses a type not in lower versions of .NET, we can't explicitly reference it as we
            //         need to compile in lower versions.
            var registryKeyConstructorTypes = Environment.Version.Major == 4
                ? new Type[] { safeRegistryHandleType, typeof( bool ), Type.GetType( "Microsoft.Win32.RegistryView" ) } // .NET 4 constructor version
                : new Type[] { safeRegistryHandleType, typeof( bool ) };                                                // .NET 3.5 and below

            // Get the constructor info for our object
            var registryKeyCtorInfo = registryKeyType.GetConstructor( constructorFlags, null, registryKeyConstructorTypes, null );

            // Invoke the constructor, getting us a RegistryKey
            if ( Environment.Version.Major == 4 )
            {
                // .NET 4: Need to create the right enum type without explicit reference to it so that we
                //         can compile in .NET 3.5.
                var rvType = Type.GetType( "Microsoft.Win32.RegistryView" );
                var enumVal = Enum.Parse( rvType, "Default", true );
                switch ( options )
                {
                    case RegWow64Options.KEY_WOW64_32KEY:
                        enumVal = Enum.Parse( rvType, "Registry32", true );
                        break;

                    case RegWow64Options.KEY_WOW64_64KEY:
                        enumVal = Enum.Parse( rvType, "Registry64", true );
                        break;
                }

                return (RegistryKey)registryKeyCtorInfo.Invoke( new Object[] { safeHandle, writable, enumVal } );
            }
            return (RegistryKey)registryKeyCtorInfo.Invoke( new Object[] { safeHandle, writable } );
        }
Ejemplo n.º 8
0
        static RegistryKey OpenSubKey(RegistryKey parentKey, string subKeyName, bool writable, RegWow64Options regOptions)
        {
            int rights = (int)131097;

            if (writable)
            {
                rights = (int)131078;
            }

            Type keyType = typeof(RegistryKey);

            FieldInfo fieldInfo = keyType.GetField("hkey", BindingFlags.NonPublic | BindingFlags.Instance);

            IntPtr keyHandle = ((SafeHandle)fieldInfo.GetValue(parentKey)).DangerousGetHandle();

            if (parentKey == null || keyHandle == IntPtr.Zero)
            {
                return(null);
            }

            int subKeyHandle, result = RegOpenKeyEx(keyHandle, subKeyName, 0, rights | (int)regOptions, out subKeyHandle);

            if (result != 0)
            {
                return(null);
            }

            IntPtr hKey = (IntPtr)subKeyHandle;

            Type safeHandleType = typeof(SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle");

            Type[] safeHandleConstructorTypes = new Type[] { typeof(IntPtr), typeof(bool) };

            Type[] keyConstructorTypes = new Type[] { safeHandleType, typeof(bool) };

            ConstructorInfo safeHandleConstructorInfo = safeHandleType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, safeHandleConstructorTypes, null);

            Object[] keyAndOwns = new Object[] { hKey, false };

            Object[] invokeParameters = new Object[] { safeHandleConstructorInfo.Invoke(keyAndOwns), writable };

            ConstructorInfo keyConstructorInfo = keyType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, keyConstructorTypes, null);

            return((RegistryKey)keyConstructorInfo.Invoke(invokeParameters));
        }
Ejemplo n.º 9
0
        private static RegistryKey _openSubKey(RegistryKey parentKey, string subKeyName, bool writable, RegWow64Options options, out IntPtr subKeyHandle)
        {
            //Sanity check
            if (parentKey == null || _getRegistryKeyHandle(parentKey) == IntPtr.Zero)
            {
                subKeyHandle = IntPtr.Zero;
                return null;
            }

            //Set rights
            int rights = (int)RegistryRights.ReadKey;
            if (writable)
                rights = (int)RegistryRights.WriteKey;

            //Call the native function >.<
            int x, result = RegOpenKeyEx(_getRegistryKeyHandle(parentKey), subKeyName, 0, rights | (int)options, out x);
            subKeyHandle = (IntPtr)result;

            //If we errored, throw an exception
            if (result != 0)
            {
                throw new System.ComponentModel.Win32Exception("Exception encountered opening registry key.");
            }

            //Get the key represented by the pointer returned by RegOpenKeyEx
            RegistryKey subKey = _pointerToRegistryKey(subKeyHandle, writable, false);
            return subKey;
        }