CComBSTR::WriteToStream

Saves the m_str member to a stream.

HRESULT WriteToStream( 
   IStream* pStream  
) throw( );

Parameters

  • pStream
    [in] A pointer to the IStream interface on a stream.

Return Value

A standard HRESULT value.

Remarks

You can recreate a BSTR from the contents of the stream using the ReadFromStream function.

Example

//implementation of IDataObject::GetData()
STDMETHODIMP CMyDataObj::GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium)
{
    HRESULT hr = S_OK;
    if (pformatetcIn->cfFormat == CF_TEXT && pformatetcIn->tymed == TYMED_ISTREAM)
    {
        IStream *pStm;
        // Create an IStream from global memory
        hr = CreateStreamOnHGlobal(NULL, TRUE, &pStm);
        if (FAILED(hr))
            return hr;

        // Initialize CComBSTR
        CComBSTR bstrStr = OLESTR("Hello World");

        // Serialize string into stream 
        // the length followed by actual string is serialized into stream
        hr = bstrStr.WriteToStream(pStm);

        // Pass the IStream pointer back through STGMEDIUM struct
        pmedium->tymed = TYMED_ISTREAM;
        pmedium->pstm = pStm;
        pmedium->pUnkForRelease = NULL; 
    }

    return hr;
}

Requirements

Header: atlbase.h

See Also

Reference

CComBSTR Class

CComBSTR::ReadFromStream