Reading from registry?? (a question)
hi all :D
I searched google on how reading from registry and i have found this code
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
Re: Reading from registry?? (a question)
Re: Reading from registry?? (a question)
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.
Re: Reading from registry?? (a question)
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... ;)
Re: Reading from registry?? (a question)
Quote:
Originally Posted by ovidiucucu
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?!
Re: Reading from registry?? (a question)
Yup! (you should only use Create when you want to...well...ummm...Create)
Re: Reading from registry?? (a question)
Quote:
Originally Posted by TheCPUWizard
Yup! (you should only use Create when you want to...well...ummm...Create)
lol..thx alot :D