Registry API Wrapper

 Download Source Code and Example

I personally find the Win32 Registry API to be overly complex for performing simple operations. The Win32 Registry API is very "rich". You can do a lot with it, but most of the time you just want to read or write a value. The error checking involved in doing even simple registry operations is enough to hide the meaning of the code. Most of the time you either want the operation to succeed or fail as a whole, you're not interested in handling errors at each stage and performing recovery operations...

I've seen many Win32 Registry API wrapper classes but all of them failed to actually make it easier to use the registry than the API. Most have been simple wrappers which add no value (apart from saving you the bother or having to pass the HKEY to each function...).

CRegistryKey is essentially just a very simple wrapper around a HKEY. It adds value by converting errors to exceptions, defaulting most of the rarely used parameters with sensible values and providing STL style iterators for sub keys and values. All of the Win32 Registry API  is present as member functions, but if you want to check for errors without using exceptions there's a conversion operator that will allow access to the underlying HKEY.

CRegistryKey was designed to make code like this as easy as possible...

try
{
   CRegistryKey key = CRegistryKey(HKEY_LOCAL_MACHINE, _T("SOFTWARE"));
  
   // for each sub key of this key...
  
   for (CRegistryKey::SubkeyIterator it = key.BeginSubkeyIteraton();
        it != key.EndSubkeyIteraton();
        ++it)
   {
      // Dump out the key's name

      tcout << it.GetName() << endl;

      // Then display each value
  
      CRegistryKey subKey = it.OpenKey();
  
      for (CRegistryKey::ValueIterator val = subKey.BeginValueIteration();
           val != subKey.EndValueIteration();  
           ++val)
      {
         tcout << " " << val.name() << T(" ") << val.valueAsString() << endl;
      }
   }
}
catch (CRegistryKey::Exception &e)
{
   e.MessageBox();
}

So that this article and code can be kept up to date more easily I've provided a link to where the article is located on my own web pages. Read the full article and download the source code.

Last updated: 3 July 1998




Comments

  • A little error.

    Posted by Legacy on 05/21/1999 12:00am

    Originally posted by: Laurent Kemp�

    Hi Len,

    I have found a little error in your code.

    In your CRegistryKey::Value constructor you allocate memory with
    _tcsdup that should be freed by free function because it's allocated
    with malloc (take alook into MSDN for "_strdup, _wcsdup, _mbsdup").

    CRegistryKey::Value::Value(const Value &rhs)
    : m_pName(_tcsdup(rhs.m_pName)),

    CRegistryKey::Value::~Value()
    {
    //ERROR
    // delete[] m_pName;
    free(m_pName);

    Best regards.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds