Click to See Complete Forum and Search --> : Reading from registry?? (a question)
S-I-B
June 1st, 2007, 10:15 PM
hi all :D
I searched google on how reading from registry and i have found this code
int main()
{
char buffer[500];
unsigned long size = sizeof(buffer);
DWORD type;
HKEY hKey;
RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Visual Basic 5.0",&hKey);
RegQueryValueEx(hKey,"Version",NULL,&type,(LPBYTE)buffer,&size);
RegCloseKey(hKey);
cout << buffer << endl;
system("pause");
return 0;
}
but i have a question, what if the size of the value in the registry key is bigger than 500 ?!!!
is there's a way to fix this problem without making the size of buffer 9999999999 lol
Marc G
June 2nd, 2007, 02:32 PM
[ moved thread ]
Marc G
June 2nd, 2007, 02:35 PM
You shouldn't store such big data in the registry.
If your data exceeds a certain size, it's much better to write it to some file.
Anyway, you can create your buffer dynamically.
First call RegQueryValueEx with lpData set to NULL. Then the required size will be returned in the size variable. Then allocate your dynamic buffer with new and call RegQueryValueEx with your buffer. Don't forget to call delete on your buffer once your done with it.
ovidiucucu
June 2nd, 2007, 03:35 PM
Beside that Marc G already stated.
It has no sense to "open" a key using RegCreateKey when you need it for reading a value. If the user is not an administrator, most possible that function will fail.
You have to check the returned values from RegXX functions to prevent your program going in the binary heaven (or hell) :)
After calling first time RegQueryValueEx with lpData set to NULL, verify if the type value is indeed REG_SZ and test size against a "paranoid" but of common sense limit (which can be enough for string values like "Version").
Never know... a wise guy can put some (huge) garbage in the registry and again, your program can go in the binary... ;)
S-I-B
June 2nd, 2007, 09:27 PM
Beside that Marc G already stated.
It has no sense to "open" a key using RegCreateKey when you need it for reading a value. If the user is not an administrator, most possible that function will fail.
thx for the help..So what should i use?
RegOpenKey?!
TheCPUWizard
June 2nd, 2007, 10:44 PM
Yup! (you should only use Create when you want to...well...ummm...Create)
S-I-B
June 2nd, 2007, 11:40 PM
Yup! (you should only use Create when you want to...well...ummm...Create)
lol..thx alot :D
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.