Access to the registry key '[KEY_NAME]' is denied

16,366

Solution 1

Thanks Matt, I tried running it with requireAdministrator as well but that didn't help either. Anyway, I found the solution to this and it seems the problem lied with the permissions on the registry key that I was trying to modify.

Full Control access was only given to the TrustedInstaller group, so I granted Full Control to the users in the Administrators group as well.

I started 'regedit' with SYSTEM privileges using Sysinternals' PsExec tool [psexec -si regedit] and navigated to the key I wished to manipulate using my program and used [Edit -> Permissions] to grant write access to myself.

After doing that, my code worked and this:

Dim keyString = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\" _
+ "MMDevices\Audio\Render\{91801674-82d9-459a-9358-6e5cf3d81d21}\FxProperties"

Dim regKey = "{ad75efc0-8f48-4285-bfa8-40fb036cdab2},2"

My.Computer.Registry.SetValue( _
keyString, regKey, "00000000", RegistryValueKind.DWord)

could successfully flip the value of the DWORD. Although this worked, I would like to know if there's a way to do this without having to manually change permissions on the registry subkey.

I found a similar problem and solution for this in C# given here but I couldn't successfully convert the C# code mentioned there to VB.NET code. Could you help with that?

Solution 2

You should probably try with requireAdministrator in your manifest because highestAvailable may not actually be an administrator.

I would also try specifying the data type (in your case I think it is binary):

My.Computer.Registry.SetValue(keyString, _
"{ad75efc0-8f48-4285-bfa8-40fb036cdab2},2", _ 
"00000000", _
RegistryValueKind.Binary)

However the value you are setting may need to be a byte array (something else you could try)

Solution 3

Here is the vb.net code for the c# link referenced below. You will need to set a reference to System.Security.

    Imports System.Security
    Imports System.Security.Principal
    Imports System.Security.AccessControl

    Imports Microsoft.Win32

    Private Sub TestMethod(ByVal subkey As String)
        ' Create access rule giving full control to the Administrator user.
        Dim rs As New RegistrySecurity()
        rs.AddAccessRule( New RegistryAccessRule( _
            "Administrator", _
            RegistryRights.FullControl, _
            InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, _
            PropagationFlags.InheritOnly, _
            AccessControlType.Allow))

        ' Get the registry key desired with ChangePermissions Rights.
        Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey( _
            subkey, _
            RegistryKeyPermissionCheck.ReadWriteSubTree, _
            RegistryRights.ChangePermissions Or RegistryRights.ReadKey)

        ' Apply the new access rule to this Registry Key.
        rk.SetAccessControl(rs)

        ' Get the registry key desired with ChangePermissions Rights.
        rk = Registry.LocalMachine.OpenSubKey( _
            subkey, _
            RegistryKeyPermissionCheck.ReadWriteSubTree, _
            RegistryRights.ChangePermissions Or RegistryRights.ReadKey)

        ' Apply the new access rule to this Registry Key.
        rk.SetAccessControl(rs)

        ' Open the key again with full control.
        rk = Registry.LocalMachine.OpenSubKey( _
            subkey, _
            RegistryKeyPermissionCheck.ReadWriteSubTree, _
            RegistryRights.FullControl)

        ' Set the security's owner to be Administrator.
        rs.SetOwner(New NTAccount("Administrator"))

        ' Set the key with the changed permission so Administrator is now owner.
        rk.SetAccessControl(rs)
    End Sub
Share:
16,366
Vinayak
Author by

Vinayak

Just another computer geek. Need to contact me personally for some reason? Send an email to: [email protected]

Updated on June 04, 2022

Comments

  • Vinayak
    Vinayak about 2 years

    I'm writing a small program in Visual Basic 2008 that flips the values of specific DWORDs in a registry key

    The registry key in question is:

    'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\{91801674-82d9-459a-9358-6e5cf3d81d21}\FxProperties'
    

    The dword I'm manipulating is "{e0a941a0-88a2-4df5-8d6b-dd20bb06e8fb},4"

    This is the line of code I wrote to set the DWORD's value is this:

    Dim keyString = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\{91801674-82d9-459a-9358-6e5cf3d81d21}\FxProperties"
    My.Computer.Registry.SetValue(keyString, "{ad75efc0-8f48-4285-bfa8-40fb036cdab2},2", "00000000")
    

    But I get a UnauthorizedAccessException at runtime stating that "Access to the registry key [KEY_NAME] is denied."

    I ran the program with Administrator privileges, changed the app's manifest to include:

    <requestedExecutionLevel level="highestAvailable" uiAccess="false" />
    

    But that didn't work either. So I searched a few forums and tried this:

    Dim rkLM As RegistryKey = Registry.LocalMachine
    Dim pRegKey As RegistryKey = rkLM.OpenSubKey("\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\{91801674-82d9-459a-9358-6e5cf3d81d21}\FxProperties", True)
    pRegKey.SetValue("{ad75efc0-8f48-4285-bfa8-40fb036cdab2},2", "00000000")
    

    But that threw a NullReferenceException at me stating "Object reference not set to an instance of an object."

    Is there any way I can modify that that key without having to run my program with SYSTEM privileges?

  • Vinayak
    Vinayak over 11 years
    Thanks Matt. I tried requireAdministrator but that didn't work. I found a solution though. I'll post it after a few hours after the self-answer time limit is removed.
  • natenho
    natenho almost 10 years
    You're probably looking for the same thing described in my question here. It that the best way you've found to access audio enhacements?
  • Vinayak
    Vinayak almost 10 years
    @natenho I was able to change the values in the registry but that didn't do anything except check/uncheck the related check boxes in the Speaker Properties window.
  • natenho
    natenho almost 10 years
    I had to restart the audio service to make it work net stop audiosrv then net start audiosrv or use the ServiceController class.
  • Vinayak
    Vinayak almost 10 years
    Thanks. I'll try it out and see if it works. By the way, regarding your question, I think I figured out which keys to change in the registry by starting Process Monitor and then enabling/disabling an enhancement effect and looking at the results in Process Monitor to see what changed.
  • natenho
    natenho almost 10 years
    Yes, I've found using the same method and got stuck at the same problem (writing the values in registry). Your answer helped me a lot! Although I'm still unable to find where I can get effects names and manipulate their values. There is nothing over the web. I just got a clue that is something related to sAPO, but after fiddling over it, i couldn't do more than enumerate that MMDevices.