Registry rekursiv durchsuchen



  • Guten Tag die Herren.

    Ich versuche gerade ein Programm zu schreiben, dass die Registry rekursiv durchsucht. Das tut es soweit auch. Allergings hab ich irgendwo noch ein Fehler drin, und ich komm einfach ums Verrecken nicht drauf wo der Fehler liegt. Ich such jetzt schon seit Tagen danach. Ich hoffe deshalb ihr habt ein geschulteres Auge dafür.

    Also hier mal ein wenig code:

    bool CKlasse::StartSearching()
    {
    
    	strncpy(szSearchPath, "HKEY_LOCAL_MACHINE", sizeof(szSearchPath)-1);
    
    	HKEY hTestKey;
    	if( RegOpenKeyEx( ROOTKEY, TEXT(""), 0, KEY_READ, &hTestKey) == ERROR_SUCCESS )
    	{
    			SearchKeys(hTestKey);
    			RegCloseKey(hTestKey);
    	}
            return true;
    }
    
    void CKlasse::SearchKeys(HKEY hKey)
    {
    	TCHAR    achKey[MAX_KEY_LENGTH];		// buffer for subkey name
        DWORD    cbName;						// size of name string 
        TCHAR    achClass[MAX_PATH] = _T("");	// buffer for class name 
        DWORD    cchClassName = MAX_PATH;		// size of class string 
        DWORD    cSubKeys=0;					// number of subkeys 
        DWORD    cbMaxSubKey;					// longest subkey size 
        DWORD    cchMaxClass;					// longest class string 
        DWORD    cValues;						// number of values for key 
        DWORD    cchMaxValue;					// longest value name 
        DWORD    cbMaxValueData;				// longest value data 
        DWORD    cbSecurityDescriptor;			// size of security descriptor 
        FILETIME ftLastWriteTime;				// last write time 
    
    	TCHAR	szErr[128];						// TODO: wird das noch 
    
    benötigt?
    
    	int ret = 0;
    
    	BYTE  lpData[MAX_VALUE_NAME];
    	DWORD lpcbData, valType;
    
        DWORD i = 0, 
    		 ii = 0, 
    		 retCode;
    
    	TCHAR achValue[MAX_VALUE_NAME];    
        DWORD cchValue = MAX_VALUE_NAME; 
    
    	HKEY hTestKey;
    
        // Get the class name and the value count. 
        retCode = RegQueryInfoKey(
            hKey,                    // key handle 
            achClass,                // buffer for class name 
            &cchClassName,           // size of class string 
            NULL,                    // reserved 
            &cSubKeys,               // number of subkeys 
            &cbMaxSubKey,            // longest subkey size 
            &cchMaxClass,            // longest class string 
            &cValues,                // number of values for this key 
            &cchMaxValue,            // longest value name 
            &cbMaxValueData,         // longest value data 
            &cbSecurityDescriptor,   // security descriptor 
            &ftLastWriteTime);       // last write time 
    
    	// If there are values in the key, read them
    	if(cValues)
    	{
            cchValue = MAX_VALUE_NAME; 
    		lpcbData = MAX_VALUE_NAME;
    
            achValue[0] = '\0'; 
    		lpData[0] = '\0';
    		valType = 0;
    
    		for (i=0; i<cValues; i++)
    		{
                RegEnumValue(	hKey, i, 
    							achValue, 
    							&cchValue, 
    							NULL, 
    							&valType,		// typ of value (1 = 
    
    char)
    							lpData,
    							&lpcbData);
    
    //			TRACE("(%d) %s - %s\n", b, path, achValue);
    		}
    		// b--;
    	}
    
        // Enumerate the subkeys, until RegEnumKeyEx fails.
        if (cSubKeys)
    	{	
            for (i=0; i<cSubKeys; i++)
    		{ 
                cbName = MAX_KEY_LENGTH;
                retCode = RegEnumKeyEx(hKey, i,
    					 achKey, 
                         &cbName, 
                         NULL, 
                         NULL, 
                         NULL, 
                         &ftLastWriteTime); 
    
                if ( retCode == ERROR_SUCCESS )
    			{
    				path += "\\";
    				path += achKey;
    
    				ret = RegOpenKeyEx( hKey, achKey, 0, KEY_READ, &hTestKey );
    				if( ret == ERROR_SUCCESS )
    				{
    					SearchKeys( hTestKey );
    					RegCloseKey( hTestKey );
    				}
    				else
    				{
    					FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | 
    
    FORMAT_MESSAGE_ARGUMENT_ARRAY, NULL, ret, 0, szErr, sizeof(szErr), NULL); 
    					strncat(szErr, achKey, sizeof(szErr)-1);
    					strncat(szErr, "\n\n", sizeof(szErr)-1);
    					TRACE(szErr);
    					path = "";
    					RegCloseKey( hTestKey );
    				}
    			}
            }
        }
    
    	if(cSubKeys <= 0)
    	{
    
    		int tmpPathLength = tmpPath.GetLength();
    		int tmpB = b;
    
    		for(int ff = tmpPathLength; ff >= 0; ff--)
    		{
    			if(tmpPath.GetAt(ff) == '\\')
    			{
    				if(tmpB > 0)
    				{
    					tmpPath.Delete(ff, tmpPathLength);
    				}
    				tmpB--;
    			}
    		}
    
    		tmpPath += path;
    
    		//TODO: search option must be placed here
    		//      (code example see below..)
    		CString s(tmpPath);
    		CString ss("test");			// search string
    
    		s.MakeLower();
    		ss.MakeLower();
    
    		if(s.Find(ss) >= 0)
    		{
    			TRACE("FOUND: %s\n", tmpPath);
    
    			strncpy(szSearchPath, "HKEY_LOCAL_MACHINE", sizeof(szSearchPath)-1);
    			strncat(szSearchPath, tmpPath, sizeof(szSearchPath)-1);
    
    			REGKEY *newKey = NULL;
    			newKey = addElement(&startPtr);
    
    			strncpy(newKey->szPath, szSearchPath, strlen(szSearchPath));
    			newKey->szType = 'K';
    
    			memset(szSearchPath, 0, sizeof(szSearchPath));
    		}
    		path.Delete(0, path.GetLength());
    
    		b = 0;
    	}
    	b++;
    }
    

    Das Problem ist, dass wenn ich z.B. eine solche Ausgabe bekomme:

    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\test
    HKEY_LOCAL_MACHINE\test
    HKEY_LOCAL_MACHINE\CurrentControlSet\test

    wobei der erste Key noch richtig ist. Also der Pfad stimmt. Bei den nächsten 2 fehlen dann aber schon Unterordner.
    zb: "KEY_LOCAL_MACHINE\CurrentControlSet\test" müsste "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\test" heißen.

    Vielen dank schonmal.



  • Nur so als Hinweis:

    RegEnumKeyEx should return all keys, shouldn´t it?
    http://blog.kalmbachnet.de/?postid=41

    The registry becomes more and more complex...
    http://blog.kalmbachnet.de/?postid=99



  • Hi,

    dass die Registry inzwischen ein rießen Ramschladen ist, was nicht mal mehr MS richtig durchschaut war mir schon klar. Allerdings kann Registry Reflection bei mir nicht die Ursache sein, da ich ja eine 32Bit Version von XP habe.

    Müsste irgendein Codefehler sein. Leider ist das alles sehr komplex wegen der Rekursion. Deshalb hab ich auch Debuggingprobleme.

    Aber schonmal vielen Dank!


Anmelden zum Antworten