3

I have a little app which reads registry key string values. It works well but for some reason it fails on this key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductId Despite working on other values of HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\

It also fails on `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\MachineGuid'

I am running as an admin, is this a factor? I'm running W7 64bit, another W7 machine and Vista machine both work fine. My only guesses are some permissioning issue, or related to me running 64-bit.

update: It appears to be something to do with my system running Windows 64bit, and \Software\Wow6432Node\. I don't know what that is though. I have both \Software\Wow6432Node\Microsoft\Windows NT\CurrentVersion\ and \Software\Microsoft\Windows NT\CurrentVersion\ but only the latter contains ProductId value... for some reason when I ask for the key Windows is apparently looking in the Wow6432Node

We're using wxWidgets but could probably use some win32 code directly if needed... our app is a 32bit application but target PCs could be running 32 or 64 bit versions of Windows

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • 1
    If you don't show us the code, we can't help you. Have you looked at the ACL? It doesn't necessarily grant to the particular admin user you are running at. What error code do you get? – bmargulies Sep 06 '10 at 17:31
  • It's wrapper code in wxWindows... you just pass in the key path and for everything else it works. – Mr. Boy Sep 06 '10 at 17:35
  • Can you expand on 'it fails'? Surely there's an error code returned, can you tell us what it is? – James Sep 06 '10 at 18:40
  • The value is shown as not existing. – Mr. Boy Sep 06 '10 at 18:54

3 Answers3

2

It's due to WOW64. This other question focuses on the details.

Community
  • 1
  • 1
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
1

I got the following code to work on a 32 bit XP box and a 64 bit Win 7 box. I think that should cover most bases.

// start out trying to read machine guid on 32 bit machine
object value = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography", @"MachineGuid", (object) "defaultValue");

if (value != null && value.ToString() != "defaultValue")
{
    return value.ToString();
}

// read machine guid on 64 bit machine
RegistryKey regKeyBase = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey regKey = regKeyBase.OpenSubKey(@"SOFTWARE\Microsoft\Cryptography", RegistryKeyPermissionCheck.ReadSubTree);
value = regKey.GetValue("MachineGuid", (object) "defaultValue");

regKeyBase.Close();
regKey.Close();

if (value != null && value.ToString() != "defaultValue")
{
    return value.ToString();
}

return string.Empty;
0

I just looked at my registry and there's no key at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductId or MachineGuid, this is on Win7 64-bit

James
  • 9,064
  • 3
  • 31
  • 49
  • When I run regedit, I see ProductId in `SOFTWARE\Microsoft\Windows NT\CurrentVersion\` but not `SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\`. Can I force regedit to work in 32/64 bit mode? – Mr. Boy Sep 06 '10 at 18:56
  • 1
    This is an insanely late comment, but you can. Use "regedit" for 64 bit registry access, and "regedt32" for 32 bit registry access. Applies only to 64 bit windows. – Wug Aug 06 '12 at 16:08