0

Here is a simplified example of a code I use in my XPCOM CPP DLL to check if a key exists in the registry. It checks for the existance of 2 keys: HKLM\SOFTWARE\Microsoft and HKLM\SOFTWARE\Microso both exist with the same permissions, but the first one is found by this code and the second one is not... any idea why?

nsCOMPtr<nsIWindowsRegKey> regKey = 
    do_CreateInstance("@mozilla.org/windows-registry-key;1");
if (!regKey) {
    log("can't create @mozilla.org/windows-registry-key;1");
    return -1;
}
NS_NAMED_LITERAL_STRING(key2,
   "SOFTWARE\\Microsoft");
if (NS_FAILED(regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
                          key2, nsIWindowsRegKey::ACCESS_QUERY_VALUE))) {

     // FAILED
    LOG("regKey:: no such key");

}

NS_NAMED_LITERAL_STRING(key1,
   "SOFTWARE\\Microso");
if (NS_FAILED(regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
                          key1, nsIWindowsRegKey::ACCESS_QUERY_VALUE))) {

    // FAILED
    LOG("regKey:: no such key");

}

EDIT: To make it clear, I've created a registry key myself, called HKLM\SOFTWARE\Microso and I can access it through regedit.

zenpoy
  • 19,490
  • 9
  • 60
  • 87

2 Answers2

0

nsIWindowsRegKey.Open is implemented via RegOpenKeyEx WinAPI function. This function requires existing relative path passed as argument and does not support templates. If there is no exactly same path in the registry, it falls. SOFTWARE\Microsoft is exists in the HKLM root space, HKLM\SOFTWARE\Microso is not.

Serge Z
  • 425
  • 2
  • 11
0

The issue is most likely that you are viewing the registry with the x64 regedit and then expecting Firefox to have the same view. However, regedit is an x64 application whereas the usual Firefox builds are x86 and run inside the 32-bit subsystem (via WoW64). When an x86 application accesses the HKLM\Software key it gets redirected to HKLM\Software\Wow6432Node. You can run c:\Windows\SysWOW64\regedit.exe instead of c:\Windows\regedit.exe to see the view of the registry that an x86 application gets.

So your XPCOM component is actually trying to access HKLM\Software\Wow6432Node\Microsoft and HKLM\Software\Wow6432Node\Microso - and the former exists while the latter doesn't. If you want to access the "real" registry key instead, you have to pass the WOW64_64 flag in the third parameter of nsIWindowsRegKey.open() (it corresponds to the KEY_WOW64_64KEY flag that can be passed to the RegOpenKeyEx WinAPI function):

NS_NAMED_LITERAL_STRING(key1,
   "SOFTWARE\\Microso");
if (NS_FAILED(regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT, key1,
                          nsIWindowsRegKey::ACCESS_QUERY_VALUE | nsIWindowsRegKey::WOW64_64))) {

    // FAILED
    LOG("regKey:: no such key");

}
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126