Step Four: Saving Properties

Pipeline components save their properties to persistent storage by implementing the IPersistStreamInit interface. When a component implements IPersistStreamInit, both the Win32®-based Pipeline Editor and the pipeline object use the component’s implementation of this interface to load and save the component’s properties.

When you added an object to the MinMaxShip project using the Pipeline Component Wizard (see Step 2: Adding an Object), the wizard added the following definition of IPersistStreamInit to MinMaxShipping.h file that it created:

// IPersistStreamInit
const long  m_lStreamVersionMajor;   // major version number of the 
                                    // stream
const long  m_lStreamVersionMinor;   // minor version number of the                                             // stream
STDMETHOD(GetClassID)(CLSID *pClassID);
STDMETHOD(IsDirty)(void);
STDMETHOD(Load)(IStream *pStm);
STDMETHOD(Save)(IStream *pStm, BOOL fClearDirty);
STDMETHOD(GetSizeMax)(ULARGE_INTEGER *pcbSize);
STDMETHOD(InitNew)(void);

The m_lStreamVersionMajor and m_lStreamVersionMinor variables are not part of the interface itself, but the CMinMaxShipping object’s constructor uses these variables to initialize the version of the MinMaxShip component.

CMinMaxShipping():m_lStreamVersionMajor(0),m_lStreamVersionMinor(0)
{
}

In addition to adding the IPersistStreamInit interface definition to the component’s class definition file, the wizard added stub implementations of the interface’s methods to CMinMaxShipping.cpp. To complete the implementation of this interface, you replace the body of these stub implementations with your own.

In the IPersistStreamInit::GetClassID implementation, return the class ID for the CMinMaxShipping object. The default implementation that the Pipeline Component Wizard provides you is fine:

STDMETHODIMP CMinMaxShipping::GetClassID(CLSID *pClassID)
{
    *pClassID = GetObjectCLSID();
    return S_OK;
}

Recall that in Step 3: Adding Properties, you added CComVariant member variables to the CMinMaxShipping class definition to store the values of the MinMaxShip component’s MinShipping and Percentage properties. When you implement IPersistStreamInit::Load and IPersistStreamInit::Save, the use of CComVariant to store these values makes sense; you can use the CComVariant object’s WriteToStream and ReadFromStream methods to write the values of these properties to a persistent stream that the pipeline passes to your Load and Save implementations, as follows:

STDMETHODIMP CMinMaxShipping::Save(IStream *pStm, BOOL fClearDirty)
{
    HRESULT hRes = S_OK;

    hRes = m_varPercentage.WriteToStream(pStm);

    if(FAILED(hRes)){
        return hRes;
    }

    hRes = m_varMinShipping.WriteToStream(pStm);
    if(FAILED(hRes){
        return hRes;
    }
    return hRes;
}

Finally, implement IPersistStreamInit::GetSizeMax to identify the amount of space that the pipeline MinMaxShip component needs to set aside to store the component’s property values. The MinMaxShip component’s GetSizeMax implementation adds the size of the size of the m_varMinShipping and m_varPercentage variables, and returns the sum in the LowPart member of the GetSizeMax method’s pcbSize out-value variable:

STDMETHODIMP CMinMaxShipping::GetSizeMax(ULARGE_INTEGER *pcbSize)
{
    pcbSize->LowPart = sizeof(m_varMinShipping) + sizeof(m_varPercentage);
    pcbSize->HighPart = 0;
    return S_OK;
}


© 1997-1998 Microsoft Corporation. All rights reserved.