Reading Registry values don't work on some keys

I'm having an odd problem with this part of my code. I'm trying to read the system colors from the registry and if I try to read ActiveBorder, it returns the value from MenuBar, If I try to read ActiveText, it gets the value from MenuBar. and if I read Background and so on, it reads the correct values. For some reason when I try to read some of the Multie String values, it gets the value from another one with that key instead of the correct one.

I'm using Windows 7 btw

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int RegEdit(){
 HKEY hKey = 0;
char buf[MAX_PATH];
DWORD dwType = 0;
DWORD dwBufSize = MAX_PATH;
const char* subkey = "Control Panel\\Colors";
if( RegOpenKeyEx(HKEY_CURRENT_USER,subkey,0,KEY_QUERY_VALUE,&hKey) == ERROR_SUCCESS){
dwType = REG_SZ;
if( RegQueryValueEx(hKey,"ActiveBorder",NULL, NULL, (BYTE*)buf, &dwBufSize) == ERROR_SUCCESS)
{
cout << "key value is '" << buf << "'\n";
}
else
cout << "can not query for key value\n";
RegCloseKey(hKey);
}
}


If you change "ActiveBorder" to "InfoWindow" it will then return the correct value.

This has me very confused. I've been fighting with this for the last couple of days

Thank you
Last edited on
I don't know what compiler you're using. try this:
result = RegOpenKeyEx(HKEY_CURRENT_USER,L"Control Panel\\Colors",0,KEY_QUERY_VALUE,&hKey);
then test for result.

See what happens. your code returned 2 when I chopped it up.
also, I did this to output the value.
1
2
3
4
5
6
7
8
9
string keyVal = string(buf);
int i = 0;
do
     {
	cout << buf[i];
	i++;
    }	
	while(i<dwBufSize);
	cout<<"\n";


edit: the string isn't necessary, I was just funnin.
Last edited on
You're not passing in the type parameter.
Thank you bob, I'm using wxDevC++ and I tried the code and its still returning the wrong value. Every time I try to get the value for ActiveBorder, it gets the value from Menu instead. From AppWorkspace and down the list it gets the correct values. Its just having issues with the first 2 values and idk, it just keeps getting the value from the wrong key.



I also tried the following:

1
2
int result = RegOpenKeyEx(HKEY_CURRENT_USER,"Control Panel\\Colors",0,KEY_QUERY_VALUE,&hKey);
cout << result <<"\n";


and it outputs 0.

I wasn't able to use the "L" as it gave the following error:
 
68 H:\Projects\PWS_DEV\PWS_DEV.h cannot convert `const wchar_t*' to `const CHAR*' for argument `2' to `LONG RegOpenKeyExA(HKEY__*, const CHAR*, DWORD, REGSAM, HKEY__**)'


Thanks for your help Bob, I just don't know whats causing this. Its not like its doing it on all of them , like if I get the value of Menu, it return correct, and some other keys as well but when I try to read ActiveBorder, I get the value from Menu. If I try to read ActiveTitle I get the value from MenuBar.

If I try to read Background, I get the correct value from Background, it only acts up with ActiveBorder and ActiveTitle
kbw: the parameter type is optional in regqueryvalueex

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724911%28v=vs.85%29.aspx
Senith: the output of 0 for result is correct. that is no error. so reset result to 0 again and use it to check regqueryvalueex and make sure it's no error also. do a memset before each read to clear out your buffer. let me know if that helps. if not, post all of your code and we'll go back thru it.
Here is the full code for the registry part. The rest of my code is just custom functions that I came up with that doesn't have anything to do with the registry.

If you will still need it though let me know.

Also I call it at WM_CREATE and if I try to read ActiveBorder, or ActiveTitle I get the wrong values. If its any other such as Menu or Background, its the correct value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int RegEdit(){
 HKEY hKey = 0;
char buf[MAX_PATH];
DWORD dwType = 0;
DWORD dwBufSize = MAX_PATH;
const char* subkey = "Control Panel\\Colors";
int result = RegOpenKeyEx(HKEY_CURRENT_USER,"Control Panel\\Colors",0,KEY_QUERY_VALUE,&hKey);
cout << result <<"\n";

if( RegOpenKeyEx(HKEY_CURRENT_USER,"Control Panel\\Colors",0,KEY_QUERY_VALUE,&hKey) == ERROR_SUCCESS){
dwType = REG_SZ;
if( RegQueryValueEx(hKey,"ActiveBorder",NULL, NULL, (BYTE*)buf, &dwBufSize) == ERROR_SUCCESS)
{
cout << "key value is '" << buf << "'\n";
}
else
cout << "can not query for key value\n";
RegCloseKey(hKey);
}
//string keyVal = string(buf);
int i = 0;
do
     {
	cout << buf[i];
	i++;
    }	
	while(i<dwBufSize);
	cout<<"\n";

}


This was some code that I found online and I modified it some. I read though all the info for the functions and API's, I just don't know what can be causing it.

Thanks everyone and thanks bob.
Last edited on
Without reading the type, you can't know how to interpret the buffer that comes back.
I've tried this on two different machines and it works fine. I'll try kbw's suggestion in a second.
try this for now take out the L from my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int RegEdit(){
 HKEY hKey = 0;
char buf[MAX_PATH];
DWORD dwType = 0;
DWORD dwBufSize = MAX_PATH;
const char* subkey = "Control Panel\\Colors";
int result = RegOpenKeyEx(HKEY_CURRENT_USER,L"Control Panel\\Colors",0,KEY_QUERY_VALUE,&hKey);
cout << result <<"\n";

result = 0;
result = RegOpenKeyEx(HKEY_CURRENT_USER,L"Control Panel\\Colors",0,KEY_QUERY_VALUE,&hKey);

if(result == ERROR_SUCCESS)
	{
	dwType = REG_SZ;
	result = 0;
	result = RegQueryValueEx(hKey,L"ActiveBorder",NULL, NULL, (BYTE*)buf, &dwBufSize);
	if(result == ERROR_SUCCESS)
		{
			cout << "key value is '" << buf << "'\n";
			//string keyVal = string(buf);
			int i = 0;
			do
				{
					cout << buf[i];
					i++;
				}	
			while(i<dwBufSize);
		cout<<"\n";
		}
	else
	{
		cout << "can not query for key value\n";
	}

RegCloseKey(hKey);
return 0;
}



}
I just tried your code bob and now its working. Thank you for your help and I have no idea why it was acting the way it was.

Its all working now.

Now its returning the correct values for ActiveBorder and ActiveTitle.


I'm still stumped on while it was doing this.

I'm using Windows 7 and wxDevc++, I don't know if any of that matters but it still bathers me. Everything is working now and if anything else comes up about this I'll let you all know.

and again thank you bob and everyone else ^_^
glad to hear it. this is my cleaned up code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int RegEdit()
{
	HKEY hKey = 0;
	char buf[MAX_PATH];
	DWORD dwType = 0;
	DWORD dwBufSize = MAX_PATH;
	const char* subkey = "Control Panel\\Colors";
	int result = RegOpenKeyEx(HKEY_CURRENT_USER,L"Control Panel\\Colors",0,KEY_QUERY_VALUE,&hKey);
	//cout << result <<"\n";

	result = 0;
	result = RegOpenKeyEx(HKEY_CURRENT_USER,L"Control Panel\\Colors",0,KEY_QUERY_VALUE,&hKey);

	if(result == ERROR_SUCCESS)
		{
		dwType = REG_SZ;
		result = 0;
		result = RegQueryValueEx(hKey,L"ActiveBorder",NULL, &dwType, (BYTE*)buf, &dwBufSize);
		if(result == ERROR_SUCCESS)
			{
				cout << "key value is '" << buf << "'\n";
				//string keyVal = string(buf);
				int i = 0;
				do
					{
						cout << buf[i];
						i++;
					}	
				while(i<dwBufSize);
			cout<<"\n";
			}
		else
		{
			cout << "can not query for key value\n";
		}

	
	}//end if
	RegCloseKey(hKey);
	return 0;
}//end function 
Topic archived. No new replies allowed.