How do I read the registry?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 90
Reputation: Brent.tc is an unknown quantity at this point 
Solved Threads: 1
Brent.tc Brent.tc is offline Offline
Junior Poster in Training

How do I read the registry?

 
0
  #1
Mar 6th, 2007
I would like to know how to read a value from the registry. And store that value in a variable...
I use Bloodshed ide and ?mingw? compiler, NOT visual c++ so please do not give answers if they only apply to vc++.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,642
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 471
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: How do I read the registry?

 
0
  #2
Mar 6th, 2007
The only requirement is that you should be working on a Windows OS and the compiler you use should have the required header files ( windows.h)

Also read this.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: How do I read the registry?

 
0
  #3
Mar 7th, 2007
Originally Posted by ~s.o.s~ View Post
The only requirement is that you should be working on a Windows OS and the compiler you use should have the required header files ( windows.h)

Also read this.
To reinforce the information ~S.O.S~ has provided on the post linked to by 'this', you can referrence Microsofts website. Here is the linke: http://msdn2.microsoft.com/en-us/library/ms838625.aspx

Good luck, LamaBot
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 90
Reputation: Brent.tc is an unknown quantity at this point 
Solved Threads: 1
Brent.tc Brent.tc is offline Offline
Junior Poster in Training

Re: How do I read the registry?

 
0
  #4
Mar 7th, 2007
Okay, I appreciate the help, but could someone please just give me a quick example, say I want to read from the value
"HKEY_CURRENT_USER\something\more\value"
This is a made up value of course, but how would I do it...
I want the value stored in a variable called TheValue
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: How do I read the registry?

 
0
  #5
Mar 7th, 2007
Originally Posted by Brent.tc View Post
Okay, I appreciate the help, but could someone please just give me a quick example, say I want to read from the value
"HKEY_CURRENT_USER\something\more\value"
This is a made up value of course, but how would I do it...
I want the value stored in a variable called TheValue
Bernt.tc did you care to click the link I'd provided above? The page contains sample code. Here is an example nevertheless:

  1. #include <windows.h>
  2. #include <malloc.h>
  3.  
  4. #define TOTALBYTES 8192
  5. #define BYTEINCREMENT 1024
  6.  
  7. DWORD BufferSize = TOTALBYTES;
  8. PPERF_DATA_BLOCK PerfData = NULL;
  9.  
  10. while( RegQueryValueEx( HKEY_PERFORMANCE_DATA,
  11. TEXT("Global"),
  12. NULL,
  13. NULL,
  14. (LPBYTE) PerfData,
  15. &BufferSize ) == ERROR_MORE_DATA )
  16. {
  17. // Get a buffer that is big enough.
  18.  
  19. BufferSize += BYTEINCREMENT;
  20. PerfData = (PPERF_DATA_BLOCK) realloc( PerfData, BufferSize );
  21. }

The RegQueryValueEx retrievs the type and data of the value specified for what ever key you want to read from. Also, and just my opinion, when ever I have to write a program or script that is relatively simple I just write batch scripts using the REG command and associated operations (i.e. REG QUERY) and just check the %ERRORLEVEL% Global variable to see if a key exists. Anyway just a thought. Here is a link to the Microsoft website where you can find the above example as well as a more thorough description of RegQueryValueEx and other related Registry functions used in C\C++: http://msdn2.microsoft.com/en-us/library/ms724911.aspx

Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 7th, 2007 at 1:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,577
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How do I read the registry?

 
0
  #6
Mar 7th, 2007
Here is another example
  1. #include <windows.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. HKEY hKey = 0;
  8. char buf[255] = {0};
  9. DWORD dwType = 0;
  10. DWORD dwBufSize = sizeof(buf);
  11. const char* subkey = "Software\\Microsoft\\Office\\PowerPoint\\AddIns\\MultiMgrAddIn.AddInDesigner1";
  12.  
  13. if( RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&hKey) == ERROR_SUCCESS)
  14. {
  15. dwType = REG_SZ;
  16. if( RegQueryValueEx(hKey,"Description",0, &dwType, (BYTE*)buf, &dwBufSize) == ERROR_SUCCESS)
  17. {
  18. cout << "key value is '" << buf << "'\n";
  19. }
  20. else
  21. cout << "can not query for key value\n";
  22. RegCloseKey(hKey);
  23.  
  24. }
  25. else
  26. cout << "Can not open key\n";
  27. cin.ignore();
  28.  
  29. return 0;
  30. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 90
Reputation: Brent.tc is an unknown quantity at this point 
Solved Threads: 1
Brent.tc Brent.tc is offline Offline
Junior Poster in Training

Re: How do I read the registry?

 
0
  #7
Mar 7th, 2007
Originally Posted by Lazaro Claiborn View Post
Bernt.tc did you care to click the link I'd provided above? The page contains sample code. Here is an example nevertheless:
Yes I did read your link, but it provided a very unhelpful link, by this I mean, it gave an example, but no real explanation.
Your's is better.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 90
Reputation: Brent.tc is an unknown quantity at this point 
Solved Threads: 1
Brent.tc Brent.tc is offline Offline
Junior Poster in Training

Re: How do I read the registry?

 
0
  #8
Mar 8th, 2007
A new problem has arrived, the key I want to read has spaces and for some reason it won't read it with the spaces
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,642
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 471
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: How do I read the registry?

 
0
  #9
Mar 8th, 2007
You can convert the key you receive into a C++ string and then do something like:

  1. int main (void)
  2. {
  3. using namespace std ;
  4.  
  5. string key = "something \ with \ spaces ." ;
  6. for ( int i = 0, j ; i < key.length( ) ; ++ i )
  7. {
  8. if ( key [i] == ' ' )
  9. {
  10. for ( j = i + 1; j < key.length ( ) ; ++j )
  11. {
  12. if ( key [j] != ' ' )
  13. break ;
  14. }
  15.  
  16. key = key.erase ( i, (j - i) ) ;
  17. }
  18. }
  19.  
  20. cout << key ;
  21. getchar () ;
  22. return 0;
  23. }

Though its a bit crude, but I think should serve your purpose.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC