问题
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.
回答1:
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
.
来源:https://stackoverflow.com/questions/24138574/what-is-32-64-bit-c-code