Link to home
Start Free TrialLog in
Avatar of mweagle
mweagle

asked on

CArchived CDocument -> Structured Storage

I've got an SDI whose CDocument I'd like to break apart using structured storage.  I think that I need to use COleStreamFile, but I can't find any examples of how to break apart the document's variables into different streams.  (This is possible, right?)  My question is really for a referral: Besides Kruglinski, can anyone point me in the direction of sample code/explanations which demonstrate how to do this?  For bonus points, anyone have a snippet?  Thanks for any help.  
ASKER CERTIFIED SOLUTION
Avatar of RONSLOW
RONSLOW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mweagle
mweagle

ASKER

This is exactly the kind of thing I'm looking for.  I have something like this:
CMyDocument:public CDocument
{
...
CMyBigObject1 m_object1;
CMyBigObject2 m_object2;
.
.
.
CMyBigObjectN m_objectN;
...}

Each CMyBigObjectN type has multiple CObLists and other variable storage types and so I'd like to break each CMyBigObjectN into a different stream so I don't have to read in the whole file.  Is this what you mean when you ask about document properties in different streams?  If it is, I'd really like to see your code.  Thanks for the help.  

I mean that I write out OLE document properties to the approriate stream (as opposed to the "content" stream that MFC writes your serialized data to).  I also write out (using the code above) my own data stored in a heirachy of lists.

What I would do for your code is
void CMyDocument::SaveToStorage(CObject* pObject) {
      m_object1.WriteToStorage(m_lpRootStg,"Object1");
      m_object2.WriteToStorage(m_lpRootStg,"Object2");
      ...
      m_objectN.WriteToStorage(m_lpRootStg,"ObjectN");
      CDocument::SaveToStorage(pObject);
}

where WriteToStorage method for each object is as per my answer above.  You simply use the same Serialize method as you'd use for standard MFC serializing.

Similar for reading from storage.  Of course, for reading, you don't need to read all the storages in, nor in the same order that you wrote them as each is independant.

Avatar of mweagle

ASKER

Great, I think that will solve the problem.  Sorry for my slowness (OLE is uncharted ground for me), but I have two related ?s if you don't mind.

(1) What is pObject in SaveToStorage?; and
(2) What is going on in CDocument::SaveToStorage(pObject)?  Is this a COleDocument call?  I searched MSDN but didn't find the function.

I wish VC5's docs were as clear as your explanation :)  Alas, they often are not, so now I'm off to amazon to find a good intro book on COM.  I need a bigger bookshelf.  Thanks again.
pObject is usually NULL (if you want to save the whole document) - but COleServerItem also uses this to save itself, in which case it calls the SaveToStorage of its associated document and passes itself (this) as the pObject to the document.

Yes - SaveToStorage is an (undocumented) COleDocument function and is called by OnSaveDocument to do the saving.  The default implementation is very much like my code above but is hard coded to write to the stream "Contents".  I simply took the MFC code and changed it a little to allow me to write to a nominated stream.

Look in the MFC source to see how it is called (that's what I did).

A COM book may not help much with MFC environment .. they are usually straight C++

Avatar of mweagle

ASKER

Good thing mail interrupts IE4 (you saved me some money).  I now just need to change CMyDocument's base class to COleDocument and I've solved a problem I should have foreseen in the planning stage.  (The worst kind.)

If not COM, could you recommend a book on OLE & MFC?  Or is the MFC source sufficient for this problem?
I haven't read many book on OLE/MFC - I do have a book on COM and have attended some seminars on COM.  Most of what I know comes from reading the docco, then looking at source code an smaples to work out what the docs really meant and find out how they work.

So, I cannot really recommend a book for you.

But the MFC source was sufficient for me as I just looked at the source code to see how files were saved to OLE structured storage and adapted what I needed from there.

Avatar of mweagle

ASKER

OK, I'll do the same.  Thanks again for the answer.