Serialization routine

It starts with the CObject class that is the ancestor to most MFC classes, which is equipped with a Serialize() member function. The class should declare with the macros DECLARE_SERIAL(< class name >) and also should add implement macro IMPLEMENT_SERIAL(< class name >, < parent class name >, < Schema version >). Serialize routine has this prototype.

void Serialize(CArchive& ar)

Let us look into a simple example. Here we declared a class called CStudent to store and retrieve fields of a student. It contains standard, enrollment number, name, marks and age. We are saving or restoring these members Serialize routine.

class CStudent : public CObject
{
public :
  CStudent(void);
  ~CStudent(void);


  WORD    m_wStd;
  WORD    m_wRoll;
  CString m_sName;
  double  m_dMarks;
  WORD    m_wAge;

public :
  void Serialize(CArchive& ar);

  DECLARE_SERIAL(CStudent);
};

IMPLEMENT_SERIAL(CStudent, CObject, 0)

/* Main Serialize routine */
void CStudent::Serialize(CArchive& ar)
{
  CObject::Serialize(ar);

  if( ar.IsStoring() ) {
    /* Serialize STD, ROLL, NAME, MARKs, AGE to file */
    ar << m_wStd << m_wRoll << m_sName << m_dMarks << m_wAge;
  } else {
    /* Read STD, ROLL, NAME, MARKs, AGE from file */
    ar >> m_wStd >> m_wRoll >> m_sName >> m_dMarks >> m_wAge;
  }

}

CArchive class

Serialization routine does not directly deal with CFile. It uses CArchive object instead of CFile. CArchive class decouples the file object from Serialize() routine. This is to support any type of stream classes in Serialize routine and make it independent of file stream. Thus Serialize is not limited to disk/storage files. It can be socket file to Serialize in network stream or serial stream file transmitted in serial port.
/* Serialize routine for data saving */
void OnSave() 
{
  UpdateData(TRUE);

  CFile file;
  /* Open file for writing */
  file.Open("student.dat", CFile::modeCreate | CFile::modeWrite);
  /* pass file to CArchive constructor */
  CArchive ar(&file, CArchive::store);
  m_Student.Serialize(ar);
  ar.Close();
  file.Close();
}

/* Serialize routine for data retrieving */
void OnOpen() 
{
  CFile file;
  /* Open file for writing */
  file.Open("student.dat",  CFile::modeRead);
  /* pass file to CArchive constructor */
  CArchive ar(&file, CArchive::load);
  m_Student.Serialize(ar);
  ar.Close();
  file.Close();
  UpdateData(FALSE);
}

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#