/// <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); } }
/// <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); }
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); }
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); }
/// <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); } }
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); }
/// <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 + "\""); }