views:

2668

answers:

5

I want my program to be able to edit a values within a registry key that resides in 'HKEY_LOCAL_MACHINE'

    My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\XYZ", "MyValue", "MyData")

The above works fine in Windows XP, but throws an UnauthorizedAccessException in Vista.

+8  A: 

You are running into Vista's UAC feature. It will not let you write to arbitrary places in the HKLM hive because you are not running with Administrative priviledges.

There are two ways to work around this issue

  1. Run the program with Administrative priviledges (different than running a program as an account which has Administrative priviledges)
  2. Choose another place, perhaps HKCU, to store the data

The second option is much better as it allows your application to run with non-Admin priviledges which you can't always assume your user has.

Here is a fairly detailed article on UAC. It's not 100% programming material but it gives a good explanation as to what exactly it is and you can hopefully relate that to your particular program: http://technet.microsoft.com/en-us/library/cc709691.aspx

JaredPar
Since it ran fine on XP, he was running with Admin privs. Users , by default, can't write to HKLM. I think what you may mean is that the process is running with the Admin token stripped off. So the process is running with User Permissions.
Rob Haupt
@Rob, editted to try and make the wording a bit clearer
JaredPar
To complete this answer, about "running the program as an administrator", see for example http://www.sevenforums.com/tutorials/11841-run-administrator.html
Daniel Daranas
A: 

Vista has tighter restrictions around Adminstrator accounts. If you're not logged in as an Administrator account, you'll have to write to HKEY_CURRENT_USER as opposed to HKEY_LOCAL_MACHINE.

IMO, this makes more sense. Each user has their own settings/etc for their programs. If you want to make global settings for your program, you'll have to do it with an Administrator account.

belgariontheking
More like Vista has tighter restrictions on Administrator accounts and makes you specifically run an app as admin to have the admin access rights.
crashmstr
@crashmstr: yeah.
belgariontheking
+2  A: 

Since changes to the Local Machine hive can have affect across the system you will find that changes to it are restricted to non-administrative users. If you try to run your same code on an XP machine a non-admin account you will get the same error. On Vista since the process is non-admin by default you are getting this error. Information on this is in the "Made for Windows 2000", "Made for Windows XP", and "Made for Windows Vista" certification guidelines.

Given that I've got little information on what your program is doing more information may be needed to give you specific guidance, so I will speak in the general case. You want your application to leave the local machine hive alone unless you need it. When your program is run in non-admin mode it you can either disable the functionality that requires access to these admin keys or you can request that the admin privs.

Joel
A: 

is there a way to get my .exe file to execute with admin rights instead of me killing the LUA? When i altar the reg is promots the user which i dont want, i am only adding the file to kill the LUA to give the user full rights to execute my .exe file.

if i can just get it to exeute as admin is would save me editing the reg, i cant find my source for the .exe coded in delphi thats the problem to add anything and the .reg file needs to be ran with the .exe files on vista

any ideas

A: 

why do you think some .exe files run without admin rights and some done, trust mine to need admin rights, its crap having to tell the user to right click and run as admin,

how do you think i should set the batch file out? thanks for the help also from everyone.