Solved

CObList Serialization in MFC Extension DLL

Posted on 1997-09-09
2
575 Views
Last Modified: 2013-11-20
 Environment: Visual C++5.0 WinNT 4.0

Hi,

I am trying to serialize a CObList in a similar manner to the Microsoft drawcli sample. The main differences are my code resides in an MFC extension DLL, while there code is in an EXE. Also their document is based on a COleDocument and mine is based on a standard CDocument.

I have included the implement serial and declare serial macros in the class and its CPP file. The object list is defined as:
    typedef CTypedPtrList<CObList, AZDRAWANNO*> CDrawObjList;

The problem I have is that dispite the fact that the serialize function in each of my objects is called the data is in some way corrupt because it will not reload; I get the error: "Unexpected File Format". In the CObList::Serialize code I have noticed that the number of objects is not initialized when attempting to load, although is correct when attempting to save.

I have also noticed that when I debug the Microsoft example the pNode object created in CObList::Serialize is defined as a CDrawRect *and* includes a node in the variables window labelled CDrawRect (allowing the data that is to be stored to be seen). When I debug my code the pNode object is defined as CDrawRect, but the CDrawRect node is missing.

Any suggestions?
0
Comment
Question by:sdj
  • 2
2 Comments
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1305969
Please send some more code to look at.

my email is mailto:Roger_Onslow@compsys.com.au
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 1305970
YOu code contains this...

void AZDIBCONTAINER::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// Save the DIB image to disk.
GetDIBImage()->Save( ar.GetFile() );
}
else
{
// Load the DIB image from disk.
GetDIBImage()->Load( ar.GetFile() );
}
// load the annotations from disk. annotationList is the Object list, it
contains
// AZDRAWRECT objects derived from AZDRAWANNO derived from
// CObject. NOTE see previous article for typedef!
annotationList.Serialize( ar );
}


The problem is you are fiddling about with the file that CArchive is using - thie is NOT ON!!.  CArchive does not keep its file up to date all the time - it uses read buffers for effciency and for backtracking.

If both you and it try to write to (or read from) this file, then you are going to tread on each others feet (so to speal)


You should at least call CArchive::Flush() beofre (and after) trying to directly acess the file.  IN fact, the help for GetFile tells you to do this anyway.

Even better, avoid directly accessing the underlying file anyway - See if your DIB image class can be change to save and load from an archive (ie. implement a serialize function itself).  Or make it write to an in-memory buffer (maybe an in-memory file), then you can transfer that to/from the archive with the CArchive::Write/Read member functions.

In anycase, the problem is with this little bit of code, which stuffs up the file so the list doesn't serialize properly.  WHen you fix this, you should find the problem goes away.

Roger

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now