Thread: Reading Registry Values

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Question Reading Registry Values

    hi All,

    I have a function that reads string values from the registry:

    Code:
    char *readregistry(char keyname[50], char keyvalue[20])
    {
    	BOOL ret, success;
    	HKEY keyhandle;
    	DWORD dwBufLen = 32;
    	char myregval[100];
    	char *str= (char *)malloc(100);
    
    	if (str == NULL)
    	{	cout << "Error allocating memory";
    		exit(1);
    	}
    
    	ret=RegOpenKeyEx(HKEY_LOCAL_MACHINE,keyname,0,KEY_READ,&keyhandle);
    
    	if (ret != ERROR_SUCCESS)
    	{	
    		strcpy(str, "NONE");
    		return str;
    	}
    	else
    	{
    		success= RegQueryValueEx(keyhandle, keyvalue, NULL, NULL, (LPBYTE)myregval, &dwBufLen);
    		if (success != ERROR_SUCCESS) {
    			strcpy(str, "NONE");
    			return str;
    		}
    		else
    		{	strcpy(str, myregval);
    			return str;
    		}
    	}
    
    }
    This normally works fine however if I am trying to read a unc network path from the registry some times it fails and returns 'NONE'.

    For instance it cannot read this value:

    \\computername\share\directory\subdirectory\

    But it can read this:

    \\computername\share\directory\

    Any thoughts as to how to get around this problem?

    Thanks in advance..
    Aaron

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You are passing a buffer size of 32. The string is more than 32 characters. You will get an error, ERROR_MORE_DATA.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    Thank you anonytmouse.. That did it.. I upped the buffer to 255 and it works fine now.

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading in a text file containing hex values
    By gaza2k1 in forum C Programming
    Replies: 34
    Last Post: 02-29-2008, 07:15 PM
  2. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  3. Reading values into an array
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 11-25-2007, 10:59 AM
  4. reading values from a file
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-25-2007, 02:08 AM
  5. Reading values from a string table..
    By Dual-Catfish in forum Windows Programming
    Replies: 1
    Last Post: 06-28-2002, 05:22 PM