Registry manipulation class with exception handling

This is another registry class manipulation which uses exception handling.

It basically uses the extended API provided for registry access (RegXXXXXEx) and I tried to simplify it as possible. Although right now there's no function overidables for Read/Write to much simplify the passing of data (so you don't have to typecast them to LPBYTE) but I'll work out this one.

Opening of data is fairly simple, you just specify the location of the key handle (HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER...etc) and the subkey (SOFTWARE\\MyCompany\\MyProject...). The option (reserved for future enhancement of the API) and the security access mask (default to all access) are optional. You can open the registry via call to CRegistry::Open() or just by passing the parameters of the Open() function to the constructor.

CRegistry reg1( HKEY_LOCAL_MACHINE, "SOFTWARE\\MyCompany\\somedata" );

CRegistry reg2;
reg2.Open( HKEY_LOCAL_MACHINE, "SOFTWARE\\MyCompany\\someotherdata" );

Reading and writing values are done by calls to Read() and Write() respectively.

Since it is using the CException derived class to handle exception, it is also easier to display the error encountered:


try {
    CRegistry regConfig;
    regConfig.Open( HKEY_LOCAL_MACHINE, "SOFTWARE\\MyCompany\\MyData" );
    DWORD    nLen;
    BYTE        nSomeByteData;
    char        szSomeStringData;
    DWORD    nSomeDWordData;
    /*if you dont want the type of the data to be returned... 
    then specify NULL on the third parameter...
    */
    regConfig.Read( &nSomeData, ( nLen=sizeof( nSomeData ), &nLen ), NULL, "Some Byte Data" );
    regConfig.Read(( LPBYTE )&szSomeStringData, ( nLen=sizeof( szSomeStringData ), &nLen ), NULL, "Some String Data" );
    regConfig.Read(( LPBYTE )&nSomeDWordData, ( nLen=sizeof( nSomeDWordData ), &nLen ), NULL, "Some DWord Data" );

    /* ...  but if you want the data type, then pass a DWORD pointer
    */
    DWORD    nType;
    char          nSomeUnknownData[ 50 ];
    regConfig.Read(( LPBYTE )&nSomeUnknownData, ( nLen=sizeof( nSomeUnknownData ), &nLen ), &nType, "Some Unknown Data" );
    /* your nType now may have the following value: REG_BINARY, REG_DWORD, REG_SZ....
    */
}

catch( CRegistryException *regError ) {
    // display the error
    regError->ReportError();

    // now delete the handle
    regError->Delete();
}

All the data and data types used by this class are also the same one being used by the API (like HKEY_LOCAL_MACHINE, KEY_READ, REG_EXPAND_SZ, REG_LINK....etc).

Download source - 3 KB



Comments

  • All fine and dandy, but the exceptions...

    Posted by Legacy on 09/30/2001 12:00am

    Originally posted by: Martijn

    Hi,

    The exception is thrown if a key does not exist. This is all fine and dandy, but it poses a problem when checking for key existence. e.g.: my program runs and reads it's config from the registry. The first time, there is no key referencing my program, because my program stores it's default values upon program close. This will throw an exception (2, file not found) from your class. However, in my catch() there's no way of finding what went wrong because m_nError is protected...

    Reply
  • I'don't Know How Operation...

    Posted by Legacy on 10/26/2000 12:00am

    Originally posted by: Choi Hyun Kil

    Sorry.

    My Name Is Hyun Kil Choi. I'm Korean
    Recently, I Started on MFC Programming....by the way
    I Need This Class'Operation Sample (Example)

    pls, Help Me

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

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds