protected override void DisposeUnmanaged() { if (hive_ != null) { if (!readOnly_) { // Trying to save in any case try { Save(); } catch { } } cacheForDeletedMarks_.Dispose(); cacheForDeletedMarks_ = null; // Closing hive_.Close(); hive_ = null; fileLock_.Dispose(); fileLock_ = null; } }
// Create key and if needed all parent keys private OffregLib.OffregKey CreateKeys(OffregLib.OffregHive hiveImpl, string path, string lpClass, Win32Api.RegOption dwOptions, IntPtr lpSecurityDescriptor, IntPtr lpdwDisposition) { OffregLib.OffregKey result = null; try { OffRegHive.ConvertException(() => result = hiveImpl.Root.OpenSubKey(path)); return(result); } catch (FileNotFoundException) { } // We either take the first subkey of the path, or the last string parentPath, subkey; if (KeyIdentity.PartitionPath(path, out parentPath, out subkey)) { OffregLib.OffregKey parentKey = CreateKeys(hiveImpl, parentPath, lpClass, dwOptions, lpSecurityDescriptor, lpdwDisposition); OffRegHive.ConvertException(() => parentKey.Close()); } // TODO: do something with lpClass, lpSecurityDescriptor, for now they are ignored OffRegHive.ConvertException(() => result = hiveImpl.Root.CreateSubKey( path, (OffregLib.RegOption)dwOptions)); if (lpdwDisposition != IntPtr.Zero) { Marshal.WriteInt32(lpdwDisposition, (int)Win32Api.RegKeyDisposition.REG_CREATED_NEW_KEY); } // TODO: if there is an existing key in windows registry, but we create one // in offreg hive, which disposition do we need to return? return(result); }
internal OffRegKey(OffRegHive hive, OffregLib.OffregHive hiveImpl, KeyIdentity identity, string lpClass, Win32Api.RegOption dwOptions, IntPtr lpSecurityDescriptor, IntPtr lpdwDisposition) { hive_ = hive; identity_ = identity; key_ = CreateKeys(hiveImpl, GetMainOffRegPath(identity), lpClass, dwOptions, lpSecurityDescriptor, lpdwDisposition); }
private void OpenOrCreateHive(string offRegHivePath, bool createIfNotExists) { try { ConvertException(() => hive_ = OffregLib.OffregHive.Open(offRegHivePath)); } catch (FileNotFoundException) { if (!createIfNotExists) { throw; } ConvertException(() => hive_ = OffregLib.OffregHive.Create()); } }
public static bool TryOpen(OffRegHive hive, OffregLib.OffregHive hiveImpl, KeyIdentity identity, out OffRegKey openedKey) { // TODO: possibly we need to simulate security restrictions in some way so that, // for instance, write operations from app w/o administrative permissions // to HKEY_LOCAL_MACHINE hive would fail List <string> paths = GetOffRegPaths(identity); foreach (string path in paths) { OffregLib.Win32Result result; OffregLib.OffregKey key; if (hiveImpl.Root.TryOpenSubKey(path, out key, out result)) { openedKey = new OffRegKey(hive, key, identity); return(true); } if ((int)result != (int)Win32Api.Error.ERROR_FILE_NOT_FOUND) { throw Win32Exception.Create((int)result); } } openedKey = null; return(false); }