3

I'm using RegOpenKeyEx() and RegQueryValueEx() to try and get the value for six keys in the Windows registry. I'm able to do it for four of the six but am failing on certain others.

wchar_t * getRegKeyValue(HKEY rootKeyToGet, LPCWSTR subKeyToGet, LPCWSTR valueToGet) { HKEY resultHKey = 0; wchar_t resultString[255] = L""; DWORD dwType = REG_SZ; DWORD resultSize = 255; // See if the subkey exists. If it does, get its value. if (RegOpenKeyEx(rootKeyToGet, subKeyToGet, NULL, KEY_ALL_ACCESS, &resultHKey) == ERROR_SUCCESS) { RegQueryValueEx(resultHKey, valueToGet, NULL, &dwType, (LPBYTE) &resultString, &resultSize); } RegCloseKey(resultHKey); resultHKey = NULL; RegCloseKey(rootKeyToGet); rootKeyToGet = NULL; return resultString; } 

The following are some successful calls:

swprintf(buffer, L"&ie=%s", getRegKeyValue(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Internet Explorer", L"Version")); swprintf(buffer, L"&os=%s.", getRegKeyValue(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", L"CurrentVersion")); wcscat(url, getRegKeyValue(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", L"CurrentBuild")); 

Example of an unsuccessful call:

wcscpy(buffer, getRegKeyValue(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", L"CSDVersion")); 

I'm able to open the key in the unsuccessful call but the query for that value returns an empty string. I'm running Visual Studio as an administrator. Have been scratching my head for the last day on where I am going wrong.

Update: The code returned is ERROR_FILE_NOT_FOUND. The codes are most definitely shown to exist in regedit.

3
  • 1
    There are multiple problems here. You are requesting write access even though you only intend to read. You are returning a pointer to a local variable. Your resultSize is initialized incorrectly. You are closing HKEY_LOCAL_MACHINE. You never check the error code from RegQueryValueEx. That last bit explains why you're getting nothing back. Commented Sep 30, 2011 at 16:10
  • What are the dangers of requesting more access than I need? I recently added the RegCloseKey(rootKeyToGet) in at an attempt to solve the problem. What negative effects does this have? I will check that error code. Commented Sep 30, 2011 at 16:19
  • 2
    If you request more access than you need, then the request may fail because you don't have one of those extra accesses that you never cared about. And closing HKEY_LOCAL_MACHINE is probably a bad idea since you're closing a key you never opened. Commented Sep 30, 2011 at 16:42

1 Answer 1

6

I guess that you have a 32 bit process and a 64 bit machine. When this happens, registry redirection confounds matters. Attempts to read HKLM\Software\... get redirected to HKLM\Software\Wow64Node\.... So you need to open the 64 bit view of the registry with the RegistryView enumeration.

Sign up to request clarification or add additional context in comments.

6 Comments

It appears that you're right about the key not being present in my wow6432node stuff. Why do I even have a 32 bit registry with seemingly congruent entries? Seems redundant. Is it in place to that 32 bit applications can access it?
Yes, 32 bit processes have a separate view of some parts of the registry. Read this: msdn.microsoft.com/en-us/library/aa384232(v=VS.85).aspx
Note that GetVersionEx is the supported way of getting the operating system version and CSD version. Make it do the work of finding the right registry keys.
@Raymond Chen It never occurred to me to look at which keys were being looked at. But you are quite right that registry hacking for version info is bad.
If you are on .net 4 then you should use RegistryView. Otherwise you need to P/invoke - yeuch!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.