共用方式為


CComBSTR::WriteToStream

儲存 m_str 成員至資料流。

HRESULT WriteToStream(
   IStream* pStream 
) throw( );

參數

  • pStream
    [out] IStream 介面的指標資料流。

傳回值

標準 HRESULT 值。

備註

使用 ReadFromStream 函式,您可以重新建立資料流的內容的 BSTR。

範例

//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;
}

需求

Header: atlbase.h

請參閱

參考

CComBSTR 類別

CComBSTR::ReadFromStream