RegQueryValueEx Problem

Jul 11, 2014 at 3:42am
I'm trying to retrieve a value from the registry. InstallDate is a REG_DWORD value and GetInstallDate returns true, but returnstr is "0". return_size is 4 as would be expected. I really don't know what's going wrong here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool GetInstallDate(std::string &returnstr)
{
std::string reg_key="SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
std::string reg_name="InstallDate";

HKEY hkey; DWORD dwdispos;
LONG created_key=RegCreateKeyEx(HKEY_LOCAL_MACHINE, reg_key.c_str(), 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE, NULL, &hkey, &dwdispos);
if (created_key==ERROR_SUCCESS)
{
	DWORD return_type;
	DWORD return_size=sizeof(DWORD);
	DWORD return_value;
	LONG got_value=RegQueryValueEx(hkey, reg_name.c_str(), 0, &return_type,
        reinterpret_cast<LPBYTE>(&return_value), &return_size);
	returnstr=to_string((long long)return_value);
	RegCloseKey(hkey);
	if (got_value==ERROR_SUCCESS) {return true;}
}
return false;
}

Thanks in advance for enlightening me
Jul 11, 2014 at 6:50am
What is the value of return_type? Is it REG_DWORD?

Why are you calling RegCreateKeyEx? Shouldn't you use RegOpenKeyEx?

Why is there are cast in to_string()?

Otherwise, it looks fine to me.
Jul 11, 2014 at 5:56pm
Yes, it's REG_DWORD.
Changed that line to LONG created_key=RegOpenKeyEx(HKEY_LOCAL_MACHINE, reg_key.c_str(), 0, KEY_QUERY_VALUE, &hkey);
still not having any luck.
Without the cast, the function call is ambiguous.
Jul 11, 2014 at 6:35pm
Figured out what the problem is. The registry call is being redirected to SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion
The 'InstallDate' value at that location is actually 0. Is it possible to prevent this redirection?
Jul 11, 2014 at 8:24pm
@ OP: Yes, but you shouldn't get into the habit of leaning on the registry for this kind of stuff for this among other reasons. The Win32_Product class has the data you need in a property titled "InstallDate2".
Jul 11, 2014 at 11:19pm
That solved my problem, thank you.
Topic archived. No new replies allowed.