Thread: RegEnumValue: data types?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    69

    RegEnumValue: data types?

    i'm having trouble reading the value data from the values that i'm looking at. they show up as ascii symbols instead of the data that they should represent, or in the case of strings, they just show the first character of the string.

    Code:
    #include <Windows.h>
    #include <iostream>
    #include <string>
    using namespace std;
    
    void CopyAllValues(HKEY subkey, char* path);
    
    const int valueSize=16383;
    const int keySize=255;
    
    #define DEBUG
    #define INITIALIZE
    
    int main()
    {
    	DWORD status=0;
    	char* path="SYSTEM\\CurrentControlSet\\Services";
    //	HKEY hsubkey;
    
    //	status=RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hsubkey);
    
    //	if (status==ERROR_SUCCESS)
    //	{
    		CopyAllValues(HKEY_LOCAL_MACHINE, path);
    //	}
    
    	return 0;
    }
    
    void CopyAllValues(HKEY subkey, char* path)
    {
    	HKEY hsubkey;
    	char* conString;
    	DWORD openResult=0;
    	//value vars
    	DWORD i, j;
    	TCHAR valueName[valueSize];
    	DWORD valueNameSize=0;
    	DWORD type=0;
    	BYTE data=0;
    	DWORD dataSize=0;
    	DWORD valueResult=0;
    
    	//key vars
    	TCHAR keyName[keySize];
    	DWORD keyNameSize=keySize;
    	TCHAR Class='\0';
    	DWORD ClassSize=0;
    	FILETIME LastWriteTime;
    	DWORD keyResult=0;
    //	bool used;
    
    
    	openResult=RegOpenKeyEx(subkey, path, 0, KEY_READ, &hsubkey);
    
    	if (openResult==ERROR_SUCCESS)
    	{
    #ifdef INITIALIZE
    		//initializing the names to null characters
    		memset(valueName, '\0', valueSize);
    //		for (int k=0;k<valueSize;k++)
    //		{
    //			valueName[k]='\0';
    //		}
    		memset(keyName, '\0', keySize);
    //		for (int m=0;m<keySize;m++)
    //		{
    //			keyName[m]='\0';
    //		}
    #endif
    
    		for (j=0, keyResult=ERROR_SUCCESS; keyResult==ERROR_SUCCESS; j++)
    		{
    	
    			keyResult=RegEnumKeyEx(hsubkey,
    				j,
    				keyName,
    				&keyNameSize,
    				NULL,
    				NULL,
    				NULL,
    				&LastWriteTime);
    		
    			//allocating memory for conString:
    			conString = (char *)calloc(strlen(path) + strlen(keyName) + 2, sizeof(char));
    			//concatenating the string to form the new path:
    			strcat(conString, path);
    			strcat(conString, "\\");
    			strcat(conString, keyName);
    		
    #ifdef DEBUG
    			//outputting the complete path and key name:
    			cout<<"Complete path is: " <<conString <<endl;
    //			cout<<"key name is: " <<keyName <<endl;
    #endif
    
    			if (keyResult==ERROR_SUCCESS && openResult==ERROR_SUCCESS)// && keyResult!=ERROR_NO_MORE_ITEMS)
    			{
    				CopyAllValues(subkey, conString);
    				//stop enumerating after using the current subkey?
    //				used=true;
    			}
    //			if (used)
    //			{
    //				break;
    //			}
    
    		}
    
    		for (i=0, valueResult=ERROR_SUCCESS; valueResult==ERROR_SUCCESS; i++)
    		{
    			dataSize=valueSize;
    			valueNameSize=valueSize;
    			RegEnumValue(hsubkey, i, valueName, NULL, NULL, &type, NULL, NULL);
    			valueResult=RegEnumValue(hsubkey,
    				i,
    				valueName,
    				&valueNameSize,
    				NULL,
    				&type,
    				&data,
    				&dataSize);
    
    #ifdef DEBUG
    			//outputting value name:
    			switch (type)
    			{
    
    				case 0:
    					cout<<valueName <<"		REG_NONE	" <<data <<endl;
    					break;
    				case 1:
    					cout<<valueName <<"		REG_SZ	" <<data <<endl;
    					break;
    				case 2:
    					cout<<valueName <<"		REG_EXPAND_SZ	" <<data <<endl;
    					break;
    				case 3:
    					cout<<valueName <<"		REG_BINARY	" <<data <<endl;
    					break;
    				case 4:
    					cout<<valueName <<"		REG_DWORD	" <<data <<endl;
    					break;
    				case 5:
    					cout<<valueName <<"		REG_DWORD_BIG_ENDIAN	" <<data <<endl;
    					break;
    				case 6:
    					cout<<valueName <<"		REG_LINK	" <<data <<endl;
    					break;
    				case 7:
    					cout<<valueName <<"		REG_MULTI_SZ	" <<data <<endl;
    					break;
    				case 8:
    					cout<<valueName <<"		REG_RESOURCE_LIST	" <<data <<endl;
    					break;
    				case 9:
    					cout<<valueName <<"		REG_FULL_RESOURCE_DESCRIPTOR	" <<data <<endl;
    					break;
    				case 10:
    					cout<<valueName <<"		REG_RESOURCE_REQUIREMENTS_LIST	" <<data <<endl;
    					break;
    				case 11:
    					cout<<valueName <<"		REG_QWORD	" <<data <<endl;
    					break;
    			}
    //			cout<<"value name is: " <<valueName <<"=" <<data <<endl;
    #endif
    
    		}	
    		RegCloseKey(hsubkey);
    	}
    }

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    The reason being is that you have not declared a size for your buffer. Therefore, the RegEnumValue function will be writing to memory which has not been allocated for it, and could possibly lead to stack corruption. Allocate the buffer to whatever size is required, i.e.

    BYTE data[valueSize]; and in your function call dont pass the address of the ptr but just pass the ptr.

    valueResult=RegEnumValue(hsubkey, i, valueName, &valueNameSize, NULL, &type, data,
    &dataSize);

    Hope this helps a bit, btw you probably might not need a buffer that size or declared locally on the statck. But i haven't really had a proper look at that reg function.
    Be a leader and not a follower.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    69
    woo, that fixed up the problem with the strings. now i just need to know how to interpret the DWORD's and BINARY values

    heh, the DWORD only needs a cast to display
    Last edited by talz13; 06-28-2004 at 12:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM