开发者

How to delete from the 64 bit registry under XP?

开发者 https://www.devze.com 2023-04-09 14:25 出处:网络
I\'m making an application, and i need to write some stuff in the registry, and later edit them if neccessary. I\'m writing to the 64 bit registry using KEY_WOW64_64KEY. I created my Key Software\\MyA

I'm making an application, and i need to write some stuff in the registry, and later edit them if neccessary. I'm writing to the 64 bit registry using KEY_WOW64_64KEY. I created my Key Software\MyApp and here some other values 5 or 6. My problem is the following. I have the following code to read every values, under a key

void ReadAndDeleteValues( HKEY hKey )
{

//TCHAR    achKey[ MAX_KEY_LENGTH ];        // buffer for subkey name
//DWORD    cbName;                          // size of name string 
TCHAR    achClass[ MAX_PATH ] = TEXT("");   // 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 

DWORD i, retCode; 

TCHAR  achValue[ MAX_VALUE_NAME ]; 
DWORD cchValue = MAX_VALUE_NAME; 

// 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 ( cValues > 0 ) printf( "\nNumber of values: %d\n", cValues );

for ( i = 0, retCode = ERROR_SUCCESS; i < cValues; i++ ) 
{ 
    cchValue = MAX_VALUE_NAME; 
    achValue[ 0 ] = '\0'; 

    retCode = RegEnumValue( hKey, 
                            i, 
                            achValue, 
                            &cchValue, 
                            NULL, 
                            NULL,
                            NULL,
                            NULL
                          );

    if ( retCode == ERROR_SUCCESS ) 
    { 

        DWORD cbData = 8192;
        DWORD dwRet;
        DWORD type = 0;
        wchar_t PerfData[ 2048 ] = { 0 };

        memset( PerfData, 0, wcslen( PerfData ) );

        dwRet = RegQueryValueEx( hKey,
                                 achValue,
                                 NULL,
                                 &type,
                                 ( LPBYTE )PerfData,
                                 &cbData 
                               );

        if ( dwRet == ERROR_SUCCESS ) ;//do nothing
        else printf( "\n\nRegQueryValueEx Failed!" );

        _tprintf( TEXT( "\n  #%.3d - [ %-30s ]" ), i + 1, achValue ); 

        RegDeleteValue( hKey, achValue );

    }//if
}//for

}//ReadValues

It works fine, so i thought, i just place RegDeleteValue there and every Value will be deleted. Unfortunately this is not what's happening. This API will delete only 2-3 values, then returns. If i run it again, then it will delete 2-3 values again and returns again, but i don't know why???? Theoratically if i a find a vale, i can delete, so i don't understand, why is this happening.

Could someone help me correct my code?

Thanks!


Your program deletes only a few values because of the classic 'deleting from array' mistake, like in this pseudocode:

// this program will not remove all elements
for (int i = 0, n = arraySize; i < n; ++i)
    array_remove(array, i);

// step 1, i=0: 1 2 3 4 5 6
//              ^ removed
// step 2, i=1: 2 3 4 5 6
//                ^ removed
// step 3, i=2: 2 4 5 6
//                  ^ removed
// step 4, i=3: 2 4 6
//                    ^ RegEnumValue returns error and the loop exits

A correct way will be something like:

while (cValues > 0) {
    /* delete registry value at index 0 */
    --cValues;
}

To quickly fix your code, replace the second parameter of RegEnumValue() with 0.

0

精彩评论

暂无评论...
验证码 换一张
取 消