Hi All,
I am implementing a recursive registry delete using RegOpenKeyEx, RegDeleteKey and RegEnumKey.
Problem:: Though the code works perfectly fine for Vista x86/x64 and Win 7 x86/x64 but fails on XP for some keys in HKCR
Problem Area:: HKCR\Installer\SomeKey
Error Code:: 87 (INVALID_PARAMETER)
Weird Behaviour:: Deletes the key the moment I open the key using REGEDIT.
Code::

static BOOL K7RT_RcrsvRegDel( HKEY hKey, LPTSTR lpszSub )
{
    BOOL    bRet = TRUE ;
    LONG    lRet ;
    DWORD   dwSize = MAX_PATH ;
    TCHAR   szName[MAX_PATH] ;
    TCHAR   szFullKey[MAX_PATH * 2] ;
    HKEY    hKeySub = NULL ;
    HRESULT hr = NULL ;

    do{
        lRet = RegOpenKeyEx( hKey, lpszSub, 0, KEY_ALL_ACCESS |KEY_WOW64_32KEY , &hKeySub ) ;
        printf("RegOpenKey:: %S :: lRet = %ld\n", lpszSub, lRet) ;
        if( lRet != ERROR_SUCCESS )
        {
            if( lRet == ERROR_FILE_NOT_FOUND )
            {
                bRet = FALSE ;
                break ;
            }
            else
            {
                bRet = FALSE ;
                break ;
            }
        }

        while( ERROR_NO_MORE_ITEMS != (lRet = RegEnumKeyEx(hKeySub, 0, szName, &dwSize, NULL, NULL, NULL, NULL)) )
        {
            hr = StringCchPrintf( szFullKey, MAX_PATH*2, TEXT("%s\\\\%s\0"), lpszSub, szName ) ;
            if( hr != S_OK )
            {
                bRet = FALSE ;
                break ;
            }
            szName[0] = '\0' ;
            dwSize = MAX_PATH ;

            bRet = K7RT_RcrsvRegDel( hKey, szFullKey ) ;
            if( bRet == FALSE )
                break ;
        }

        if( hKeySub != NULL )
        {
            RegCloseKey(hKeySub) ;
            hKeySub = NULL ;
        }

        lRet = RegDeleteKey( hKey, lpszSub ) ;
        printf("RegDelKey:: %S :: lRet = %ld\n", lpszSub, lRet) ;
        if( lRet == ERROR_SUCCESS )
        {
            bRet = TRUE ;
            break ;
        }
    }while(0) ;
    return bRet ;
}

Any idea whats going wrong here.

Question for you.. Are you sure the following is correct?

        if( lRet != ERROR_SUCCESS )
        {
            if( lRet == ERROR_FILE_NOT_FOUND )
            {
                bRet = FALSE ;
                break ;
            }
            else
            {
                bRet = FALSE ;         //Are you sure? :S
                break ;
            }
        }

The reason I'm asking is because it's never true throughout that entire block :S
If ....... False else... False.. Thus there is no reason for the if statement correct?

ALSO just wanted to say that KEY_ALL_ACCESS |KEY_WOW64_32KEY Could be the problem. Why?

After the application has accessed an alternate registry view using one of the flags, all subsequent operations (create, delete, or open) on child registry keys must explicitly use the same flag. Otherwise, there can be unexpected behavior.

You seem to specify such a flag for opening only.. Thus the rest of stuff is undefined behaviour since you didn't explicity supply the same flag.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129(v=vs.85).aspx

That explains your flags and undefined behaviour but on top of that, it states that you should enumerate twice before opening.

Hopefully this solves your problems. Then again I could be wrong and you may have to wait on someone else's reply but I tried since I saw you waiting 17 hrs for one reply :P

Edited 4 Years Ago by triumphost

Thanks for trying. That if-else condition is My Bad. And again, RegOpenKeyEx is returning the INVALID_PARAMETER ERROR. My first problem is atleast opening the key.

This article has been dead for over six months. Start a new discussion instead.