Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 1st, 2008, 2:11 PM   #1
KuraKai
Newbie
 
Join Date: Mar 2008
Posts: 25
Rep Power: 0 KuraKai is on a distinguished road
RegQueryValue Issues

I am trying to read from the registry and there is issues with trying to query it, it can get to where the value is located but it can't find the value for some odd reason. Here is the code I am currently running.

C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include "windows.h"
  4. #include "winnt.h"
  5. #include "winreg.h"
  6.  
  7. using namespace std;
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. HKEY MyKey;
  12.  
  13. char szBuff[80];
  14. LONG cb;
  15.  
  16. if (RegOpenKey(HKEY_CURRENT_USER, "Software\\Random\\Program", &MyKey) == ERROR_SUCCESS)
  17. {
  18. cout << "Program is installed\n";
  19. cb = sizeof(szBuff);
  20.  
  21. //Debug info, this normally isn't here!
  22. printf("%d",RegQueryValue(MyKey, "hexData", szBuff, &cb) );
  23. cout << '\n';
  24.  
  25. if(RegQueryValue(MyKey, "hexData, szBuff, &cb) == ERROR_SUCCESS)
  26. {
  27. cout << "Program has been run once\n";
  28. printf(szBuff);
  29. }
  30.  
  31. RegCloseKey(MyKey);
  32. }
  33.  
  34. system("PAUSE");
  35. return EXIT_SUCCESS;
  36. }
  37.  

The value that it should be printing out 0611623CA8EFE3D880FF72E0F9A5AACDE5D9C171EAE2C6FDB6888BF7B5386DEE , however it can't find it even though it should be there. Any help with this?
KuraKai is offline   Reply With Quote
Old Mar 1st, 2008, 4:58 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 530
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: RegQueryValue Issues

are you certain that's exactly the code you compiled? line 25 has syntax error -- you need to close the quote after "hexData.
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is offline   Reply With Quote
Old Mar 1st, 2008, 5:01 PM   #3
KuraKai
Newbie
 
Join Date: Mar 2008
Posts: 25
Rep Power: 0 KuraKai is on a distinguished road
Re: RegQueryValue Issues

Oops, but that isn't the exact code, I cut out the pieces that where for something else. (Mostly all the commented out parts of previous tries at getting it right.) But that is what i use for this specific part.
KuraKai is offline   Reply With Quote
Old Mar 1st, 2008, 5:05 PM   #4
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 530
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: RegQueryValue Issues

After fixing the quote error and adding that key/string value to my registry this is what you program prints
Quote:
Program is installed
2
Press any key to continue . . .
Seems to work for me, but maybe the registry entry on your computer isn't the same as you said it is in the program code.
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is offline   Reply With Quote
Old Mar 1st, 2008, 5:09 PM   #5
KuraKai
Newbie
 
Join Date: Mar 2008
Posts: 25
Rep Power: 0 KuraKai is on a distinguished road
Re: RegQueryValue Issues

No, if it was working properly it should find the registry and actually say
Quote:
Program is installed
1
Program has been run once
0611623CA8EFE3D880FF72E0F9A5AACDE5D9C171EAE2C6FDB6888BF7B5386DEE
Press any key to continue . . .
The 2 means there was an error when it tries to query for the value, if it found the value it should print it out also.
KuraKai is offline   Reply With Quote
Old Mar 1st, 2008, 5:28 PM   #6
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 530
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: RegQueryValue Issues

I changed your program to call RegQueryKeyEx() and it works now
cplusplus Syntax (Toggle Plain Text)
  1. int main(int argc, char *argv[])
  2. {
  3. HKEY MyKey = 0;
  4.  
  5. BYTE szBuff[255] = {0};
  6. DWORD cb = 0;
  7. DWORD dwType = REG_SZ;
  8. LONG rval;
  9. if (RegOpenKey(HKEY_CURRENT_USER, "Software\\Random\\Program", &MyKey) == ERROR_SUCCESS)
  10. {
  11. cout << "Program is installed\n";
  12. cb = sizeof(szBuff);
  13.  
  14. //Debug info, this normally isn't here!
  15.  
  16. if((rval = RegQueryValueEx(MyKey, "hexData", 0, &dwType, szBuff, &cb)) == ERROR_SUCCESS)
  17. {
  18. cout << "Program has been run once\n";
  19. printf((char *)szBuff);
  20. }
  21. printf("%d",rval);
  22. cout << '\n';
  23.  
  24. RegCloseKey(MyKey);
  25. }
  26.  
  27. system("PAUSE");
  28. return EXIT_SUCCESS;
  29. }
And the output is
Quote:
Program is installed
Program has been run once
0611623CA8EFE3D880FF72E0F9A5AACDE5D9C171EAE2C6FDB6888BF7B5386DEE0
Press any key to continue . . .
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is offline   Reply With Quote
Old Mar 1st, 2008, 5:36 PM   #7
KuraKai
Newbie
 
Join Date: Mar 2008
Posts: 25
Rep Power: 0 KuraKai is on a distinguished road
Re: RegQueryValue Issues

Alrighty thank you, for some odd reason that didn't work before for me...But also you didn't do a '\n' after printing out the registry value. And thanks again for the help.
KuraKai is offline   Reply With Quote
Old Mar 1st, 2008, 5:39 PM   #8
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 530
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: RegQueryValue Issues

The code as posted could use a little clearning up, such as don't mix both cout and printf(). I didn't bother with that, especially since you said this is only a small part of some larger program.
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is offline   Reply With Quote
Old Mar 1st, 2008, 6:48 PM   #9
KuraKai
Newbie
 
Join Date: Mar 2008
Posts: 25
Rep Power: 0 KuraKai is on a distinguished road
Re: RegQueryValue Issues

I have a quick question about the same topic, how would i modify this if i was going to query multiple values that are in the same location?
KuraKai is offline   Reply With Quote
Old Mar 1st, 2008, 7:01 PM   #10
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 530
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: RegQueryValue Issues

RegEnumVal(). There is an example program near the bottom of that web page.
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
LWP form Data array issues Johnmcter Perl 0 Feb 13th, 2008 2:21 PM
Intermittent issues blud Community Announcements and Feedback 3 Nov 2nd, 2007 8:46 AM
Table issues... Glastea HTML / XHTML / CSS 2 Jan 12th, 2007 1:54 AM
I have an idea, but copyright issues. Booooze Project Ideas 25 Aug 3rd, 2006 9:31 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC