共用方式為


CComBSTR::Attach

Attaches a BSTR to the CComBSTR object by setting the m_str member to src.

void Attach(
   BSTR src 
) throw( );

Parameters

  • src
    [in] The BSTR to attach to the object.

Remarks

Do not pass an ordinary wide-character string to this method. The compiler cannot catch the error and run time errors will occur.

注意

This method will assert if m_str is non-NULL.

Example

// STDMETHOD(BSTRToUpper)(/*[in, out]*/ BSTR bstrConv);
STDMETHODIMP InplaceBSTRToUpper(BSTR bstrConv)
{
   // Assign bstrConv to m_str member of CComBSTR
   CComBSTR bstrTemp;
   bstrTemp.Attach(bstrConv); 

   // Make sure BSTR is not NULL string
   if (!bstrTemp)
        return E_POINTER;

   // Make string uppercase 
   HRESULT hr;
   hr = bstrTemp.ToUpper();
   if (hr != S_OK)
       return hr;

   // Set m_str to NULL, so the BSTR is not freed
   bstrTemp.Detach(); 

   return S_OK; 
}

Requirements

Header: atlbase.h

See Also

Reference

CComBSTR Class

CComBSTR::Detach

CComBSTR::operator =

Other Resources

CComBSTR Members