/// <summary> /// Creates a new key for the specified <paramref name="keyFullPath"/>. /// </summary> /// <param name="keyFullPath"></param> /// <returns></returns> protected VirtualRegistryKey ConstructRegistryKey(string keyFullPath) { uint keyIndex = _indexGenerator.Next(this); VirtualRegistryKey registryKey = new VirtualRegistryKey(keyIndex, keyFullPath); return(registryKey); }
/// <summary> /// Loads all values to the provided <see cref="VirtualRegistryKey"/>. /// </summary> /// <param name="registryKey">The <see cref="VirtualRegistryKey"/> to load the values for.</param> /// <param name="overwriteIfExists"> /// Set to true if the values in <paramref name="registryKey"/> must overwrite existing values, if any. /// </param> /// <param name="handleOwnLock"> /// Set to true if this method should handle its own read lock on <see cref="_keysSynchronizationLock"/>. /// </param> /// <returns>The <see cref="VirtualRegistryKey"/> with all its values loaded.</returns> private VirtualRegistryKey LoadAllValues(VirtualRegistryKey registryKey, bool overwriteIfExists, bool handleOwnLock) { if (handleOwnLock) { _keysSynchronizationLock.EnterReadLock(); } try { if (!_keys.ContainsKey(registryKey.Handle)) { return(registryKey); } VirtualRegistryKey loadedKey = _keys[registryKey.Handle]; foreach (var valuePair in registryKey.Values) { if (!loadedKey.Values.ContainsKey(valuePair.Key)) { loadedKey.Values.Add(valuePair); } else if (overwriteIfExists) { loadedKey.Values[valuePair.Key] = valuePair.Value; } } return(loadedKey); } finally { if (handleOwnLock) { _keysSynchronizationLock.ExitReadLock(); } } }
/// <summary> /// Writes the provided <see cref="VirtualRegistryKey"/> to <see cref="_keys"/>. /// </summary> /// <exception cref="ThreadStateException"> /// A <see cref="ThreadStateException"/> is thrown if the current thread /// can't acquire a read or write lock on <see cref="_keysSynchronizationLock"/>. /// </exception> /// <param name="registryKey"><see cref="VirtualRegistryKey"/> to write to the database.</param> /// <param name="discardOldKeyValues"> /// Set to true if all existing values of the key must be discarded and only those specified in <paramref name="registryKey"/> must be saved; /// Otherwise, if all existing values must be preserved or overwritten in case <paramref name="registryKey"/> contains the same value, set to false. /// </param> protected void WriteKey(VirtualRegistryKey registryKey, bool discardOldKeyValues) { if (!discardOldKeyValues) { registryKey = LoadAllValues(registryKey, false, true); } WriteKey(registryKey); }
public RegDbTests() { _entryValue = new VirtualRegistryValue("myValue", new ASCIIEncoding().GetBytes("someData"), ValueType.REG_SZ); _entryKey = new VirtualRegistryKey(456, @"HKEY_USERS\MyTestUser\TestEntry", new Dictionary <string, VirtualRegistryValue> { { _entryValue.Name, _entryValue } }); }
public void UpdateItemsKey() { var db = RegistryDatabase.CreateDefaultDatabase(DbConstants.DatabaseFile); db.Initialize(); _entryKey = new VirtualRegistryKey(_entryKey.Handle, @"HKEY_USERS\MyTestUser\UpdatedTestEntry", _entryKey.Values); db.EnqueueAction(new DatabaseAction <VirtualRegistryKey>(_entryKey, DatabaseActionType.Set)); Thread.Sleep(500); // Give the database some time to write var items = db.ReadAll(); var rEntry = items.First(); Assert.IsTrue(rEntry.Path == _entryKey.Path, "Inputted path is \"" + _entryKey.Path + "\" while outputted path is \"" + rEntry.Path + "\""); }
public override NativeResultCode QueryValue(RegistryValueRequest request) { var resultCode = base.QueryValue(request); if (resultCode != NativeResultCode.FileNotFound) { return(resultCode); // Base knows the value } if (!IsKnownKey(request)) { return(NativeResultCode.InvalidHandle); // Base does not know the handle } if (request.VirtualizationType == VirtualizationType.Virtual) { return(NativeResultCode.FileNotFound); // Not allowed to retrieve value from host registry } // Query the value from the real registry. try { ValueType valueType; var realKeyPath = RegistryTranslator.ToRealPath(request.KeyFullPath); var data = HostRegistry.QueryValue(realKeyPath, request.Value.Name, out valueType); if (data == null) { return(NativeResultCode.FileNotFound); } request.Value = new VirtualRegistryValue(request.Value.Name, data.ToByteArray(), valueType); } catch { return(NativeResultCode.AccessDenied); } // Determine whether the newly acquired value needs to be written to the base. if (request.VirtualizationType < VirtualizationType.TransparentRead) { var key = new VirtualRegistryKey(request.Handle, request.KeyFullPath); key.Values.Add(request.Value.Name, request.Value); WriteKey(key, false); } return(NativeResultCode.Success); }
/// <summary> /// Writes or overwrites the provided <see cref="VirtualRegistryKey"/> to <see cref="_keys"/>. /// This method needs to be able to acquire a write lock on <see cref="_keysSynchronizationLock"/>. /// </summary> /// <exception cref="ThreadStateException"> /// A <see cref="ThreadStateException"/> is thrown if the current thread /// can't acquire a writelock on <see cref="_keysSynchronizationLock"/>. /// </exception> /// <param name="registryKey"><see cref="VirtualRegistryKey"/> to write to the database.</param> private void WriteKey(VirtualRegistryKey registryKey) { if (!_keysSynchronizationLock.TryEnterWriteLock(2500)) { throw new ThreadStateException( string.Format("Thread {0} can't get a write-lock to write the new key with path {1}.", Thread.CurrentThread.Name, registryKey.Path)); } try { if (_keys.ContainsKey(registryKey.Handle)) { _keys[registryKey.Handle] = registryKey; } else { _keys.Add(registryKey.Handle, registryKey); } } finally { _keysSynchronizationLock.ExitWriteLock(); } }
public override NativeResultCode QueryValue(RegistryValueRequest request) { var resultCode = base.QueryValue(request); if (resultCode != NativeResultCode.FileNotFound) return resultCode; // Base knows the value if (!IsKnownKey(request)) return NativeResultCode.InvalidHandle; // Base does not know the handle if (request.VirtualizationType == VirtualizationType.Virtual) return NativeResultCode.FileNotFound; // Not allowed to retrieve value from host registry // Query the value from the real registry. try { ValueType valueType; var realKeyPath = RegistryTranslator.ToRealPath(request.KeyFullPath); var data = HostRegistry.QueryValue(realKeyPath, request.Value.Name, out valueType); if (data == null) return NativeResultCode.FileNotFound; request.Value = new VirtualRegistryValue(request.Value.Name, data.ToByteArray(), valueType); } catch { return NativeResultCode.AccessDenied; } // Determine whether the newly acquired value needs to be written to the base. if (request.VirtualizationType < VirtualizationType.TransparentRead) { var key = new VirtualRegistryKey(request.Handle, request.KeyFullPath); key.Values.Add(request.Value.Name, request.Value); WriteKey(key, false); } return NativeResultCode.Success; }
/// <summary> /// Writes or overwrites the provided <see cref="VirtualRegistryKey"/> to <see cref="_keys"/>. /// This method needs to be able to acquire a write lock on <see cref="_keysSynchronizationLock"/>. /// </summary> /// <exception cref="ThreadStateException"> /// A <see cref="ThreadStateException"/> is thrown if the current thread /// can't acquire a writelock on <see cref="_keysSynchronizationLock"/>. /// </exception> /// <param name="registryKey"><see cref="VirtualRegistryKey"/> to write to the database.</param> private void WriteKey(VirtualRegistryKey registryKey) { if (!_keysSynchronizationLock.TryEnterWriteLock(2500)) throw new ThreadStateException( string.Format("Thread {0} can't get a write-lock to write the new key with path {1}.", Thread.CurrentThread.Name, registryKey.Path)); try { if (_keys.ContainsKey(registryKey.Handle)) _keys[registryKey.Handle] = registryKey; else _keys.Add(registryKey.Handle, registryKey); } finally { _keysSynchronizationLock.ExitWriteLock(); } }
/// <summary> /// Loads all values to the provided <see cref="VirtualRegistryKey"/>. /// </summary> /// <param name="registryKey">The <see cref="VirtualRegistryKey"/> to load the values for.</param> /// <param name="overwriteIfExists"> /// Set to true if the values in <paramref name="registryKey"/> must overwrite existing values, if any. /// </param> /// <param name="handleOwnLock"> /// Set to true if this method should handle its own read lock on <see cref="_keysSynchronizationLock"/>. /// </param> /// <returns>The <see cref="VirtualRegistryKey"/> with all its values loaded.</returns> private VirtualRegistryKey LoadAllValues(VirtualRegistryKey registryKey, bool overwriteIfExists, bool handleOwnLock) { if (handleOwnLock) _keysSynchronizationLock.EnterReadLock(); try { if (!_keys.ContainsKey(registryKey.Handle)) return registryKey; VirtualRegistryKey loadedKey = _keys[registryKey.Handle]; foreach (var valuePair in registryKey.Values) { if (!loadedKey.Values.ContainsKey(valuePair.Key)) loadedKey.Values.Add(valuePair); else if (overwriteIfExists) loadedKey.Values[valuePair.Key] = valuePair.Value; } return loadedKey; } finally { if (handleOwnLock) _keysSynchronizationLock.ExitReadLock(); } }
/// <summary> /// Writes the provided <see cref="VirtualRegistryKey"/> to <see cref="_keys"/>. /// </summary> /// <exception cref="ThreadStateException"> /// A <see cref="ThreadStateException"/> is thrown if the current thread /// can't acquire a read or write lock on <see cref="_keysSynchronizationLock"/>. /// </exception> /// <param name="registryKey"><see cref="VirtualRegistryKey"/> to write to the database.</param> /// <param name="discardOldKeyValues"> /// Set to true if all existing values of the key must be discarded and only those specified in <paramref name="registryKey"/> must be saved; /// Otherwise, if all existing values must be preserved or overwritten in case <paramref name="registryKey"/> contains the same value, set to false. /// </param> protected void WriteKey(VirtualRegistryKey registryKey, bool discardOldKeyValues) { if (!discardOldKeyValues) registryKey = LoadAllValues(registryKey, false, true); WriteKey(registryKey); }
/// <summary> /// Creates a new key for the specified <paramref name="keyFullPath"/>. /// </summary> /// <param name="keyFullPath"></param> /// <returns></returns> protected VirtualRegistryKey ConstructRegistryKey(string keyFullPath) { uint keyIndex = _indexGenerator.Next(this); VirtualRegistryKey registryKey = new VirtualRegistryKey(keyIndex, keyFullPath); return registryKey; }