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.  
mweagleAsked:
Who is Participating?
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:
Do you want document properties in separate streams?  I have sample code for that if you want.

Also here is some sample code to serialize a CObject derived object to/from a stream.  You would call this in your SaveToStorage/ReadFromStorage functions of your document and pass m_lpRootStg as the lpRootStg param.

bool CMyObject::WriteToStorage(LPSTORAGE lpRootStg, LPCTSTR szName) {
      COleStreamFile file;
      CFileException fe;
      if (!file.CreateStream(lpRootStg, szName,
            CFile::modeReadWrite|CFile::shareExclusive|CFile::modeCreate, &fe))
      {
            if (fe.m_cause == CFileException::fileNotFound)
                  AfxThrowArchiveException(CArchiveException::badSchema);
            else
                  AfxThrowFileException(fe.m_cause, fe.m_lOsError);
      }
      // save to Contents stream
      CArchive saveArchive(&file, CArchive::store | CArchive::bNoFlushOnDelete);
      saveArchive.m_pDocument = NULL;
      saveArchive.m_bForceFlat = FALSE;
      TRY
      {
            Serialize(saveArchive);
            saveArchive.Close();
            file.Close();
            
            // commit the root storage
            SCODE sc = lpRootStg->Commit(STGC_ONLYIFCURRENT);
            if (sc != S_OK)
                  AfxThrowOleException(sc);
      }
      CATCH_ALL(e)
      {
            file.Abort();   // will not throw an exception
            // CommitItems(FALSE); // abort save in progress
            saveArchive.Abort();
            THROW_LAST();
      }
      END_CATCH_ALL;
      return true;
}

bool CMyObject::ReadFromStorage(LPSTORAGE lpRootStg, LPCTSTR szName) {
      // open stream
      COleStreamFile file;
      CFileException fe;
      if (!file.OpenStream(lpRootStg, szName, CFile::modeRead|CFile::shareExclusive, &fe)) {
            QSchema::SetSchemaDirect(21);      // last schema without schema support
            if (fe.m_cause == CFileException::fileNotFound) {
                  return false;
            } else {
                  AfxThrowFileException(fe.m_cause, fe.m_lOsError);
            }
      }

      // load it with CArchive (loads from Contents stream)
      CArchive loadArchive(&file, CArchive::load | CArchive::bNoFlushOnDelete);
      loadArchive.m_pDocument = NULL;
      loadArchive.m_bForceFlat = FALSE;
      TRY
      {
            Serialize(loadArchive);     // load main contents
            loadArchive.Close();
            file.Close();
      }
      CATCH_ALL(e)
      {
            file.Abort();   // will not throw an exception
            // DeleteContents();   // removed failed contents
            loadArchive.Abort();
            THROW_LAST();
      }
      END_CATCH_ALL;
      return true;
}



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
mweagleAuthor Commented:
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.  

0
RONSLOWCommented:
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.

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

mweagleAuthor Commented:
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.
0
RONSLOWCommented:
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++

0
mweagleAuthor Commented:
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?
0
RONSLOWCommented:
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.

0
mweagleAuthor Commented:
OK, I'll do the same.  Thanks again for the answer.  
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.