Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,
I have written a code to recursively delete the registry keys from HKLM and HKCR. While deleting the HKLM keys pose no problem but HKCR keys exit with the error code 87 i.e., INVALID_PARAMETER.
Any idea on what's going on?

My Code Snippet::
hKey = Predefined Root Key, lpszSub = SubKey To be Deleted
Works fine on: Vista and Win7 but throws Error on XP x86
C++
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_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 ;
}
Posted
Updated 24-Sep-12 23:53pm
v5

After the first RegDeleteKey() you are closing the handle if deletion was successful. But you did not clear the hKey variable which is then used later to close the key again which fails:
C++
if( lRet == ERROR_SUCCESS )
{
    bRet = TRUE ;
    RegCloseKey(hKey) ;
    // This line is missing in your code:
    hKey = NULL ;
    break ;
}
 
Share this answer
 
Comments
Espen Harlinn 18-Sep-12 10:40am    
That seems to be the problem :-D
Abhineet Ayan Verma 20-Sep-12 7:19am    
@Jochen- Are you serious???? First of all, the question is entirely different. Secondly, I have already mentioned in my question that "hKey is the HANDLE to PREDEFINED ROOT KEY such as HKCR/HKLM/etc" and as quoted in MSDN - "http://msdn.microsoft.com/en-us/library/windows/desktop/ms724897(v=vs.85).aspx" ( See the <phkResult> ), I do not think the hKey closure would cause anything like this in my code.
Jochen Arndt 20-Sep-12 8:15am    
It's often more difficult to find errors by just looking on code than having it inside the dev environment where you can run it through the debugger.

I missed that you mention that the key is a predefined one. But then you don't need to close it at three points in your code. Perhaps your intention is that the function may be also called with opened keys. Then my solution would at least fix a bug that may occur in the future when passing an opened key.
Abhineet Ayan Verma 20-Sep-12 7:22am    
Anyways I have checked with the solution you provided and it is still same. Also, sometimes the key is there and the "lRet" returns ERROR_FILE_NOT_FOUND but when I open "REGEDIT" and open the respective key, the code finds the key and deletes it successfully. Any ideas????
Jochen Arndt 20-Sep-12 8:18am    
At first you should identify which call fails on which key/subkey/value.
Just add some TRACE() commands to your code into the failure blocks.
Trace also all arguments of the call.

Alternatively set breakpoints or add ASSERTs and run within the debugger.

Error 87 source are sometimes hard to debug. There are many sources for this error code.

Additional helpful information would be if your app is Unicode and if you use a 64-Bit Windows.
Seems like you are trying to do what this function does for you:
RegDeleteTree[^]

Deletes the subkeys and values of the specified key recursively

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Abhineet Ayan Verma 20-Sep-12 7:43am    
Yeah, I know the API but I want to do it recursively myself. Its always nice to see your own code working good rather than any API :-)
Espen Harlinn 20-Sep-12 7:44am    
Fair enough :-D
Check the value of
C++
hKeySub
 
Share this answer
 
v2
Comments
Abhineet Ayan Verma 26-Sep-12 1:05am    
Yeah I have checked...its 0 (NULL).
Michael Haephrati 21-Sep-17 15:50pm    
That means it fails

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900