Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to HKEY_LOCAL_MACHINE from .Net doesn't get virtualized in Vista or Windows 7?

I have trouble understanding the VirtualStore virtualization of registry operations in Vista and/or Windows 7. I thought trying to write something into the HKEY_LOCAL_MACHINE root while running as a standard user would prompt Windows to virtualize the operation and write to HKEY_CURRENT_USER/Classes/VirtualStore/MACHINE instead. I've seen several older applications behave exactly like that.

However, when trying to replicate that behaviour in C# and .Net4, the write operation to HKLM just fails with an UnauthorizedAccessException instead. Is there some way to force virtualization?

To give some context to the question: I'm trying to read and manipulate registry keys originally created by another legacy app. In Vista or Windows 7 these will be located in the VirtualStore. I'd like to use the same registry access logic both for WinXP and above and thought my registry operations would be virtualized just the same as the operations from the legacy app (and would operate on the same keys, because of that). This doesn't work as expected and the only workaround I know is to specifically access the VirtualStore path, if the user is running Vista or 7 and has UAC enabled... seems ugly, though, especially if Microsoft decides to change virtualization behaviour in future Windows versions.

like image 560
Mario Avatar asked Jan 23 '11 23:01

Mario


1 Answers

The MSDN article Registry Virtualization in Windows Vista explains that certain classes of processes have virtualization disabled:

  • 64-bit Processes
  • Executables that have a requestedExecutionLevel specified in the manifest
  • and some others; check the article for details

One or both of the first two are typically true for a .NET application (due to the compiler-supplied default manifest). You would have to remove the manifest (so that your application looks like a legacy app) in order to enable virtualization, but this would be a bad idea (for forwards compatibility with Windows). Instead, just assume that your app isn't virtualized on Vista (and later) and proceed accordingly.

Additionally, the article warns that "Microsoft intends to remove this form of virtualization from future versions of the Windows operating system ... it is imperative your application does not take a hard dependency on the presence of virtualization in the system." Whatever code you write should anticipate that the VirtualStore key may not exist on a future version of Windows.

like image 164
Bradley Grainger Avatar answered Oct 20 '22 15:10

Bradley Grainger