views:

78

answers:

2

Is is possible to write to the HKLM registry branch in Win 7 from an application?

My existing code is not able to write to the HKLM registry branch on Win 7 machines, while it is able to do this on XP machines.

How do you allow an application read/write access to HKLM on Win 7, or should all applications now just use HKCU instead? What if I need to store settings on a machine basis rather than a user basis?

A: 

Win 7 uses Registry Virtualization

Read the article and look into (HKEY_USERS\<User SID>_Classes\VirtualStore\Machine\Software).

belisarius
Does that mean that I cannnot access the HKLM branch from an application?
Craig Johnston
Processes with requestedExecutionLevel in their manifest don't access a virtualized registry (But they still need the user they run as to have the right to edit/read the keys as with any NT system).
VirtualBlackFox
@VirtualBlackFox yep, but I don't know if there is a way to get rid of the infamous UAC
belisarius
@belisarius Running under UAC is the same as running as an unprivileged user except that you have a simpler way to ask the user for an elevation of privilege (via the UAC prompt) than when a non-privileged user is really logged on. But yes if you want Administrator access to the registry most of the time it will mean an UAC prompt for your users.
VirtualBlackFox
A: 

You need to decide whether you are writing an administrative app, that deliberately changes settings for all users (by writing to HKLM) or an ordinary app, that does not. If you really are writing an administrative app then put a manifest on it that has a requestedExecutionLevel of requireAdministrator. The user will get a UAC prompt every time they run the app, but your writes to HKLM will succeed. Alternatively, change the app to write to HKCU or some other per-user store.

(No idea how to add a manifest? Tell me what language/IDE you're using and I'll try to help.)

Relying on virtualization is a bad idea. It was implemented to let unmanifested applications at least sorta kinda work. It will go away some day and is not that great while it's here.

Kate Gregory
@Kate: if I choose to confine my writes to HKCU only, how can I configure machine-level settings for the application?
Craig Johnston
Well that's the point. If you really need machine-wide settings, then you're an admin app. Put on a manifest that says so. A lot of people write to HKLM out of habit without thinking about it. They could switch to HKCU with no consequence. Part of UAC is identifying which apps do admin things and which do not. Sounds like your app does admin things. Give it a manifest that says so.
Kate Gregory