|
-
June 1st, 2007, 10:15 PM
#1
Reading from registry?? (a question)
hi all 
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
-
June 2nd, 2007, 02:32 PM
#2
Re: Reading from registry?? (a question)
-
June 2nd, 2007, 02:35 PM
#3
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.
-
June 2nd, 2007, 03:35 PM
#4
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...
Last edited by ovidiucucu; June 2nd, 2007 at 03:44 PM.
-
June 2nd, 2007, 09:27 PM
#5
Re: Reading from registry?? (a question)
 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?!
-
June 2nd, 2007, 10:44 PM
#6
Re: Reading from registry?? (a question)
Yup! (you should only use Create when you want to...well...ummm...Create)
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
June 2nd, 2007, 11:40 PM
#7
Re: Reading from registry?? (a question)
 Originally Posted by TheCPUWizard
Yup! (you should only use Create when you want to...well...ummm...Create)
lol..thx alot
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|