Link to home
Start Free TrialLog in
Avatar of Dominic34
Dominic34Flag for Canada

asked on

write registry on Windows 7 with VB.NET

Hi,
  I have a software in VB 2008 that needs to read/write the registry at start-up. On Windows XP, it works fine. I have migrated to Windows 7 last weekend, and now, it doesn't work anymore, I have an exception thrown at the Write registry line with the error: "Requested registry access is not allowed".

How can I fix this?

I can't modify the source code, because many other programmers uses the same code.

thanks a lot for your time and help
Avatar of McKnife
McKnife
Flag of Germany image

Your software requires administrative rights - what a shame :)
In xp, using a member of the local admin group was enough. In vista/2008/Win7, you need to elevate the program as well. Please inform yourself about elevation and UAC at wikipedia: http://en.wikipedia.org/wiki/User_Account_Control
 Hi there Dominic34,,,

I can only suggest  if you can make the software  " run as Administrator" then see .

Also I found a similar case on internet which you can refer to it :
http://www.texttoolkit.com/index.php?option=com_content&view=article&id=74:visual-studio-requested-registry-access-is-not-allowed&catid=35:technology&Itemid=55

Best Wishes
You have to disable UAC to be able to do that.
And to successfully do it you have to reboot the machine as well.
So you will have to alter the code a little bit for the purpose....
A workaround would be to toggle the consent behavior of UAC - this does not require a reboot.
disable:
reg ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0 /f
reactivate:
reg ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 2 /f

But this alone is not enough. To trigger scripted elevation (in a batch), you can use elevate, a powertoy by Microsoft: http://technet.microsoft.com/en-us/magazine/2008.06.elevation.aspx
Haha...
But to add anything to HKLM, you need to run elevated as well, so no chance to do this quietly scripted without a reboot.
You should attempt to use the user space instead, which won't require elevated rights.
Under Vista & Windows 7, thanks to UAC, by default even if you're logged in as an administrator, you still have limited access rights. This is to keep normal users who are in the bad habit of loggin in as an administrator from inadvertently doing damage to their system.  In addition to logging in with appropriate rights, you need to specifically ask for administrative access - you can do this by right-clicking on a shortcut or .exe and choosing the "Run as Administrator" option, you can click on properties instead, go to the compatability tab and check the "run as administrator" checkbox to always run this exe/shortcut with elevated privileges.

You should write your code to NOT assume you have administrative access, and should probably choose to write to HKCU instead of HKLM, or to use application settings so that your application does not require administrative access.

However, if you legitimately need admin access you should set UAC settings in your project, so that Windows will know it needs to run with elevated privileges, right click your project, go to properties, click UAC settings, and change requestedExecutionLevel to requireAdministrator.


Of course, all that's moot if the security permissions on the registry key in question prohibit you from writing. ;)
Untitled.png
Avatar of Dominic34

ASKER

Hi,

  I tried to set the "run as administrator" checkbox, but it didn't worked.

I should have mentionned that I execute the program directly from Visual Studio. I don't know if it changed anything regarding the solution...
tgerbert, I tried your solution, and I still have the same exception thrown:


Capture.JPG
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks, it works now!!