Structured Storage and Persistent COM Objects

The EX27A program explicitly called member functions of IStorage and IStream to write and read a compound file. In the object-oriented world, objects should know how to save and load themselves to and from a compound file. That's what the IPersistStorage and IPersistStream interfaces are for. If a COM component implements these interfaces, a container program can "connect" the object to a compound file by passing the file's IStorage pointer as a parameter to the Save and Load member functions of the IPersistStorage interface. Such objects are said to be persistent. Figure 27-3 shows the process of calling the IPersistStorage::Save function.

A COM component is more likely to work with an IStorage interface than an IStream interface. If the COM object is associated with a particular storage, the COM component can manage substorages and streams under that storage once it gets the IStorage pointer. A COM component uses the IStream interface only if it stores all its data in an array of bytes. ActiveX controls implement the IStream interface for storing and loading property values.

click to view at full size.

Figure 27-3. Calling IPersistStorage::Save.

The IPersistStorage Interface

Both the IPersistStorage and IPersistStream interfaces are derived from IPersist, which contributes the GetClassID member function. Here's a summary of the IPersistStorage member functions:

HRESULT GetClassID(CLSID* pClsid);

Returns the COM component's 128-bit class identifier.

HRESULT InitNew(IStorage* pStg);

Initializes a newly created object. The component might need to use the storage for temporary data, so the container must provide an IStorage pointer that's valid for the life of the object. The component should call AddRef if it intends to use the storage. The component should not use this IStorage pointer for saving and loading; it should wait for Save and Load calls and then use the passed-in IStorage pointer to call IStorage::Write and Read.

HRESULT IsDirty(void);

Returns S_OK if the object has changed since it was last saved; otherwise, returns S_FALSE.

HRESULT Load(IStorage* pStg);

Loads the COM object's data from the designated storage.

HRESULT Save(IStorage* pStg, BOOL fSameAsLoad);

Saves the COM object's data in the designated storage.

The IPersistStream Interface

Here's a summary of the IPersistStream member functions:

HRESULT GetClassID(CLSID* pClsid);

Returns the COM component's 128-bit class identifier.

HRESULT GetMaxSize(ULARGE_INTEGER* pcbSize);

Returns the number of bytes needed to save the object.

HRESULT IsDirty(void);

Returns S_OK if the object has changed since it was last saved; otherwise, returns S_FALSE.

HRESULT Load(IStream* pStm);

Loads the COM object's data from the designated stream.

HRESULT Save(IStream* pStm, BOOL fClearDirty);

Saves the COM object's data to the designated stream. If the fClearDirty parameter is TRUE, Save clears the object's dirty flag.

IPersistStream Programming

The following container program code fragment creates a stream and saves a COM object's data in it. Both the IPersistStream pointer for the COM object and the IStorage pointer are set elsewhere.

 extern IStorage* pStg; extern IPersistStream* pPersistStream; IStream* pStream; if (pStg->CreateStream(L"MyStreamName",     STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,     0, 0, &pStream) == S_OK) {     ASSERT(pStream != NULL);     pPersistStream->Save(pStream, TRUE);     pStream->Release(); } 

If you program your own COM class for use in a container, you'll need to use the MFC interface macros to add the IPersistStream interface. Too bad there's not an "interface wizard" to do the job.



Programming Visual C++
Advanced 3ds max 5 Modeling & Animating
ISBN: 1572318570
EAN: 2147483647
Year: 1997
Pages: 331
Authors: Boris Kulagin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net