Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   RegQueryValue Issues (http://www.programmingforums.org/showthread.php?t=15296)

KuraKai Mar 1st, 2008 3:11 PM

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.

:

  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?

Ancient Dragon Mar 1st, 2008 5:58 PM

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.

KuraKai Mar 1st, 2008 6:01 PM

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.

Ancient Dragon Mar 1st, 2008 6:05 PM

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.

KuraKai Mar 1st, 2008 6:09 PM

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.

Ancient Dragon Mar 1st, 2008 6:28 PM

Re: RegQueryValue Issues
 
I changed your program to call RegQueryKeyEx() and it works now
:

  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 . . .

KuraKai Mar 1st, 2008 6:36 PM

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.

Ancient Dragon Mar 1st, 2008 6:39 PM

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.

KuraKai Mar 1st, 2008 7:48 PM

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?

Ancient Dragon Mar 1st, 2008 8:01 PM

Re: RegQueryValue Issues
 
RegEnumVal(). There is an example program near the bottom of that web page.


All times are GMT -5. The time now is 4:10 AM.

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