-
Read Registry value
I got a problem with reading a value from the registry.Ive check MSDN what function to use, and i tried RegGetValue, RegQueryalueEx, but RegGetValue is declared, and RegQueryValue returns something what is not right.What function do i need to use?
This is the piece of code
Code:
HKEY key;
DWORD a;
HKEY buffer;
DWORD iNumGet=512;
char bufGet[512];
int OpenKey = MessageBox(
NULL,
"Do you want to check if the key's are there?",
"Open/Write Key",
MB_YESNO
);
if (OpenKey == IDYES) {
RegOpenKeyEx(
key,
"Software\\Nephiroth",
0,
NULL,
&buffer
);
RegQueryValueEx(
key,
"Test",
0,
NULL,
(BYTE *)bufGet,
&iNumGet
);
RegCloseKey(key);
if (bufGet == "texst") {
MessageBox(
NULL,
"works",
"test",
MB_OK
);
} else {
MessageBox(
NULL,
"Nope",
"test",
MB_OK
);
}
} else if (OpenKey == IDNO) {
// not needed stuff here
}
The registry looks like this schematic
HKEY_LOCAL_MACHINE
->Software
->Nephiroth
-"Test" with the value "texst"
Please help me on this one
-
Maybe it has something to do with key being uninitialised.
Y'know, these functions return fairly detailed error information for those bothered enough to look at it.
-
I dont really need the error information, i need the function to use to read a value.
-
> I got a problem with reading a value from the registry
> I dont really need the error information,
When you've resolved your logical inconsistencies - come back with some proper code.
-
The problem is that i dont know the function to use
-
The problem is, you don't initialise variables
> HKEY key;
Which key were you planning to read with this statement?
Perhaps if you'd bothered to look at the error messages, you might have seen RegOpenKeyEx return (I guess) something like "unknown key".
Here, start reading
http://msdn.microsoft.com/library/de...gopenkeyex.asp
> if (bufGet == "texst")
Also, to compare strings, you need to use strcmp()
-
Okey, ive done a couple of things...
Fixed the HKEY key to HKEY key = HKEY_LOCAL_MACHINE
Added return check
and used strcmp() with the string comparison, thanks
i got this now
Code:
if (OpenKey == IDYES) {
if (RegOpenKeyEx(
key,
"Software\\Nephiroth",
0,
NULL,
&buffer
) == ERROR_ACCESS_DENIED) {
MessageBox(
NULL,
"There was an error, opening the registry key",
"Error",
MB_ICONERROR|MB_OK
);
}
RegQueryValueEx(key, "Test", 0, NULL, (BYTE *)bufGet, &iNumGet);
RegCloseKey(key);
if (strcmp (bufGet,"texst") == 0) {
MessageBox(
NULL,
"works",
"test",
MB_OK
);
} else {
MessageBox(
NULL,
"Nope",
"test",
MB_OK
);
}
} else if (OpenKey == IDNO) {
//stuff here
}
I did some testing with the return values, and there comes out ACCES DENIED.Kinda weird actually, as i have made a key before.
EDIT:
Added KEY_READ and KEY_QUERY_VALUE as parameters..no errors now.But isnt giving right answer
-
Code:
HKEY hKey;
char szTest[512] = {0};
DWORD datasize;
if(RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Software\\Nephiroth", 0, KEY_QUERY_VALUE,
&hKey) == ERROR_SUCCESS)
{
datasize = sizeof (szTest);
if (RegQueryValueEx(hKey, "Test",NULL, NULL, (unsigned char *)&szTest,
(unsigned long *)&datasize) == ERROR_SUCCESS)
printf("RegQueryValueEx returned %s\n", szTest);
else
printf("RegQueryValueEx failed\n");
RegCloseKey(hKey);
}
else printf("RegOpenKeyEx failed\n");
-
great i used parts of your method to figure it all out.Got it working now, thanks.