日期:2014-05-17  浏览次数:21446 次

删除注册表HKEY_CURRENT_USER下的SOFTWARE\\Microsoft\\Internet Explorer\\SearchScopes\\
当我使用RegDeleteKey这个函数时,我只是想删除SearchScopes下的子键,但是出乎我意料的是这个函数把我earchScopes也给删除了,谁能解释一下
C/C++ code
DeleteReglist.h
BOOL RegDelnodeRecurse (HKEY hKeyRoot, LPTSTR lpSubKey)
{
    LPTSTR lpEnd;
    LONG lResult;
    DWORD dwSize;
    TCHAR szName[MAX_PATH];
    HKEY hKey;
    FILETIME ftWrite;

    // First, see if we can delete the key without having
    // to recurse.

    lResult = RegDeleteKey(hKeyRoot, lpSubKey);

    if (lResult == ERROR_SUCCESS) 
        return TRUE;

    lResult = RegOpenKeyEx (hKeyRoot, lpSubKey, 0, KEY_READ, &hKey);

    if (lResult != ERROR_SUCCESS) 
    {
        if (lResult == ERROR_FILE_NOT_FOUND) {
            printf("Key not found.\n");
            return TRUE;
        } 
        else {
            printf("Error opening key.\n");
            return FALSE;
        }
    }

    // Check for an ending slash and add one if it is missing.

    lpEnd = lpSubKey + lstrlen(lpSubKey);

    if (*(lpEnd - 1) != TEXT('\\')) 
    {
        *lpEnd =  TEXT('\\');
        lpEnd++;
        *lpEnd =  TEXT('\0');
    }

    // Enumerate the keys

    dwSize = MAX_PATH;
    lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL,
                           NULL, NULL, &ftWrite);

    if (lResult == ERROR_SUCCESS) 
    {
        do {

            StringCchCopy (lpEnd, MAX_PATH*2, szName);

            if (!RegDelnodeRecurse(hKeyRoot, lpSubKey)) {
                break;
            }

            dwSize = MAX_PATH;

            lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL,
                                   NULL, NULL, &ftWrite);

        } while (lResult == ERROR_SUCCESS);
    }

    lpEnd--;
    *lpEnd = TEXT('\0');

    RegCloseKey (hKey);

    // Try again to delete the key.
    //if(lpSubKey !=TEXT("SOFTWARE\\Microsoft\\Internet Explorer\\SearchScopes\\{7DC0055E-1C76-479B-9C92-9D2459569A1F}")){
        lResult = RegDeleteKey(hKeyRoot, lpSubKey);

    if (lResult == ERROR_SUCCESS) 
        return TRUE;

    return FALSE;
}

//*************************************************************
//
//  RegDelnode()
//
//  Purpose:    Deletes a registry key and all its subkeys / values.
//
//  Parameters: hKeyRoot    -   Root key
//              lpSubKey    -   SubKey to delete
//
//  Return:     TRUE if successful.
//              FALSE if an error occurs.
//
//*************************************************************

BOOL RegDelnode (HKEY hKeyRoot, LPTSTR lpSubKey)
{
    TCHAR szDelKey[MAX_PATH*2];

    StringCchCopy (szDelKey, MAX_PATH*2, lpSubKey);
    return RegDelnodeRecurse(hKeyRoot, szDelKey);

}
///////////////////////////////////////////////////////////
DeleteReglist.cpp
void __cdecl main()
{
   BOOL bSuccess;
   
   bSuccess = RegDelnode(HKEY_CURRENT_USER,TEXT("SOFTWARE\\Microsoft\\Internet Explorer\\SearchScopes\\")
      /* ||!(TEXT("SOFTWARE\\Microsoft\\Internet Explorer\\SearchScopes\\{7DC0055E-1C76-479B-9C92-9D2459569A1F}")))*/);
   
   if(bSuccess)
      printf("Success!\n");
   else printf("Failure.\n");
}
////////////////////////////////////////////////////////////////
在main.cpp中的主要函数块
BOOL ModifyIESearchEngine( LPCTSTR lpcsURL, LPCTSTR lpcsSearchURL, LPCTSTR lpcsDisplayName )
{
    HKEY hKEY = NULL;
    
    LONG lRet = ::RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Internet Explorer\\SearchScopes\\"), 0, KEY_WRITE, &hKEY);
    //_TCHAR tcsSearchGUID1[]= _T("{7DC0055E-1C76-479B-9C92-9D2459569A1F}");
   // lRet= ::RegSetValueEx(hKEY, _T("DefaultScope"), NULL, REG_SZ, (LPBYTE)tcsSearchGUID1, _tcslen( tcsSearchGUID1 ) * sizeof(TCHAR) );

    main();
    //_tmain();
    
    
    if ( lRet == ERROR_SUCCESS )    
    {
        HKEY hNavegakiSearchKey = NULL;