CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Creating subkeys under HKCU\Software & Permissions

  1. #1
    Join Date
    Aug 2007
    Location
    Farnborough, Hants, UK
    Posts
    45

    Red face Creating subkeys under HKCU\Software & Permissions

    (Visual Studio 2005, .NET 2.0)

    I can read keys no problem, but if I try and create a subkey under Software, I get an UnauthorizedAccessException ("Cannot write to registry key"). I've tried requesting the permission in the assembly, and I've also tried dynamically requesting Create and Write access using a RegistryPermission object (which did not throw an exception, apparently saying I got permission)... yet it still throws the exception. I'm running the project from a local drive, so no network access issues here.

    I tried this in my assembly:

    [assembly: RegistryPermission(SecurityAction.RequestMinimum, Unrestricted = true)]

    and this with the RegistryPermission object:

    RegistryPermission permission = new RegistryPermission(PermissionState.None);
    permission.SetPathList(RegistryPermissionAccess.Create, "HKEY_CURRENT_USER\\Software");
    permission.SetPathList(RegistryPermissionAccess.Read, "HKEY_CURRENT_USER\\Software");
    permission.SetPathList(RegistryPermissionAccess.Write, "HKEY_CURRENT_USER\\Software");
    permission.Demand();


    Does anyone know what I'm doing wrong?
    Last edited by JTeagle; April 20th, 2008 at 04:25 AM.

  2. #2
    Join Date
    Sep 2004
    Posts
    1,361

    Re: Creating subkeys under HKCU\Software & Permissions

    What credentials is your software running under? If it is local system, such as a service, it can't create a key under HKCU (CU = Current User) because there is no current user for Local System.

    If you are working under vista, and perhaps XP there can be two things.

    If the OS is 64 bits and the app is 32 bits, you might be trying to write to a 64 bit area and are not allowed to. The "normal" registry path on a 64 bit system is the 64 bit data. The normal response from a 32 bit app is to be re-directed to the 32 bit area, however, you can open the registry in such a mode where this translation does not happen.

    If its vista, in addition to the above, your program may not have admin privs, although I do not think you need them for HKCU. In the case that you do need admin privs, you must run your program as administrator. That is the explicit RUN AS, not just double clicking the icon. It will ask for an elevation request if you do.

  3. #3
    Join Date
    Aug 2007
    Location
    Farnborough, Hants, UK
    Posts
    45

    Re: Creating subkeys under HKCU\Software & Permissions

    Thanks for the response. This is actually running as a normal application, under XP, 32-bit app on 32-bit XP.

    Further playing around shows that it seems to be a .NET feature related to security. The Application object has a couple of members such as UserAppDataRegistry, I found, and I can write to these with no problems whatsoever. It seems that .NET apps protect the registry (a little over zealously in my opinion, but that's another conversation).

    Since I have issues with the keys provided, I was hoping it would be possible to convince the system that I should be able to at least write anywhere under my own app's key, but this does not work. I do find it highly annoying that the facility to request permissions exists, but makes no difference whatsoever. I will have to resort to writing settings to a local config file.

    Thanks anyway.

  4. #4
    Join Date
    Sep 2004
    Posts
    1,361

    Re: Creating subkeys under HKCU\Software & Permissions

    Hmmm. Does the key exist before you try and write to it? There is a difference between writing a key and creating a key. There are also some things you need to do in a certain order as well. Let me give you a few examples.

    Lets say the current registry looks like this: HKCU/Software/MyApp

    Now you want to add a value here, so you need to CREATE a value, MyIntVal at the above path.

    If you want to change MyIntVal, now you WRITE MyIntVal because it already exists.

    Now you want to organize your data and you want to create MyStringValue at HKCU/Software/MyApp/Animals/Cats.

    If you try and create that, it will fail. You need to Create Animals first, then Create Cats, then Create the value MyStringValue.

    If at some point you want to remove Cats and the associated values, you must nuke the values first before you can delete Cats.

    I am basing this off of the CPP win32 registry stuff. I do not know if the .NET registry stuff is nicer and will let you do complex stuff in one step.

  5. #5
    Join Date
    Aug 2007
    Location
    Farnborough, Hants, UK
    Posts
    45

    Re: Creating subkeys under HKCU\Software & Permissions

    I understand what you are saying, and no, they did not pre-exist - it was attempting to create them that failed.

    It seems that with .NET, if you use the key name they provide, you can happily set values and it will create the hierarchy for you - no problem. But if I try creating a subkey (one level only) or a value, both fail.

    What I tried was to use their key to get the hierarchy created, and then I tried to open the parent key (my app's key), and create values there (note 'create' rather than 'write'). But that failed. I can only conclude that they have locked it to specifically their key - presumably it is using my assembly (where the version and app name come from) to detect what it will and won't allow.

    I could have understood if I simply had to request extra permissions to get access to that slightly different path, but as you can see in the original post, I tried that and although it never threw any exception when I requested the permissions, it still failed when I tried to create values.

    Bummer!

  6. #6
    Join Date
    Sep 2014
    Posts
    1

    Re: Creating subkeys under HKCU\Software & Permissions

    I have same problem under Visual Studio Express 2013 Desktop .


    A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

    Additional information: O acesso chave do Registro 'HKEY_LOCAL_MACHINE\SYSTEM' foi negado.

    If there is a handler for this exception, the program may be safely continued.

    MY CODE
    If CheckBox1.Checked Then
    My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM",
    "1", &H1I, Microsoft.Win32.RegistryValueKind.DWord)
    End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)