944,123 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 36393
  • C++ RSS
Mar 6th, 2007
0

How do I read the registry?

Expand Post »
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++.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Brent.tc is offline Offline
90 posts
since Oct 2006
Mar 6th, 2007
0

Re: How do I read the registry?

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Mar 7th, 2007
0

Re: How do I read the registry?

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
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
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 7th, 2007
0

Re: How do I read the registry?

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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Brent.tc is offline Offline
90 posts
since Oct 2006
Mar 7th, 2007
0

Re: How do I read the registry?

Click to Expand / Collapse  Quote originally posted by Brent.tc ...
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.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 7th, 2007
0

Re: How do I read the registry?

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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Mar 7th, 2007
0

Re: How do I read the registry?

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Brent.tc is offline Offline
90 posts
since Oct 2006
Mar 8th, 2007
0

Re: How do I read the registry?

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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Brent.tc is offline Offline
90 posts
since Oct 2006
Mar 8th, 2007
0

Re: How do I read the registry?

You can convert the key you receive into a C++ string and then do something like:

C++ Syntax (Toggle Plain Text)
  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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help w/ implementation file please
Next Thread in C++ Forum Timeline: two array comparison





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC