0

I'm trying to get a value from a registry key, and the final program has to work on both 32 & 64 bit machines.

The code so far is:

   HKEY hKey; 
   LONG Result1;
   LONG result2;
   Result1 = RegOpenKeyEx(HKEY_CLASSES_ROOT,L"Word.Application\\CurVer",0,KEY_READ,&hKey);
    cout << Result1;
    cout << "\n";
   TCHAR value[255];
   DWORD BufferSize = 255;
   result2 = RegGetValue(hKey, L"Word.Application\\CurVer", L"", RRF_RT_ANY, NULL, (PVOID)&value, &BufferSize);
   cout << result2;

I'm getting the error '2' back from RegGetValue, and looked at this RegOpenKeyEx/RegGetValue return ERROR_FILE_NOT_FOUND on keys that exist which says that it will not work if it's '32 bit code on a 64 bit OS' but I do not understand what this means.

Is it the program that has to be compiled for different architectures, or is it RegGetValue that is specific to 32 bit?

Sorry, most of my C++ programming was done back before 64bit computers became mainstream, and none of the occasional items I've written since have had this problem.

Community
  • 1
  • 1
Louise
  • 382
  • 1
  • 6
  • 16
  • [link](http://msdn.microsoft.com/en-us/library/windows/desktop/ms724072%28v=vs.85%29.aspx) There are 2 spaces of registry - 32bit and 64bit. They are different in term of content and you can access one at a time as far as I am concerned. – Łukasz Daniluk Jun 10 '14 at 10:32
  • 1
    The 32-bit and 64-bit registry keys are in different locations, therefore it depends on which registry it should query. So for 32-bit you need to `OR` your `KEY_READ` flag with `KEY_WOW64_32KEY` for 64-bit you `OR` with `KEY_WOW64_64KEY` this is for a 64-bit app to query either the 32-bit or 64-bit keys. Normally you can detect this using `ifdef _M_X64` for 64-bit and then set the flag accordingly. See http://msdn.microsoft.com/en-gb/library/windows/desktop/ms724897%28v=vs.85%29.aspx and http://msdn.microsoft.com/en-gb/library/windows/desktop/ms724878%28v=vs.85%29.aspx – EdChum Jun 10 '14 at 10:36
  • No need for `ifdef _M_X64`. You can use `KEY_WOW64_32KEY` and `KEY_WOW64_64KEY` unconditionally. – David Heffernan Jun 10 '14 at 10:44
  • @Louise You need to decide which registry view you wish to read from. So, which is it to be? – David Heffernan Jun 10 '14 at 10:47
  • @DavidHeffernan that is true but it would depend on whether you really want to read which key and this may be dependant on compiler and your app preferences but you are correct, the flag is ignored for 32-bit windows but you would still need it if you are on 64-bit windows – EdChum Jun 10 '14 at 10:48
  • @EdChum What matters if not the state of `_M_X64`, but the registry view that the program needs to read from. That is independent from the architecture of the executing code. – David Heffernan Jun 10 '14 at 10:50
  • @DavidHeffernan is correct, the reg query should work whether your app is 32 or 64-bit, in our companies' case we migrated the registry from 32 to 64-bit so we had to check the old 32-bit registry in some cases – EdChum Jun 10 '14 at 10:54
  • When I ran 'Reg Query "HKEY_CLASSES_ROOT\Word.Application\CurVer"' on both types of machines in a batch file, they all worked. So this means this registry key is present in both 32 & 64 bit registries? so for it to work on both, I presume I have to run a call on both registries and then work with whichever returns the non-error value? – Louise Jun 10 '14 at 10:58

1 Answers1

2

On 64 bit Windows there are two registry views, the 32 bit view and the 64 bit view. This is described over on MSDN in the topic titled Accessing an Alternate Registry View.

By default a 32 bit process will read from the 32 bit view, and a 64 bit process will read from the 64 bit view. If you wish to read from a particular view, irrespective of the architecture of the process you need to supply one of the following flags: KEY_WOW64_64KEY or KEY_WOW64_32KEY.

So, if the data that you need is in the 32 bit view, pass KEY_WOW64_32KEY. If the data is in the 64 bit view pass KEY_WOW64_64KEY. If the data could be in either key, check twice, once passing KEY_WOW64_32KEY and once again passing KEY_WOW64_64KEY.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490