CObList Serialization in MFC Extension DLL

 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?
sdjAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RONSLOWCommented:
Please send some more code to look at.

my email is mailto:Roger_Onslow@compsys.com.au
0
RONSLOWCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.