Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegOpenKeyEx returning Error 87 aka "INVALID_PARAMETER"

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\Products\SomeKey

Error Code:: 87 (INVALID_PARAMETER)

Weird Behaviour:: Deletes the key the moment I open the key using REGEDIT.

Code::

static BOOL 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_ENUMERATE_SUB_KEYS | DELETE, &hKeySub ) ;
    printf("RegOpenKey:: %S :: lRet = %ld\n", lpszSub, lRet) ;
    if( lRet != ERROR_SUCCESS )
    {
        if( lRet == ERROR_FILE_NOT_FOUND )
        {
            bRet = TRUE ;
            break ;
        }
        else
        {
            bRet = FALSE ;
            break ;
        }
    }

    while( ERROR_NO_MORE_ITEMS != (lRet = RegEnumKeyEx(hKeySub, 0, szName, &dwSize, NULL, NULL, NULL, NULL)) )
    {
        bRet = RcrsvRegDel( hKeySub, szName) ;
        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 goin on?

UPDATE::

I have also tried the samDesired Parameter with following flags

-KEY_READ

-KEY_READ | KEY_WRITE

-KEY_ENUMERATE_SUB_KEYS

-KEY_ENUMERATE_SUB_KEYS | DELETE

Neither of the above flag works :-(


1 Answers

Because you can not use RegDeleteKey with handle was opened with flag KEY_WOW64_32KEY. see http://msdn.microsoft.com/en-us/library/aa384129(v=vs.85).aspx for info. You have to use RegDeleteKeyEx with same keys.

like image 97
Serge Z Avatar answered Aug 02 '26 13:08

Serge Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!