Click here to Skip to main content
12,761,248 members (30,181 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 23 Jan 2001

Serializing into the registry

, 23 Jan 2001
Rate this:
Please Sign up or sign in to vote.
A template class to serialize objects into the registry in binary form
<!-- Download Links -->
  • Download source files - 1 Kb
  • Download demo project - 15 Kb
  • Introduction

    This example shows how to serialize CObject derived classes into the registry in binary form.

    Most of MFC classes are derived from an abstract base class called CObject, either directly or indirectly. CObject provides the functionality of  serializing the object data members by ordering the data in a stream of bytes and then saving it into a file. By this you can make your objects persistent and reload the information after terminating the application and restarting it.

    The information that is serialized is very much application specific. In order to serialize and object one must override the Serialize method from the base class and implement its own serialization code.

    There are a few things that must be taken into consideration. In the following example I decided that instead of showing how to serialize some abstract class derived from CObject I will do it with a real MFC object class. Therefore I used Zafir Anjum's article on serializing a CTreeCtrl, published in CodeGuru which explains how to serialize this control. Once you have a serialize method the way to serializing into the registry is short.

    CRegSerialize is a template class that can accept any CObject derived class as parameter. Using a template class insures that if your class is not serializable this error will be discovered in compile time and not during run time.

    All you have to do is initalize a CRegSerialize class object with your CObject derived class as a parameter

    CRegSerialize<CMyTreeCtrl> 	reg;

    Then use the GetObject method to assign a pointer to the object you wish to serialize.


    Now use the write method to serialize the information into the registry giving the appropriate parameters

    reg.Write(HKEY_LOCAL_MACHINE,_T("Software\\RegSerializeDemo"),_T("Tree")) ;

    To read from the registry do the exact proccess and use the Read method instead of the Write.

    reg.Read(HKEY_LOCAL_MACHINE,_T("Software\\RegSerializeDemo"),_T("Tree")) ;


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Amit Nabarro
    United States United States
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.170217.1 | Last Updated 24 Jan 2001
    Article Copyright 2001 by Amit Nabarro
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid