views:

2802

answers:

4

I'm writing a tweak utility that modifies some keys under HKEY_CLASSES_ROOT.

All works fine under Windows XP and so on. But I'm getting error Requested registry access is not allowed under Windows 7. Vista and 2008 I guess too.

How should I modify my code to add UAC support?

+8  A: 

You can't access the HKCR (or HKLM) hives in Vista and newer versions of Windows unless you have administrative privileges. Therefore, you'll either need to be logged in as an Administrator before you run your utility, give it a manifest that says it requires Administrator level (which will prompt the user for Admin login info), or quit changing things in places that non-Administrators shouldn't be playing. :-)

Ken White
thanks for keyword 'manifest' :)
abatishchev
You're welcome. :-) Sorry I couldn't post the proper manifest, but I didn't have one on this machine and figured if someone had to search for one, it might as well be you. <g>
Ken White
+8  A: 

app.manifest should be like this:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
   <assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
      <security>
         <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
         </requestedPrivileges>
      </security>
   </trustInfo>
</asmv1:assembly>
abatishchev
+2  A: 

As a temporary fix, users can right click the utility and select "Run as administrator."

Brian
+4  A: 

If you don't need admin privs for the entire app, or only for a few infrequent changes you can do the changes in a new process and launch it using:

Process.StartInfo.UseShellExecute = true;
Process.StartInfo.Verb = "runas";

which will run the process as admin to do whatever you need with the registry, but return to your app with the normal priviledges. This way it doesn't prompt the user with a UAC dialog every time it launches.

Davy8
Do you mean that it requires to implement a fork, where one part of code launches the same application with parameter so another part of code will be executed?
abatishchev
It could be the same app with parameters or it could be a separate small windowless app that writes what it needs.
Davy8