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   
    Generalvolatile keys lost on shutdownmemberTom Mason27 Jun '04 - 22:16 
    If you specify a key that does not already exist. then the funtion "CreateKey" is called, which does a RegCreateKeyEx with the REG_OPTION_VOLATILE flag. This means that when the PC is shutdown, or the user logs off (depending upon which hive the key is added to), the new key and all values are lost.
     
    Should be REG_OPTION_NON_VOLATILE.
    GeneralGood ideamemberTrollslayer25 May '03 - 0:09 
    I have been trying to decide the correct way to store application configurations and this is perfect - I just define the storage class and serialise it.
    Thank you Smile | :)
     
    Elaine Rose | [Rose]
     
    The tigress is here Big Grin | :-D
    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.
    GeneralExcellent IdeamemberAnonymous22 Jun '01 - 6:14 
    I was hoping to build a class to store/restore window position but was partial to storing the information as a blob rather than a series of keys. Your code made that a possability! Thanks Wink | ;)
    GeneralA WarningmemberPhilippe Lhoste30 Jan '01 - 23:09 
    This article is interesting for the techniques it presents, but I feel I must issue a warning.
     
    Microsoft, somewhere in its documentation, recommand not to use the registry as a database. In other words, avoid to store big binary data in the registry.
     
    Frown | :( Of course, MS uses to break the rules it has set (among others "don't put spaces in key names"...), and you can take a look at:
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MenuOrder
    (if you have Active Desktop with menu ordering by mouse) to see how they can store huge binary informations in the registry...
     
    So if you have a lot of data to save, do it in a file, not in the registry. Of course, the code presented in this article can be used for little data set, so it is not the article I blame, but the careless programmers Poke tongue | ;-P
    GeneralRe: A WarningmemberAmit Nabarro31 Jan '01 - 9:22 
    I do agree that the registry isn't there for holding large block of information. If i remember correctly i even put a limit on this.
     
    But this is very convinient when you want to store small structures of data. For example microsoft uses the registry to store the layout of a toolbar in its office application.
     
    Therefore what you're saying is very true, care must be taken at all times when handling the registry, that my opinion.

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

    Permalink | Advertise | Privacy | Mobile
    Web04 | 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