Results 1 to 18 of 18

Thread: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Question Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    I am using Visual Studio 2013 and have almost completed conversion of my project from vb6 to vb.net

    I need the application to be able to read and write to the HKEY_CLASSES_ROOT in the Registry (for file extension associations)

    When I try to use:

    Code:
    ' This works OK
    My.Computer.Registry.CurrentUser.CreateSubKey("MyTestKey")
    
    ' This does NOT work
    My.Computer.Registry.ClassesRoot.CreateSubKey("MyTestKey")
    I get:

    "An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

    Additional information: Access to the registry key 'HKEY_CLASSES_ROOT\MyTestKey' is denied"

    The folder "HKEY_CLASSES_ROOT\MyTestKey" is NOT actually created

    Is it possible to modify permissions so that I can develop this program without running as Administrator and then enable my users to run it as non-Administrators?

    Obviously, I want users to be able to just run it (in other words I don't want them having to personally access the Registry)

    But either way, an idiot's guide to permission granting would be very useful ... many thanks
    Last edited by wavering; Nov 26th, 2014 at 05:49 AM.

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    Can you manually create a key under HKEY_CLASSES_ROOT IN regedit?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    Quote Originally Posted by NeedSomeAnswers View Post
    Can you manually create a key under HKEY_CLASSES_ROOT IN regedit?
    Yes, just done it!

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    thats strange. Your app should have the same permissions to the registry as the logged in user!

    You can check the permissions of HKEY_CLASSES_ROOT by right clicking on it in regedit and selecting permissions.

    At the top of the permissions screen you have a list of users and there permissions. You should check to see if your user OR more likely your User Group ( e.g. Users or Administrators ) has the correct permissions.

    If any permissions are missing give that group full control and they should then be able to write to that registry key.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    you need to give your app Administrator Privileges:

    Project-->Properties-->Application-->View UAC Settings-->app.manifest

    follow the instructions on that page...

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    Quote Originally Posted by NeedSomeAnswers View Post
    thats strange. Your app should have the same permissions to the registry as the logged in user!

    You can check the permissions of HKEY_CLASSES_ROOT by right clicking on it in regedit and selecting permissions.

    At the top of the permissions screen you have a list of users and there permissions. You should check to see if your user OR more likely your User Group ( e.g. Users or Administrators ) has the correct permissions.

    If any permissions are missing give that group full control and they should then be able to write to that registry key.
    I reset this to give myself full privileges but it still fails when I just run by pressing the Start button in the Visual Studio IDE ... but see below

    Quote Originally Posted by .paul. View Post
    you need to give your app Administrator Privileges:

    Project-->Properties-->Application-->View UAC Settings-->app.manifest

    follow the instructions on that page...
    I wondered what that was for! There is a full article here on the subject of the app.manifest:

    http://www.codeproject.com/Articles/...lication-Start

    the problem, it seems to me, is that if your user is NOT an administrator there is nothing you can do that will enable him to write to the Registry. As it says in this article:

    1. requireAdministrator:
    Means that the application requires administrator privileges (elevation, in other words.) If this is an administrator, he will be asked for approval. If this is a standard user, he will be asked to provide an administrator’s username and password. Therefore, the administrator executes the application of behalf of the standard user.

    2. highestAvailable:
    The application gets the privileges the user has but only after getting the consent from the user. This means that if the user is a standard user, the application gets standard user privileges. On the other hand, if the user is an administrator, the application gets the admin privileges but after the request for elevation.

    3. asInvoker:
    The application is running with the security token of the user. This means that if the user is a standard user or an administrator, the application gets the standard user privileges without elevation, and does not request it.


    This makes sense - otherwise, viruses could do just about anything (maybe they can?)

    So, to cut a long story short, it looks like I am going to have to tell my users to Right Click the exe file and Run as Administrator (but how does that work - is the PC telepathic - does it know that the user is also an Administrator on the quiet?). Unless somebody can see a way?

    PS
    One further complication. It seems that Microsoft does not favour people writing to HKEY_CLASSES_ROOT as explained in the article here:

    http://msdn.microsoft.com/en-gb/libr...=vs.85%29.aspx

    Note that it says

    To change the settings for the interactive user, store the changes under HKEY_CURRENT_USER\Software\Classes rather than HKEY_CLASSES_ROOT.

    To change the default settings, store the changes under HKEY_LOCAL_MACHINE\Software\Classes. If you write keys to a key under HKEY_CLASSES_ROOT, the system stores the information under HKEY_LOCAL_MACHINE\Software\Classes. If you write values to a key under HKEY_CLASSES_ROOT, and the key already exists under HKEY_CURRENT_USER\Software\Classes, the system will store the information there instead of under HKEY_LOCAL_MACHINE\Software\Classes.


    And, indeed, when I try this as an Adimn:
    Code:
    My.Computer.Registry.ClassesRoot.CreateSubKey("ATestKey")
    It actually creates this Key in: HKEY_LOCAL_MACHINE\Software\Classes

    If I appear confused, that is because I am ...

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    If you change it to requireAdministrator, you can write anywhere in the registry. Whether it is recommended or not is another question...

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    Quote Originally Posted by .paul. View Post
    If you change it to requireAdministrator, you can write anywhere in the registry. Whether it is recommended or not is another question...
    OK, done that. When I click on Start in the VS IDE, I get:

    "Error 1 ClickOnce does not support the request execution level 'requireAdministrator' "

    So, I cannot compile it! Never mind run it ...

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    you can compile it.
    ClickOnce is for distributing it. In the professional versions, you can create MSI projects for most installations. There are 3rd party options available...

  10. #10
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    I should have asked this earlier really but - is there a specific reason you are writing to HKEY_CLASSES_ROOT ?

    I have to say i very rarely write to it and only then when i absolutely need to.

    If you are creating your own keys to read and write to then normally you would expect to write them either HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE depending on whether you key is user specific or not.

    HKEY_CLASSES_ROOT isn't really for storing data on your app for instance.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    Quote Originally Posted by NeedSomeAnswers View Post
    I should have asked this earlier really but - is there a specific reason you are writing to HKEY_CLASSES_ROOT ?
    Yes, I am doing file associations. I have 4 extensions (each one of which is treated by my app differently) BUT as it says in the MS article above, in Win 8 it seems to write to: HKEY_LOCAL_MACHINE\Software\Classes - even when you tell it to write to HKEY_CLASSES_ROOT

    In theory, I could get the Installer to deal with them but I have developed a lot of stuff (originally in VB6 - now in vb.net) to do it precisely as I need it ... eg letting the user do it when they are happy with the app or just doing it in a limited manner

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    try googling 'process.start' and '.reg files'

  13. #13
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    In theory, I could get the Installer to deal with them but I have developed a lot of stuff (originally in VB6 - now in vb.net) to do it precisely as I need it
    hmmm, i have always just let the installer deal with file associations It's less hassle

    try googling 'process.start' and '.reg files'
    Follow Pauls advice - second link looks like what you want !
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  14. #14

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    Quote Originally Posted by NeedSomeAnswers View Post
    hmmm, i have always just let the installer deal with file associations It's less hassle
    Follow Pauls advice - second link looks like what you want !
    Ironically, I have a whole section on Reg files on my site - see:

    http://www.mopeks.org/downloads/tools.asp

    I need to test these again on my current 64 bit system but should work!

    Thanks for all your help - I am sort of getting there ...

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    The Process class has a StartInfo property, which has a Verb member that you can set to "RunAs" to run the .reg file as administrator

  16. #16

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    Quote Originally Posted by .paul. View Post
    The Process class has a StartInfo property, which has a Verb member that you can set to "RunAs" to run the .reg file as administrator
    Thanks, Paul. My fundamental problem here is that I moving from VB6 running on Win XP to vb.net running on Win 8 and this is a bit of a learning curve but you and others here have been a great help so thank you once again .. I will check out the "RunAs" on .reg files ...

  17. #17
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    My fundamental problem here is that I moving from VB6 running on Win XP to vb.net running on Win 8
    If I was a pedant (which I am) I'd say that the problem is your moving from running on XP to anything after XP.

    The problems your running into are all to do with the UAC Microsoft introduced in Vista. The reason you're struggling to get your program to update file assosciations without interaction from a user with administrator priveleges is because allowing you to do that would be dangerous. It would, as you hit on earlier, allow you to trivially disguise a virus as something else. MS has provided you with mechanisms to update the file assosciations but they're all going to require some explicit action from an adminsitrator, ie "run as administrator", provide an adminsitrator login or similar.

    I must admit this thread made me suspcious enough to check out your other posts on the forum and I'm happy to say I don't believe you're trying to do anything we'd dissaprove of. I would like to throw out a reminder of the Ts and Cs to all participants though. Although wavering's activity does seem legit, that doesn't necessarily apply to the next person who reads it. Please just be careful about any suggestions you make in this thread.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  18. #18

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Re: Write to and Read from the HKEY_CLASSES_ROOT in the Registry

    Quote Originally Posted by wavering View Post
    OK, done that. When I click on Start in the VS IDE, I get:

    "Error 1 ClickOnce does not support the request execution level 'requireAdministrator' "

    So, I cannot compile it! Never mind run it ...
    You can remove this error problem (which occurs if you change the app.manifest to "highestAvailable") as follows:

    Project --> Properties --> Security --> UNCLICK "Enable ClickOnce Security Settings"

    This means that you can develop the project without having to "Run as Administrator" but you cannot then publish it - so its a start, anyway

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width