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