Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Windows MFC
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
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 18-Sep-12 5:05am
Edited 25-Sep-12 0:53am
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
if( lRet == ERROR_SUCCESS )
{
    bRet = TRUE ;
    RegCloseKey(hKey) ;
    // This line is missing in your code:
    hKey = NULL ;
    break ;
}
  Permalink  
Comments
Espen Harlinn at 18-Sep-12 10:40am
   
That seems to be the problem :-D
abhineet.ayan.verma at 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 at 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 at 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 at 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.
abhineet.ayan.verma at 20-Sep-12 8:33am
   
I meant no disrespect if I have caused any. Closing the PREDEFINED was my bad which I have already corrected in my current code. The main problem is:
1. The code works fine with Vista x86 and Win7 x64 but fails on XP x86 ( havent' checked on x64).
2. Works good when deleting in HKLM with keys having more subkeys but fails with ERROR 87 when hKey carries HKCR.
 
I have cross-checked the NULL-TERMINATION in lpszSub and closure of SubKey Handle but still it fails. I have no clue why this is happening. This is weird.
Jochen Arndt at 20-Sep-12 9:02am
   
So it fails with XP but not with Vista and 7?
 
You may edit your question using the 'Improve question' link to add this information with an indication (e.g. [UPDATE]: New information). This will bring your question on top again increasing the chance that others may help.
 
There is one point that may have side effects:
You are enumerating keys and delete them recursively. At least I would close and re-open the key (close it before the recursive call and open it again afterwards). This ensures that you did not get any side effects when deleting subkeys of an opened key that is used for enumeration.
abhineet.ayan.verma at 20-Sep-12 9:17am
   
Thanks mate...Appreciated
abhineet.ayan.verma at 20-Sep-12 9:21am
   
I have updated the question along with your anti-side effect advice :-) Is that what you suggested?
Jochen Arndt at 20-Sep-12 9:31am
   
I suggested to add the information about the OS version. However, reflecting the actually used code by editing the question is also a good idea.
 
But you missed the re-opening of the hKeySub key. So add it and test the code. I'm not sure if it helps. But I think it's worth a try.
 
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
abhineet.ayan.verma at 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 at 20-Sep-12 7:44am
   
Fair enough :-D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Check the value of
hKeySub
  Permalink  
v2
Comments
abhineet.ayan.verma at 26-Sep-12 1:05am
   
Yeah I have checked...its 0 (NULL).

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

  Print Answers RSS
0 ProgramFOX 205
1 Sergey Alexandrovich Kryukov 191
2 OriginalGriff 188
3 Peter Leow 150
4 Maciej Los 145
0 OriginalGriff 213
1 ProgramFOX 205
2 Sergey Alexandrovich Kryukov 183
3 Peter Leow 150
4 Dave Kreskowiak 101


Advertise | Privacy | Mobile
Web01 | 2.8.150301.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100