Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
926 views
in Technique[技术] by (71.8m points)

windows - 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 HKLMSoftware and does not know (indeed does not need to know), that the 64 bit OS is actually accessing HKLMSoftwareWow6432Node.

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 HKLMSoftware. 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 HKLMSoftware requires admin rights and typically you can only expect to have admin rights at install time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...