Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Reading the registry: ProductID returns null in x86 targeted app. "Any CPU" works fine

I have recently moved to a W7 64bit machine with VS 2010. My project is set to run on Any CPU. When I change this to be targeted at x86 I noticed some of my registry calls no longer work.

I am trying to read the ProductID field like so:

RegistryKey windowsNTKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion");
object productID = windowsNTKey.GetValue("ProductId");

productID is always null when running in x86 mode, when running in "Any CPU" it works correctly. What is going on here?

like image 717
Chris Avatar asked Jun 10 '11 09:06

Chris


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

Some registry keys are redirected by WOW64. More information on this topic is available on MSDN http://msdn.microsoft.com/en-us/library/aa384232(v=vs.85).aspx

If you really want to always access the x64 node (.Net4) :

  RegistryKey localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
  RegistryKey windowsNTKey = localMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion");
  object productID = windowsNTKey.GetValue("ProductId");
like image 155
Guillaume Avatar answered Sep 28 '22 01:09

Guillaume


This code will get the id for all kinds of os architectures and program architectures. Could be written shorter but I like the readability

    static string GetProductId()
    {
        RegistryKey localMachine = null;
        if (Environment.Is64BitOperatingSystem)
        {
            localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
        }
        else
        {
            localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
        }
        RegistryKey windowsNTKey = localMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion");
        return windowsNTKey.GetValue("ProductId").ToString();
    }
like image 44
Oskar Kjellin Avatar answered Sep 28 '22 00:09

Oskar Kjellin