private void DeleteSubKeyTreeInternal(string subkey) { int errorCode = 0; SafeTransactionHandle transactionHandle = this.GetTransactionHandle(); TransactedRegistryKey key = this.InternalOpenSubKey(subkey, true); if (key == null) { throw new ArgumentException(RegistryProviderStrings.Arg_RegSubKeyAbsent); } try { if (key.InternalSubKeyCount() > 0) { string[] subKeyNames = key.InternalGetSubKeyNames(); for (int i = 0; i < subKeyNames.Length; i++) { key.DeleteSubKeyTreeInternal(subKeyNames[i]); } } } finally { key.Close(); } errorCode = Microsoft.PowerShell.Commands.Internal.Win32Native.RegDeleteKeyTransacted(this.hkey, subkey, 0, 0, transactionHandle, IntPtr.Zero); if (errorCode != 0) { this.Win32Error(errorCode, null); } }
private TransactedRegistryKey InternalOpenSubKey(string name, RegistryKeyPermissionCheck permissionCheck, int rights) { ValidateKeyName(name); ValidateKeyMode(permissionCheck); ValidateKeyRights(rights); this.EnsureNotDisposed(); name = FixupName(name); this.CheckOpenSubKeyPermission(name, permissionCheck); SafeRegistryHandle hkResult = null; int num = 0; SafeTransactionHandle transactionHandle = this.GetTransactionHandle(); num = this.RegOpenKeyTransactedWrapper(this.hkey, name, 0, rights, out hkResult, transactionHandle, IntPtr.Zero); if ((num == 0) && !hkResult.IsInvalid) { return(new TransactedRegistryKey(hkResult, permissionCheck == RegistryKeyPermissionCheck.ReadWriteSubTree, false, Transaction.Current, transactionHandle) { keyName = this.keyName + @"\" + name, checkMode = permissionCheck }); } if ((num == 5) || (num == 0x542)) { throw new SecurityException(RegistryProviderStrings.Security_RegistryPermission); } return(null); }
internal static extern int RegDeleteKeyTransacted( SafeRegistryHandle hKey, string lpSubKey, int samDesired, uint reserved, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter);
internal static extern int RegOpenKeyTransacted( SafeRegistryHandle hKey, string lpSubKey, int ulOptions, int samDesired, out SafeRegistryHandle hkResult, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter);
private unsafe TransactedRegistryKey CreateSubKeyInternal(string subkey, RegistryKeyPermissionCheck permissionCheck, object registrySecurityObj) { ValidateKeyName(subkey); if (string.Empty == subkey) { throw new ArgumentException(RegistryProviderStrings.Arg_RegKeyStrEmpty); } ValidateKeyMode(permissionCheck); this.EnsureWriteable(); subkey = FixupName(subkey); TransactedRegistryKey key = this.InternalOpenSubKey(subkey, permissionCheck != RegistryKeyPermissionCheck.ReadSubTree); if (key != null) { this.CheckSubKeyWritePermission(subkey); this.CheckSubTreePermission(subkey, permissionCheck); key.checkMode = permissionCheck; return(key); } this.CheckSubKeyCreatePermission(subkey); Microsoft.PowerShell.Commands.Internal.Win32Native.SECURITY_ATTRIBUTES structure = null; TransactedRegistrySecurity security = registrySecurityObj as TransactedRegistrySecurity; if (security != null) { structure = new Microsoft.PowerShell.Commands.Internal.Win32Native.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(structure) }; byte[] securityDescriptorBinaryForm = security.GetSecurityDescriptorBinaryForm(); byte * pDest = stackalloc byte[securityDescriptorBinaryForm.Length]; Microsoft.PowerShell.Commands.Internal.Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length); structure.pSecurityDescriptor = pDest; } int lpdwDisposition = 0; SafeRegistryHandle hkResult = null; int errorCode = 0; SafeTransactionHandle transactionHandle = this.GetTransactionHandle(); errorCode = Microsoft.PowerShell.Commands.Internal.Win32Native.RegCreateKeyTransacted(this.hkey, subkey, 0, null, 0, GetRegistryKeyAccess(permissionCheck != RegistryKeyPermissionCheck.ReadSubTree), structure, out hkResult, out lpdwDisposition, transactionHandle, IntPtr.Zero); if ((errorCode == 0) && !hkResult.IsInvalid) { TransactedRegistryKey key2 = new TransactedRegistryKey(hkResult, permissionCheck != RegistryKeyPermissionCheck.ReadSubTree, false, Transaction.Current, transactionHandle); this.CheckSubTreePermission(subkey, permissionCheck); key2.checkMode = permissionCheck; if (subkey.Length == 0) { key2.keyName = this.keyName; return(key2); } key2.keyName = this.keyName + @"\" + subkey; return(key2); } if (errorCode != 0) { this.Win32Error(errorCode, this.keyName + @"\" + subkey); } return(null); }
internal static extern int RegCreateKeyTransacted( SafeRegistryHandle hKey, string lpSubKey, int Reserved, string lpClass, int dwOptions, int samDesigner, Win32Native.SECURITY_ATTRIBUTES lpSecurityAttributes, out SafeRegistryHandle hkResult, out int lpdwDisposition, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter);
private SafeTransactionHandle GetTransactionHandle() { if (null != this.myTransaction) { if (!this.myTransaction.Equals(Transaction.Current)) { throw new InvalidOperationException(RegistryProviderStrings.InvalidOperation_MustUseSameTransaction); } return(this.myTransactionHandle); } return(SafeTransactionHandle.Create()); }
internal TransactedRegistryKey InternalOpenSubKey(string name, bool writable) { ValidateKeyName(name); this.EnsureNotDisposed(); int registryKeyAccess = GetRegistryKeyAccess(writable); SafeRegistryHandle hkResult = null; SafeTransactionHandle transactionHandle = this.GetTransactionHandle(); if ((this.RegOpenKeyTransactedWrapper(this.hkey, name, 0, registryKeyAccess, out hkResult, transactionHandle, IntPtr.Zero) == 0) && !hkResult.IsInvalid) { return(new TransactedRegistryKey(hkResult, writable, false, Transaction.Current, transactionHandle) { keyName = this.keyName + @"\" + name }); } return(null); }
public void DeleteSubKey(string subkey, bool throwOnMissingSubKey) { ValidateKeyName(subkey); this.EnsureWriteable(); subkey = FixupName(subkey); this.CheckSubKeyWritePermission(subkey); TransactedRegistryKey key = this.InternalOpenSubKey(subkey, false); if (key != null) { try { if (key.InternalSubKeyCount() > 0) { throw new InvalidOperationException(RegistryProviderStrings.InvalidOperation_RegRemoveSubKey); } } finally { key.Close(); } int errorCode = 0; SafeTransactionHandle transactionHandle = this.GetTransactionHandle(); errorCode = Microsoft.PowerShell.Commands.Internal.Win32Native.RegDeleteKeyTransacted(this.hkey, subkey, 0, 0, transactionHandle, IntPtr.Zero); switch (errorCode) { case 0: return; case 2: if (throwOnMissingSubKey) { throw new ArgumentException(RegistryProviderStrings.ArgumentException_RegSubKeyAbsent); } return; } this.Win32Error(errorCode, null); } else if (throwOnMissingSubKey) { throw new ArgumentException(RegistryProviderStrings.ArgumentException_RegSubKeyAbsent); } }
public void DeleteSubKeyTree(string subkey) { ValidateKeyName(subkey); if ((string.IsNullOrEmpty(subkey) || (subkey.Length == 0)) && this.IsSystemKey()) { throw new ArgumentException(RegistryProviderStrings.ArgRegKeyDelHive); } this.EnsureWriteable(); int errorCode = 0; SafeTransactionHandle transactionHandle = this.GetTransactionHandle(); subkey = FixupName(subkey); this.CheckSubTreeWritePermission(subkey); TransactedRegistryKey key = this.InternalOpenSubKey(subkey, true); if (key == null) { throw new ArgumentException(RegistryProviderStrings.Arg_RegSubKeyAbsent); } try { if (key.InternalSubKeyCount() > 0) { string[] subKeyNames = key.InternalGetSubKeyNames(); for (int i = 0; i < subKeyNames.Length; i++) { key.DeleteSubKeyTreeInternal(subKeyNames[i]); } } } finally { key.Close(); } errorCode = Microsoft.PowerShell.Commands.Internal.Win32Native.RegDeleteKeyTransacted(this.hkey, subkey, 0, 0, transactionHandle, IntPtr.Zero); if (errorCode != 0) { this.Win32Error(errorCode, null); } }
private TransactedRegistryKey(SafeRegistryHandle hkey, bool writable, bool systemkey, Transaction transaction, SafeTransactionHandle txHandle) { this.hkey = hkey; this.keyName = ""; if (systemkey) { this.state |= 2; } if (writable) { this.state |= 4; } if (null != transaction) { this.myTransaction = transaction.Clone(); this.myTransactionHandle = txHandle; } else { this.myTransaction = null; this.myTransactionHandle = null; } }
public TransactedRegistryKey OpenSubKey(string name, bool writable) { ValidateKeyName(name); this.EnsureNotDisposed(); name = FixupName(name); this.CheckOpenSubKeyPermission(name, writable); SafeRegistryHandle hkResult = null; int num = 0; SafeTransactionHandle transactionHandle = this.GetTransactionHandle(); num = this.RegOpenKeyTransactedWrapper(this.hkey, name, 0, GetRegistryKeyAccess(writable), out hkResult, transactionHandle, IntPtr.Zero); if ((num == 0) && !hkResult.IsInvalid) { return(new TransactedRegistryKey(hkResult, writable, false, Transaction.Current, transactionHandle) { checkMode = this.GetSubKeyPermissonCheck(writable), keyName = this.keyName + @"\" + name }); } if ((num == 5) || (num == 0x542)) { throw new SecurityException(RegistryProviderStrings.Security_RegistryPermission); } return(null); }
/** * Creates a TransactedRegistryKey. * * This key is bound to hkey, if writable is <b>false</b> then no write operations * will be allowed. If systemkey is set then the hkey won't be released * when the object is GC'ed. */ private TransactedRegistryKey(SafeRegistryHandle hkey, bool writable, bool systemkey, System.Transactions.Transaction transaction, SafeTransactionHandle txHandle) { _hkey = hkey; _keyName = ""; if (systemkey) { _state |= STATE_SYSTEMKEY; } if (writable) { _state |= STATE_WRITEACCESS; } // We want to take our own clone so we can dispose it when we want and // aren't susceptible to the caller disposing it. if (null != transaction) { _myTransaction = transaction.Clone(); _myTransactionHandle = txHandle; } else { _myTransaction = null; _myTransactionHandle = null; } }
private int RegOpenKeyTransactedWrapper(SafeRegistryHandle hKey, String lpSubKey, int ulOptions, int samDesired, out SafeRegistryHandle hkResult, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter) { int error = Win32Native.ERROR_SUCCESS; SafeRegistryHandle hKeyToReturn = null; error = Win32Native.RegOpenKeyTransacted(_hkey, lpSubKey, ulOptions, samDesired, out hKeyToReturn, hTransaction, pExtendedParameter); if (Win32Native.ERROR_SUCCESS == error && !hKeyToReturn.IsInvalid) { // This is a check and workaround for TxR bug 181242. If we try to use the transacted hKey we just opened // for a call to RegQueryInfoKey and get back a ERROR_INVALID_TRANSACTION error, then the key might be a symbolic link and TxR didn't // do the open correctly. The workaround is to open it non-transacted, then open it again transacted without // a subkey string. If we get some error other than ERROR_INVALID_TRANSACTION from RegQueryInfoKey, just ignore it for now. int subkeyCount = 0; int valueCount = 0; error = Win32Native.RegQueryInfoKey(hKeyToReturn, null, null, Win32Native.NULL, ref subkeyCount, // subkeys null, null, ref valueCount, // values null, null, null, null); if (Win32Native.ERROR_INVALID_TRANSACTION == error) { SafeRegistryHandle nonTxKey = null; SafeRegistryHandle txKey = null; error = Win32Native.RegOpenKeyEx(_hkey, lpSubKey, ulOptions, samDesired, out nonTxKey); // If we got some error on this open, just ignore it and continue on with the handle // we got on the original RegOpenKeyTransacted. if (Win32Native.ERROR_SUCCESS == error) { // Now do an RegOpenKeyTransacted with the non-transacted key and no "subKey" parameter. error = Win32Native.RegOpenKeyTransacted(nonTxKey, null, ulOptions, samDesired, out txKey, hTransaction, pExtendedParameter); if (Win32Native.ERROR_SUCCESS == error) { // Let's use this hkey instead. hKeyToReturn.Dispose(); hKeyToReturn = txKey; } nonTxKey.Dispose(); nonTxKey = null; } } } hkResult = hKeyToReturn; return error; }
internal static extern int RegOpenKeyTransacted(SafeRegistryHandle hKey, string lpSubKey, int ulOptions, int samDesired, out SafeRegistryHandle hkResult, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter);
internal static extern int RegDeleteKeyTransacted(SafeRegistryHandle hKey, string lpSubKey, int samDesired, int reserved, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter);
internal static extern int RegCreateKeyTransacted(SafeRegistryHandle hKey, string lpSubKey, int Reserved, string lpClass, int dwOptions, int samDesigner, SECURITY_ATTRIBUTES lpSecurityAttributes, out SafeRegistryHandle hkResult, out int lpdwDisposition, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter);
internal static SafeTransactionHandle Create() { return(SafeTransactionHandle.Create(Transaction.Current)); }
private int RegOpenKeyTransactedWrapper(SafeRegistryHandle hKey, string lpSubKey, int ulOptions, int samDesired, out SafeRegistryHandle hkResult, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter) { int num = 0; SafeRegistryHandle handle = null; num = Microsoft.PowerShell.Commands.Internal.Win32Native.RegOpenKeyTransacted(this.hkey, lpSubKey, ulOptions, samDesired, out handle, hTransaction, pExtendedParameter); if ((num == 0) && !handle.IsInvalid) { int lpcSubKeys = 0; int lpcValues = 0; num = Microsoft.PowerShell.Commands.Internal.Win32Native.RegQueryInfoKey(handle, null, null, Microsoft.PowerShell.Commands.Internal.Win32Native.NULL, ref lpcSubKeys, null, null, ref lpcValues, null, null, null, null); if (0x1a2c == num) { SafeRegistryHandle handle2 = null; SafeRegistryHandle handle3 = null; num = Microsoft.PowerShell.Commands.Internal.Win32Native.RegOpenKeyEx(this.hkey, lpSubKey, ulOptions, samDesired, out handle2); if (num == 0) { num = Microsoft.PowerShell.Commands.Internal.Win32Native.RegOpenKeyTransacted(handle2, null, ulOptions, samDesired, out handle3, hTransaction, pExtendedParameter); if (num == 0) { handle.Dispose(); handle = handle3; } handle2.Dispose(); handle2 = null; } } } hkResult = handle; return(num); }
private int RegOpenKeyTransactedWrapper(SafeRegistryHandle hKey, string lpSubKey, int ulOptions, int samDesired, out SafeRegistryHandle hkResult, SafeTransactionHandle hTransaction, IntPtr pExtendedParameter) { int num = 0; SafeRegistryHandle handle = null; num = Microsoft.PowerShell.Commands.Internal.Win32Native.RegOpenKeyTransacted(this.hkey, lpSubKey, ulOptions, samDesired, out handle, hTransaction, pExtendedParameter); if ((num == 0) && !handle.IsInvalid) { int lpcSubKeys = 0; int lpcValues = 0; num = Microsoft.PowerShell.Commands.Internal.Win32Native.RegQueryInfoKey(handle, null, null, Microsoft.PowerShell.Commands.Internal.Win32Native.NULL, ref lpcSubKeys, null, null, ref lpcValues, null, null, null, null); if (0x1a2c == num) { SafeRegistryHandle handle2 = null; SafeRegistryHandle handle3 = null; num = Microsoft.PowerShell.Commands.Internal.Win32Native.RegOpenKeyEx(this.hkey, lpSubKey, ulOptions, samDesired, out handle2); if (num == 0) { num = Microsoft.PowerShell.Commands.Internal.Win32Native.RegOpenKeyTransacted(handle2, null, ulOptions, samDesired, out handle3, hTransaction, pExtendedParameter); if (num == 0) { handle.Dispose(); handle = handle3; } handle2.Dispose(); handle2 = null; } } } hkResult = handle; return num; }