Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CArchived CDocument -> Structured Storage

Posted on 1998-04-07
8
Medium Priority
?
986 Views
Last Modified: 2013-11-20
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.  
0
Comment
Question by:mweagle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 680 total points
ID: 1317536
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
 

Author Comment

by:mweagle
ID: 1317537
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1317538
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:mweagle
ID: 1317539
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1317540
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
 

Author Comment

by:mweagle
ID: 1317541
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1317542
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
 

Author Comment

by:mweagle
ID: 1317543
OK, I'll do the same.  Thanks again for the answer.  
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

636 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