Click here to Skip to main content
Click here to Skip to main content

Serializing into the registry

By , 23 Jan 2001
 
  • 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

    About the Author

    Amit Nabarro
    United States United States
    Member
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralMemory LeakmemberReuven 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.

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

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