Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

    //
    reg.GetObject(&m_TreeCtrl); 
    //
    

    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")) ;
    //
    

    License

    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

    Share

    About the Author

    Amit Nabarro

    United States United States
    No Biography provided

    Comments and Discussions

     
    Generalvolatile keys lost on shutdown PinmemberTom Mason27-Jun-04 22:16 
    GeneralGood idea PinmemberTrollslayer25-May-03 0:09 
    GeneralMemory Leak PinmemberReuven Kadison19-Nov-02 22:13 
    It was very helpful, but I found a memory leak.
    When calling:

    LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,file.Detach(), file.GetLength());

    You actually Lose the memory allocated by calloc.
     
    And the pointer you had to byData is not valid to be free later!!!
     
    You should do it like this:

    DWORD size = file.GetLength();
    byData = file.Detach();

    LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,byData, size);


     
    Reuvenk.
    GeneralExcellent Idea PinmemberAnonymous22-Jun-01 6:14 
    GeneralA Warning PinmemberPhilippe Lhoste30-Jan-01 23:09 
    GeneralRe: A Warning PinmemberAmit Nabarro31-Jan-01 9:22 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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