Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WOW6432 registry node messin things up

Trying to do the simple task of writing to the registry to make a C# application run at startup.

Using the basic Win32.RegistryKey setup but for some reason it keeps adding my keys into the /SOFTWARE/WOW6432/Microsoft/Windows.. etc directory instead of plain ol /SOFTWARE/Microsoft/Windows..

Tried reading up on it a bit but there didn't seem to be a simple answer to this question: How to I specifically write a key to the /SOFTWARE/Microsoft/Windows Registry Key instead of it writing to WOW6432? I've checked to make sure my Visual C# Express solution file had the platform listed as x86... so it's compiling correctly... I just don't want that wow6432 directory.

Thanks for any advice!

Edit:

I'm now using the following and still no success:

Microsoft.Win32.RegistryKey localKey32 = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry64);
like image 497
Goober Avatar asked Jan 17 '23 17:01

Goober


2 Answers

Your process targets x86 (i.e. it is a 32-bit process) and when run on a 64-bit machine under the WOW64 emulator, registry redirection comes into play. For certain parts of the registry the system maintains two distinct views, one for 32 bit processes and one for 64 bit processes. The list of keys affected by redirection is here: Registry Keys Affected by WOW64.

Redirection is transparent to an application. A 32 bit process access HKLM\Software and does not know (indeed does not need to know), that the 64 bit OS is actually accessing HKLM\Software\Wow6432Node.

You have a number of options available to you:

  1. Switch to the AnyCPU target so that your process will run as either 32 bit or 64 bit depending on the underlying OS. This is awkward to achieve in the Express version because the target platform cannot be specified from the IDE.
  2. Explicitly open a 64 bit view of the registry. In .net this requires the RegistryView enumeration. However, note that this functionality requires .net 4 and for earlier versions of .net, p/invoke is required to open views of the registry.
  3. Continue to target x86 and write to HKLM\Software. A 64-bit system will read both views of the registry when processing the run at startup registry keys. In other words your existing approach already works!

As a final point I would comment that the task of setting up this registry key is best left to an installation program. Modify keys under HKLM\Software requires admin rights and typically you can only expect to have admin rights at install time.

like image 71
David Heffernan Avatar answered Jan 29 '23 14:01

David Heffernan


Try to explicitly open the registry in 64 bit view by using Registry64 in RegistryView. Similarly you can open 32 bit view using Registry32 option

According to MSDN

You can specify a registry view when you use the OpenBaseKey and OpenRemoteBaseKey(RegistryHive, String, RegistryView) methods, and the FromHandle property on a RegistryKey object.

like image 35
Haris Hasan Avatar answered Jan 29 '23 13:01

Haris Hasan