Ejemplo n.º 1
0
        /// <summary>
        /// Creates and returns a <see cref="RegistryKey"/> for the specified key path.
        /// </summary>
        /// <param name="keyPath">The full path of the key, including the root key.</param>
        /// <param name="creationDisposition">Whether the key has been opened or created.</param>
        /// <returns></returns>
        public static RegistryKey CreateKey(string keyPath, out RegCreationDisposition creationDisposition)
        {
            string subKeyName;
            var    registryKey = HiveHelper.GetHive(keyPath, out subKeyName).AsRegistryKey();

            creationDisposition = RegCreationDisposition.NoKeyCreated;
            if (registryKey == null || subKeyName == null)
            {
                return(registryKey);
            }
            try
            {
                var subRegistryKey = registryKey.OpenSubKey(subKeyName);
                if (subRegistryKey != null)
                {
                    creationDisposition = RegCreationDisposition.OpenedExistingKey;
                }
                else
                {
                    subRegistryKey      = registryKey.CreateSubKey(subKeyName);
                    creationDisposition = RegCreationDisposition.CreatedNewKey;
                }
                return(subRegistryKey);
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns the registry that must be used to handle te specified <see cref="RegistryRequest"/> with.
        /// </summary>
        /// <param name="request">The <see cref="RegistryRequest"/> to get the handling registry for.</param>
        /// <param name="recoverHandle">Indicates whether or not a possible unknown <see cref="RegistryRequest.Handle"/> should be recovered and virtualized.</param>
        /// <returns></returns>
        public RegistryBase GetRegistryFor(RegistryRequest request, bool recoverHandle)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            RegistryBase result = null;

            if (_virtualRegistry.IsKnownKey(request))
            {
                result = _virtualRegistry;
            }
            else if (_transparentRegistry.IsKnownKey(request))
            {
                result = _transparentRegistry;
            }
            else if (HiveHelper.IsHiveHandle(request.Handle))
            {
                request.KeyFullPath = HiveHelper.GetHive(request.Handle).AsRegistryHiveName();
                result = GetDefaultRegistryFor(request);
            }
            else if (recoverHandle)
            {
                // Unknown handle, and allowed to be recovered and virtualized.
                result = TryRecoverUnknownHandle(request);
            }
            else
            {
                EngineCore.Log.Error("Unknown registry key handle => {0}", request.Handle);
            }
            request.VirtualizationType = GetVirtualizationType(request.KeyFullPath);
            return(result);
        }
Ejemplo n.º 3
0
        public NativeResultCode SetValue(uint hKey, VirtualRegistryValue value)
        {
            if (HiveHelper.IsHiveHandle(hKey))
            {
                return(NativeResultCode.AccessDenied);
            }
            var request = new RegistryValueRequest {
                Handle = hKey, Value = value
            };
            var registry = _switch.GetRegistryFor(request);

            return(registry != null
               ? registry.SetValue(request)
               : NativeResultCode.InvalidHandle);
        }
Ejemplo n.º 4
0
        public NativeResultCode DeleteKey(uint hKey)
        {
            if (HiveHelper.IsHiveHandle(hKey))
            {
                return(NativeResultCode.AccessDenied);
            }
            var request = new RegistryRequest {
                Handle = hKey
            };
            var registry = _switch.GetRegistryFor(request);

            return(registry != null
               ? registry.DeleteKey(request)
               : NativeResultCode.InvalidHandle);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Opens a <see cref="RegistryKey"/> matching the specified <paramref name="keyPath"/>.
        /// </summary>
        /// <param name="keyPath"></param>
        /// <param name="writable"></param>
        /// <returns>The opened <see cref="RegistryKey"/>; Or null, in case the method failed</returns>
        public static RegistryKey OpenKey(string keyPath, bool writable)
        {
            var key = HiveHelper.GetHive(keyPath, out keyPath).AsRegistryKey();

            if (keyPath == null)
            {
                return(key);
            }
            try
            {
                return(key.OpenSubKey(keyPath, writable));
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 6
0
        public NativeResultCode QueryValue(uint hKey, string valueName, out VirtualRegistryValue value)
        {
            NativeResultCode result;
            var request = new RegistryValueRequest(valueName)
            {
                Handle = hKey
            };

            if (!HiveHelper.IsHiveHandle(hKey))
            {
                var registry = _switch.GetRegistryFor(request);
                result = registry != null
                   ? registry.QueryValue(request)
                   : NativeResultCode.InvalidHandle;
            }
            else
            {
                result = NativeResultCode.AccessDenied;
            }
            value = request.Value;
            return(result);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Returns the default virtualization type to use on a key.
        /// </summary>
        /// <param name="keyFullPath">The key's full path.</param>
        /// <returns>The <see cref="VirtualizationType"/>, indicating how the key should be accessed.</returns>
        private static VirtualizationType GetFallBackVirtualizationType(string keyFullPath)
        {
            EngineCore.Log.Error("Falling back to default rules, no rule specified for \"" + keyFullPath + "\"");
            var hive = HiveHelper.GetHive(keyFullPath);

            if (hive == RegistryHive.Users ||
                hive == RegistryHive.CurrentUser)
            {
                return(VirtualizationType.VirtualWithFallback);
            }
            if (hive == RegistryHive.CurrentConfig ||
                hive == RegistryHive.LocalMachine ||
                hive == RegistryHive.ClassesRoot)
            {
                return(VirtualizationType.TransparentRead);
            }
            if (hive == RegistryHive.PerformanceData ||
                hive == RegistryHive.DynData)
            {
                return(VirtualizationType.Transparent);
            }
            throw new ApplicationException("Can't determine required action for unknown subkeys of  \"" + hive + "\"");
        }