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);
        }
Example #2
0
 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;
 }