
Tech Tips
edited by George Frazier

Listing One
/////////////////////////////////////////////////////////////////////////////
// Extract from compcatutil.cpp
// License:     (Licensed under the Synesis Software Standard Source License)
//              http://synesis.com.au/licenses/ssssl.html
//              Copyright (C) 2003, Gregory Peet & Matthew Wilson.
//              All rights reserved.
/////////////////////////////////////////////////////////////////////////////

extern "C"
ULONG WINAPI CompCat_FindBadA(PfnHandleBadKeyA pfn, DWORD param)

{
  using winstl::a2w;
  using winstl::reg_key_a;
  typedef winstl::reg_key_sequence_a  sequence_t;

  ULONG       cInvalid = 0;
  sequence_t  cc_keys(HKEY_CLASSES_ROOT, "Component Categories");
  sequence_t::const_iterator  begin = cc_keys.begin();
  sequence_t::const_iterator  end   = cc_keys.end();

  for(; begin != end; ++begin)
  {
    CLSID           clsid;
    reg_key_a const key(*begin);
    if(CLSIDFromString(a2w(key.name().c_str()), &clsid) != NOERROR)
    {
      if((*pfn)(key.name().c_str(), param))
      {
        ++cInvalid;
      }
      else
      {
        break;
      }
    }
  }
  return cInvalid;
}

Listing Two
/////////////////////////////////////////////////////////////////////////////
// Extract from compcatutil.cpp
// License:     (Licensed under the Synesis Software Standard Source License)
//              http://synesis.com.au/licenses/ssssl.html
//              Copyright (C) 2003, Gregory Peet & Matthew Wilson.
//              All rights reserved.
////////////////////////////////////////////////////////////////////////////

namespace 
{
  BOOL WINAPI CompCat_DeleteBadKeyA(char const *key_name, DWORD param)
  {
    return ERROR_SUCCESS == Reg_DeleteKey((HKEY)param, key_name, true);
  }
}
extern "C"
ULONG WINAPI CompCat_DeleteBad()
{
  winstl::reg_key_a;
  reg_key_a key_root(HKEY_CLASSES_ROOT, "Component Categories");
  return CompCat_FindBadA(CompCat_DeleteBadKeyA, (DWORD)key_root.get_key_handle());
}


Listing Three

LONG Reg_DeleteKey(HKEY hkey, LPCTSTR lpszSubKey, BOOL bRecurse)
{
    /* Open the sub-key. */
    HKEY    hkeySub;
    LONG    lRet = RegOpenKeyEx(hkey, lpszSubKey, 0, 
                                          KEY_READ | KEY_WRITE, &hkeySub);
    if(lRet == ERROR_SUCCESS)
    {
        /* Enumerate all of the decendents of this child. */
        for(; lRet == ERROR_SUCCESS; )
        {
            FILETIME    time;
            TCHAR       szChildKey[MAX_PATH + 1];
            DWORD       dwSize = sizeof(szChildKey) / sizeof(szChildKey[0]);
            lRet = RegEnumKeyEx(hkeySub, 0, szChildKey, &dwSize, 
                                                  NULL, NULL, NULL, &time);
            if(lRet != ERROR_SUCCESS)
            {
                if(lRet == ERROR_NO_MORE_ITEMS)
                {
                    /* The enumeration is complete, so break main 
                     * loop, but indicate success.
                     */
                    lRet = ERROR_SUCCESS;
                    break;
                }
            }
            else
            {
                if(!bRecurse)
                {
                  /* Can't delete keys with children, unless requested to. */
                    lRet = ERROR_KEY_HAS_CHILDREN;
                }
                else
                {
                    /* Recursively delete this child. */
                    lRet = Reg_DeleteKey(hkeySub, szChildKey, bRecurse);
                }
            }
        }
        /* Close the sub-key. */
        RegCloseKey(hkeySub);
        /* Delete the sub-key. */
        if(lRet == ERROR_SUCCESS)
        {
            lRet = RegDeleteKey(hkey, lpszSubKey);
       }
    }
    return lRet;
}






3



