#1
  1. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12

    RegQueryValue problems


    I'm having problems getting my code to read a value from the registry to work correctly. It'll comnpile just fine (I'm using VC++7.0), and seems to open the key correctly, but doesn't want to read the value :confused: ... What am I doing wrong?

    Code:
    	if(RegOpenKey(HKEY_CURRENT_USER, "Software\\WinImage", &theApp.m_hKey) == ERROR_SUCCESS)
    	{
    		MessageBox(NULL, "RegOpenKey worked!", ":-)", MB_OK);
    		if(RegQueryValue(theApp.m_hKey, "DefaultViewer", lpValue, lpcbValue) == ERROR_SUCCESS)
    			MessageBox(NULL, "RegQueryValue worked!", ":-)", MB_OK);
    		else
    			MessageBox(NULL, "RegQueryValue didn't work!", ":'(", MB_OK);
    	}
    	else
    		MessageBox(NULL, "RegOpenKey didn't work!", ":'(", MB_OK);
    Thanks for any help!
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Two questions for you here.
    (a) Is lpcbValue actually a pointer to long or what. If it is a variable, you should be passing it as &lpcbValue.
    (b) Have you set the value of lpcbValue to something.

    See if something like this will work:
    Code:
    if(RegOpenKey(HKEY_CURRENT_USER, "Software\\WinImage", &theApp.m_hKey) == ERROR_SUCCESS)
    	{
    		MessageBox(NULL, "RegOpenKey worked!", ":-)", MB_OK);
    
    		TCHAR szValue[255]
    		LONG cbValue = 255;
    		if(RegQueryValue(theApp.m_hKey, "DefaultViewer", szValue, &cbValue) == ERROR_SUCCESS)
    
    			MessageBox(NULL, "RegQueryValue worked!", ":-)", MB_OK);
    		else
    			MessageBox(NULL, "RegQueryValue didn't work!", ":'(", MB_OK);
    	}
    	else
    		MessageBox(NULL, "RegOpenKey didn't work!", ":'(", MB_OK);
    Hope this helps :)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12
    Thanks for the help! But it still doesnt work (same thing). :( I've tried it with a couple other keys/values as well. Any ideas?
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.
  6. #4
  7. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12
    Ok using this console app:

    Code:
    #include <iostream.h>	
    #include <windows.h>
    
    
    void main()
    {
    	HKEY hTheKey;
    	TCHAR szValue[255];
    	LONG cbValue = 255;
    	LONG lResult = 0;
    
    	RegOpenKey(HKEY_CURRENT_USER, "Software\\WinImage", &hTheKey);
    	if(lResult == ERROR_SUCCESS)
    	{
    		cout << "RegOpenKey worked!" << endl;
    		lResult = RegQueryValue(hTheKey, "DefaultViewer", szValue, &cbValue);
    		if(lResult == ERROR_SUCCESS)
    			cout << "RegQueryValue worked!" << lResult << endl;
    		else
    			cout << "RegQueryValue didn't work! lResult=" << lResult << endl;
    	}
    	else
    		cout << "RegOpenKey didn't work! lResult=" << lResult << endl;
    }
    and it said the openkey worked, but the queryvalue failed and return '2'...
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.
  8. #5
  9. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12
    Ok, I looked in winerror.h, and I believe that a return value of 2 means that it couldn't find the value/key. So, am I doing the openkey wrong, or is it somthing else?
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.
  10. #6
  11. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12
    Ok, I found an example on the net and managed to use it to get this working. Here's the codE:

    Code:
    #include <iostream.h>	
    #include <windows.h>
    
    
    void main()
    {
    	HKEY hTheKey;
    	HKEY hTheSubKey;
    	LONG lResult = 0;
    	char *szData;
    	DWORD dwType, dwLen;
        szData = new char[101];
    	dwLen = 100;
      
    	RegOpenKey(HKEY_CURRENT_USER, "Software", &hTheKey);
    	RegOpenKey(hTheKey, "WinImage", &hTheSubKey);
    	if(lResult == ERROR_SUCCESS)
    	{
    		cout << "RegOpenKey worked!" << endl;
    		lResult = RegQueryValueEx(hTheSubKey, "DefaultViewer", NULL, &dwType, (unsigned char *)szData, &dwLen);
    		if(lResult == ERROR_SUCCESS)
    			cout << "RegQueryValue worked!        " << szData << endl;
    		else
    			cout << "RegQueryValue didn't work! (" << szData << ") lResult=" << lResult << endl;
    	}
    	else
    		cout << "RegOpenKey didn't work! lResult=" << lResult << endl;
    }
    It works great. Now, can anyone tell me how this is right and what I had before is wrong? Thanks.
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.

IMN logo majestic logo threadwatch logo seochat tools logo