Rename MFC CArchive Serialized Class

I am using the MFC CArhive class to save a project file for my application. One of the class names is wildly inaccurate, and I would like to change it, but simply changing the name everywhere makes archive files with an archive error unnecessary. Is there a way to change the name of the archived class without rendering all previously saved files?

This is, of course, without using typedef to access an existing class with a new name or save a version of a class with an old name for reading archive files and copying reading objects into one class with a new name.

+6
c ++ mfc
source share
2 answers

The crucial point is that when using DECLARE_SERIAL and IMPLEMENT_SERIAL CRuntimeClass member CRuntimeClass added to your class containing the name in the m_lpszClassName field. This CRuntimeClass object CRuntimeClass also added to the list supported by the framework, which runs when classes are dynamically created. You must ensure that the CRuntimeClass object contains the old name of your class in m_lpszClassName .

You have two options:

  • CRuntimeClass object to set the name

  • Change the class name stored in its m_lpszClassName after it has been created

Overriding the construction of a CRuntimeClass object

To do this, you will need to make your own versions of DECLARE_DYMAMIC , DECLARE_DYNCREATE , DECLARE_SERIAL , IMPLEMENT_DYMAMIC , IMPLEMENT_DYNCREATE and IMPLEMENT_SERIAL . You can simply copy and rename existing implementations. In your version of IMPLEMENT_DYNAMIC you need to change the code that CRuntimeClass creates CRuntimeClass that it is initialized with the old class name.

Changing the name of the class stored in the m_lpszClassName field after creating the CRuntimeClass object

Since CRuntimeClass is created by a static initializer, I don’t think you can do this from your class. I think the best place for this is in your InitInstance application. Add the static char* variable to the application class containing the old class name. Then in InitInstance set the m_lpszClassName field to your CRuntimeClass .

  • The first method has the advantage of saving changes to the class on its own.
  • The second makes the application aware of the class in such a way that it possibly should not be.

In any case, the first thing to do is fully familiarize yourself with how dynamic creation and serialization work.

+8
source share

There is a third option that I took. I originally called one of my serializable classes CEvent , which didn't seem to be a problem on VC6.0; but on VS2010 there is a built-in MFC class called CEvent , so I had to change it.

So, I fixed it by changing the old format files before calling COleServerDoc::OnOpenDocument

I just open, read into memory, find and replace CEvent any other class name (should be the same size), write and save as a new file name, then open and read a new file, without any problems.

I still change the file extension for the new version, so everything works.

0
source share

All Articles