共用方式為


CComBSTR 類別

這個類別是的 BSTR包裝函式。

語法

class CComBSTR

成員

公用建構函式

名稱 描述
CComBSTR::CComBSTR 建構函式。
CComBSTR::~CComBSTR 解構函式。

公用方法

名稱 描述
CComBSTR::Append 將字串附加至 m_str
CComBSTR::AppendBSTR 附加 BSTRm_str
CComBSTR::AppendBytes 將指定的位元組數目附加至 m_str
CComBSTR::ArrayToBSTR BSTR從 safearray 中每個元素的第一個字元建立 ,並將它附加至 CComBSTR 物件。
CComBSTR::AssignBSTR BSTR 指定給 m_str
CComBSTR::Attach BSTR將附加至 CComBSTR 物件。
CComBSTR::BSTRToArray 建立以零起始的一維安全數位,其中陣列的每個元素都是 來自 CComBSTR 物件的字元。
CComBSTR::ByteLength 傳回位元組的 m_str 長度。
CComBSTR::Copy 傳回的 m_str複本。
CComBSTR::CopyTo 透過參數傳回的m_str[out]複本
CComBSTR::Detach m_strCComBSTR 物件中斷連結。
CComBSTR::Empty 釋放 m_str
CComBSTR::Length 傳回的 m_str長度。
CComBSTR::LoadString 載入字串資源。
CComBSTR::ReadFromStream BSTR從數據流載入物件。
CComBSTR::ToLower 將字串轉換成小寫。
CComBSTR::ToUpper 將字串轉換成大寫。
CComBSTR::WriteToStream 儲存 m_str 至數據流。

公用運算子

名稱 描述
CComBSTR::operator BSTR CComBSTR 物件轉換成 BSTR。
CComBSTR::operator ! 根據是否 m_str 為 NULL,傳回 TRUE 或 FALSE。
CComBSTR::operator != CComBSTR比較與字串。
CComBSTR::operator & 傳回 的 m_str位址。
CComBSTR::operator += CComBSTR 附加至物件。
CComBSTR::operator < CComBSTR比較與字串。
CComBSTR::operator = 將值指定給 m_str
CComBSTR::operator == CComBSTR比較與字串。
CComBSTR::operator > CComBSTR比較與字串。

公用資料成員

名稱 描述
CComBSTR::m_str 包含 BSTRCComBSTR 物件相關聯的 。

備註

類別 CComBSTR 是 的包裝函式 BSTR,其為長度前置字串。 長度會以整數的形式儲存在字串中數據之前的記憶體位置。

BSTR 最後一個計算字元之後以 Null 結束,但也可能包含內嵌在字串中的 Null 字元。 字串長度取決於字元計數,而不是第一個 Null 字元。

注意

類別 CComBSTR 提供一些成員(建構函式、指派運算符和比較運算符),以 ANSI 或 Unicode 字串作為自變數。 這些函式的 ANSI 版本比 Unicode 對應專案效率低,因為內部通常會建立暫時的 Unicode 字串。 為了提高效率,請盡可能使用 Unicode 版本。

注意

由於在 Visual Studio .NET 中實作的改良查閱行為,因此,先前 bstr = L"String2" + bstr;版本中可能已編譯的程式代碼應該改為實作為 bstr = CStringW(L"String2") + bstr

如需使用 CComBSTR時的注意事項清單,請參閱 使用 CComBSTR進行程序設計。

需求

頁眉: atlbase.h

CComBSTR::Append

lpsz 或 BSTR 成員 bstrSrc 附加至 m_str

HRESULT Append(const CComBSTR& bstrSrc) throw();
HRESULT Append(wchar_t ch) throw();
HRESULT Append(char ch) throw();
HRESULT Append(LPCOLESTR lpsz) throw();
HRESULT Append(LPCSTR lpsz) throw();
HRESULT Append(LPCOLESTR lpsz, int nLen) throw();

參數

bstrSrc
[in]要 CComBSTR 附加的物件。

ch
[in]要附加的字元。

lpsz
[in]要附加的零終止字元字串。 您可以透過 LPCOLESTR 多載傳遞 Unicode 字串,或透過 版本傳遞 LPCSTR ANSI 字串。

nLen
[in]要附加的字元 lpsz 數。

傳回值

S_OK 成功時,或任何標準 HRESULT 錯誤值。

備註

附加之前,ANSI 字串會轉換成 Unicode。

範例

enum { urlASP, urlHTM, urlISAPI } urlType;
urlType = urlASP;

CComBSTR bstrURL = OLESTR("http://SomeSite/");
CComBSTR bstrDEF = OLESTR("/OtherSite");
CComBSTR bstrASP = OLESTR("default.asp");

CComBSTR bstrTemp;
HRESULT hr;

switch (urlType)
{
case urlASP:
   // bstrURL is 'http://SomeSite/default.asp'
   hr = bstrURL.Append(bstrASP);
   break;

case urlHTM:
   // bstrURL is 'http://SomeSite/default.htm'
   hr = bstrURL.Append(OLESTR("default.htm"));
   break;

case urlISAPI:
   // bstrURL is 'http://SomeSite/default.dll?func'
   hr = bstrURL.Append(OLESTR("default.dll?func"));
   break;

default:
   // bstrTemp is 'http://'
   hr = bstrTemp.Append(bstrURL, 7);
   // bstrURL is 'http://OtherSite'
   if (hr == S_OK)
       hr = bstrTemp.Append(bstrDEF);
   bstrURL = bstrTemp;

   break;
}

CComBSTR::AppendBSTR

將指定的 BSTR 附加至 m_str

HRESULT AppendBSTR(BSTR p) throw();

參數

p
[in] BSTR 要附加的 。

傳回值

S_OK 成功時,或任何標準 HRESULT 錯誤值。

備註

請勿將一般寬字元字串傳遞至此方法。 編譯程式無法攔截錯誤,而且會發生運行時錯誤。

範例

CComBSTR bstrPre(OLESTR("Hello "));
CComBSTR bstrSuf(OLESTR("World!"));
HRESULT hr;

// Appends "World!" to "Hello "
hr = bstrPre.AppendBSTR(bstrSuf);

// Displays a message box with text "Hello World!"
::MessageBox(NULL, CW2CT(bstrPre), NULL, MB_OK);   

CComBSTR::AppendBytes

將指定的位元組數目附加至 m_str ,而不需要轉換。

HRESULT AppendBytes(const char* lpsz, int nLen) throw();

參數

lpsz
[in]要附加之位元組陣列的指標。

p
[in]要附加的位元組數目。

傳回值

S_OK 成功時,或任何標準 HRESULT 錯誤值。

範例

CComBSTR bstrPre(OLESTR("Hello "));
HRESULT hr;

// Appends "Wo" to "Hello " (4 bytes == 2 characters)
hr = bstrPre.AppendBytes(reinterpret_cast<char*>(OLESTR("World!")), 4);

// Displays a message box with text "Hello Wo"
::MessageBox(NULL, CW2CT(bstrPre), NULL, MB_OK);   

CComBSTR::ArrayToBSTR

釋放物件中 CComBSTR 保留的任何現有字串,然後從safearray中每個元素的第一個字元建立 BSTR ,並將它附加至 CComBSTR 物件。

HRESULT ArrayToBSTR(const SAFEARRAY* pSrc) throw();

參數

pSrc
[in]包含用來建立字串之專案的safearray。

傳回值

S_OK 成功時,或任何標準 HRESULT 錯誤值。

CComBSTR::AssignBSTR

BSTR 指定給 m_str

HRESULT AssignBSTR(const BSTR bstrSrc) throw();

參數

bstrSrc
[in]要指派給目前 CComBSTR 物件的 BSTR。

傳回值

S_OK 成功時,或任何標準 HRESULT 錯誤值。

CComBSTR::Attach

BSTR將 成員src設定為 ,將 m_str 附加至 CComBSTR 物件。

void Attach(BSTR src) throw();

參數

src
[in] BSTR 要附加至物件的 。

備註

請勿將一般寬字元字串傳遞至此方法。 編譯程式無法攔截錯誤,而且會發生運行時錯誤。

注意

如果 m_str 不是 NULL,這個方法會判斷提示。

範例

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

CComBSTR::BSTRToArray

建立以零起始的一維安全數位,其中陣列的每個元素都是 來自 CComBSTR 物件的字元。

HRESULT BSTRToArray(LPSAFEARRAY* ppArray) throw();

參數

ppArray
[out]用來保存函式結果之safearray的指標。

傳回值

S_OK 成功時,或任何標準 HRESULT 錯誤值。

CComBSTR::ByteLength

傳回 中的 m_str位元組數,不包括終止的 Null 字元。

unsigned int ByteLength() const throw();

傳回值

以位元組為單位的成員長度 m_str

備註

如果 m_strNULL,則傳回 0。

範例

// string with 11 chars (22 bytes)
CComBSTR bstrTemp(OLESTR("Hello World"));

unsigned int len = bstrTemp.ByteLength();

ATLASSERT(len == 22);   

CComBSTR::CComBSTR

建構函式。 預設建構函式會將 m_str 成員設定為 NULL

CComBSTR() throw();
CComBSTR(const CComBSTR& src);
CComBSTR(REFGUID  guid);
CComBSTR(int nSize);
CComBSTR(int nSize, LPCOLESTR sz);
CComBSTR(int nSize, LPCSTR sz);
CComBSTR(LPCOLESTR pSrc);
CComBSTR(LPCSTR pSrc);
CComBSTR(CComBSTR&& src) throw(); // (Visual Studio 2017)

參數

nSize
[in]要複製 sz 的字元數,或中字元 CComBSTR的初始大小。

sz
[in] 要複製的字串。 Unicode 版本會 LPCOLESTR指定 ;ANSI 版本指定 LPCSTR。

pSrc
[in] 要複製的字串。 Unicode 版本會 LPCOLESTR指定 ;ANSI 版本指定 LPCSTR。

src
[in] CComBSTR 物件。

guid
[in]結構的 GUID 參考。

備註

複製建構函式會將 設定 m_str 為的 BSTR 成員 src複本。 建 REFGUID 構函式會使用 StringFromGUID2 將 GUID 轉換成字串,並儲存結果。

其他建構函式會將 m_str 設為所指定字串的複本。 如果您傳遞的值 nSize,則只會 nSize 複製字元,後面接著終止的 Null 字元。

CComBSTR 支援移動語意。 您可以使用移動建構函式 (採用右值參考的建構函式 (&&)) 來建立新的物件,其使用的基本資料與您當作引數傳入之舊物件相同,無需額外複製物件。

解構函式會釋放 m_str 所指向的字串。

範例

CComBSTR bstr1;   // BSTR points to NULL
bstr1 = "Bye";    // initialize with assignment operator
                  // ANSI string is converted to wide char

OLECHAR* str = OLESTR("Bye bye!");  // wide char string of length 5
int len = (int)wcslen(str);
CComBSTR bstr2(len + 1);// unintialized BSTR of length 6
wcsncpy_s(bstr2.m_str, bstr2.Length(), str, len); // copy wide char string to BSTR

CComBSTR bstr3(5, OLESTR("Hello World")); // BSTR containing 'Hello', 
                                          // input string is wide char
CComBSTR bstr4(5, "Hello World");         // same as above, input string 
                                          // is ANSI

CComBSTR bstr5(OLESTR("Hey there")); // BSTR containing 'Hey there', 
                                     // input string is wide char
CComBSTR bstr6("Hey there");         // same as above, input string 
                                     // is ANSI

CComBSTR bstr7(bstr6);     // copy constructor, bstr7 contains 'Hey there'   

CComBSTR::~CComBSTR

解構函式。

~CComBSTR();

備註

解構函式會釋放 m_str 所指向的字串。

CComBSTR::Copy

配置並傳回的 m_str複本。

BSTR Copy() const throw();

傳回值

成員的 m_str 複本。 如果 m_strNULL,則會傳回 NULL

範例

CComBSTR m_bstrURL;    // BSTR representing a URL

// put_URL is the put method for the URL property. 
STDMETHOD(put_URL)(BSTR strURL)
{
    ATLTRACE(_T("put_URL\n"));

    // free existing string in m_bstrURL & make a copy 
    // of strURL pointed to by m_bstrURL
    m_bstrURL = strURL;
    return S_OK;
}

// get_URL is the get method for the URL property. 
STDMETHOD(get_URL)(BSTR* pstrURL)
{
    ATLTRACE(_T("get_URL\n"));

    // make a copy of m_bstrURL pointed to by pstrURL
    *pstrURL = m_bstrURL.Copy(); // See CComBSTR::CopyTo
    return S_OK;
}

CComBSTR::CopyTo

透過參數配置並傳回的複本 m_str

HRESULT CopyTo(BSTR* pbstr) throw();

HRESULT CopyTo(VARIANT* pvarDest) throw();

參數

pbstr
[out]要在其中傳回這個方法所配置之字串的 BSTR 位址。

pvarDest
[out]要在其中傳回這個方法所配置之字串的 VARIANT 位址。

傳回值

表示 HRESULT 複製成功或失敗的標準值。

備註

呼叫這個方法之後, VARIANTpvarDest 指向的 會是 型別 VT_BSTR

範例

CComBSTR m_bstrURL; // BSTR representing a URL

// get_URL is the get method for the URL property. 
STDMETHOD(get_URL)(BSTR* pstrURL)
{
   // Make a copy of m_bstrURL and return it via pstrURL
   return m_bstrURL.CopyTo(pstrURL);
}

CComBSTR::Detach

m_str從物件中斷連結,CComBSTR並將設定m_strNULL

BSTR Detach() throw();

傳回值

BSTRCComBSTR 物件相關聯的 。

範例

// Method which converts bstrIn to uppercase 
STDMETHODIMP BSTRToUpper(BSTR bstrIn, BSTR* pbstrOut)
{ 
    if (bstrIn == NULL || pbstrOut == NULL) 
        return E_POINTER; 

    // Create a temporary copy of bstrIn
    CComBSTR bstrTemp(bstrIn); 

    if (!bstrTemp) 
        return E_OUTOFMEMORY; 

    // Make string uppercase
    HRESULT hr;
    hr = bstrTemp.ToUpper();
    if (hr != S_OK)
        return hr;
    
    // Return m_str member of bstrTemp 
    *pbstrOut = bstrTemp.Detach(); 

    return S_OK; 
}

CComBSTR::Empty

釋放 m_str 成員。

void Empty() throw();

範例

CComBSTR bstr(OLESTR("abc"));

// Calls SysFreeString to free the BSTR
bstr.Empty();
ATLASSERT(bstr.Length() == 0);   

CComBSTR::Length

傳回 中的 m_str字元數,不包括終止的 Null 字元。

unsigned int Length() const throw();

傳回值

成員的 m_str 長度。

範例

// string with 11 chars
CComBSTR bstrTemp(OLESTR("Hello World"));

unsigned int len = bstrTemp.Length();

ATLASSERT(len == 11);

CComBSTR::LoadString

載入 所 nID 指定的字串資源,並將它儲存在此物件中。

bool LoadString(HINSTANCE hInst, UINT nID) throw();
bool LoadString(UINT nID) throw();

參數

請參閱 LoadString Windows SDK。

傳回值

如果成功載入字串,則傳 TRUE 回 ,否則傳 FALSE回 。

備註

第一個函式會透過 參數,從您 hInst 識別的模組載入資源。 第二個函式會從與 CComModule這個專案中使用的衍生對象相關聯的資源模組載入資源。

範例

CComBSTR bstrTemp;

// IDS_PROJNAME proj name stored as resource in string table
bstrTemp.LoadString(IDS_PROJNAME);

// the above is equivalent to:
// bstrTemp.LoadString(_Module.m_hInstResource, IDS_PROJNAME);

// display message box w/ proj name as title & text
::MessageBox(NULL, CW2CT(bstrTemp), CW2CT(bstrTemp), MB_OK);   

CComBSTR::m_str

包含 BSTRCComBSTR 物件相關聯的 。

BSTR m_str;

範例

CComBSTR GuidToBSTR(REFGUID guid) 
{
    // 39 - length of string representation of GUID + 1
    CComBSTR b(39); 

    // Convert GUID to BSTR
    // m_str member of CComBSTR is of type BSTR. When BSTR param 
    // is required, pass the m_str member explicitly or use implicit 
    // BSTR cast operator.
    int nRet = StringFromGUID2(guid, b.m_str, 39); 

    // Above equivalent to:
    // int nRet = StringFromGUID2(guid, b, 39); 
    // implicit BSTR cast operator used for 2nd param

    // Both lines are equivalent to:
    // CComBSTR b(guid);
    // CComBSTR constructor can convert GUIDs

    ATLASSERT(nRet); 
    return b; 
}

CComBSTR::operator BSTR

CComBSTR 物件轉換成 BSTR

operator BSTR() const throw();

備註

可讓您將物件傳遞 CComBSTR 至具有 [in] BSTR 參數的函式。

範例

請參閱 CComBSTR::m_str 的範例。

CComBSTR::operator !

檢查字串是否 BSTRNULL

bool operator!() const throw();

傳回值

如果成員為 NULL,則傳TRUE回 ,否則傳FALSEm_str回 。

備註

此運算子只會檢查 NULL 值,而不是空字串。

範例

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

CComBSTR::operator !=

傳回的 operator ==邏輯相反。

bool operator!= (const CComBSTR& bstrSrc) const throw();
bool operator!= (LPCOLESTR pszSrc) const;
bool operator!= (LPCSTR pszSrc) const;
bool operator!= (int nNull) const throw();

參數

bstrSrc
[in] CComBSTR 物件。

pszSrc
[in]以零結束的字串。

nNull
[in]必須是 NULL。

傳回值

如果比較的項目不等於 CComBSTR 物件,則傳TRUE回 ,否則傳FALSE回 。

備註

CComBSTR在使用者的預設地區設定內容中,會以文字方式比較 s。 最後一個比較運算符只會比較包含的字串與 NULL

CComBSTR::operator &

傳回儲存在成員中的 m_str 位址BSTR

BSTR* operator&() throw();

備註

CComBstr operator & 具有與其相關聯的特殊判斷提示,可協助識別記憶體流失。 程式會在初始化成員時 m_str 判斷提示。 已建立此判斷提示,以識別程序設計人員使用 & operator 將新值指派給 m_str 成員的情況,而不需要釋放 的第一個配置 m_str。 如果 m_str 等於 NULL,則程式會假設尚未配置m_str。 在此情況下,程式不會判斷提示。

預設不會啟用此判斷提示。 定義 ATL_CCOMBSTR_ADDRESS_OF_ASSERT 以啟用此判斷提示。

範例

#define ATL_NO_CCOMBSTR_ADDRESS_OF_ASSERT

void MyInitFunction(BSTR* pbstr)
{
   ::SysReAllocString(pbstr, OLESTR("Hello World"));
   return;
}
CComBSTR bstrStr ;
// bstrStr is not initialized so this call will not assert.
MyInitFunction(&bstrStr); 

CComBSTR bstrStr2(OLESTR("Hello World"));
// bstrStr2 is initialized so this call will assert.
::SysReAllocString(&bstrStr2, OLESTR("Bye"));

CComBSTR::operator +=

將字串附加至 CComBSTR 物件。

CComBSTR& operator+= (const CComBSTR& bstrSrc);
CComBSTR& operator+= (const LPCOLESTR pszSrc);

參數

bstrSrc
[in]要 CComBSTR 附加的物件。

pszSrc
[in]要附加的零終止字串。

備註

CComBSTR在使用者的預設地區設定內容中,會以文字方式比較 s。 比較 LPCOLESTR 每個 memcmp 字串中的原始資料上使用 。 建立的暫時 Unicode 複本pszSrc之後,會LPCSTR以相同的方式進行比較。 最後一個比較運算符只會比較包含的字串與 NULL

範例

CComBSTR bstrPre(OLESTR("Hello "));
CComBSTR bstrSuf(OLESTR("World!"));

// Appends "World!" to "Hello "
bstrPre += bstrSuf;

// Displays a message box with text "Hello World!"
::MessageBox(NULL, CW2CT(bstrPre), NULL, MB_OK);   

CComBSTR::operator <

CComBSTR比較與字串。

bool operator<(const CComBSTR& bstrSrc) const throw();
bool operator<(LPCOLESTR pszSrc) const throw();
bool operator<(LPCSTR pszSrc) const throw();

傳回值

如果比較的項目小於 物件,CComBSTR則傳TRUE回 ,否則傳FALSE回 。

備註

比較是使用使用者的預設地區設定來執行。

CComBSTR::operator =

m_str 成員設定為 的 pSrc 複本,或設定為 BSTR 的成員 src複本。 移動指派運算子會移動 src 而不複製它。

CComBSTR& operator= (const CComBSTR& src);
CComBSTR& operator= (LPCOLESTR pSrc);
CComBSTR& operator= (LPCSTR pSrc);
CComBSTR& operator= (CComBSTR&& src) throw(); // (Visual Studio 2017)

備註

參數 pSrcLPCOLESTR 針對 Unicode 版本或 LPCSTR ANSI 版本指定 。

範例

請參閱 CComBSTR::Copy 的範例。

CComBSTR::operator ==

CComBSTR比較與字串。 CComBSTR在使用者的預設地區設定內容中,會以文字方式比較 s。

bool operator== (const CComBSTR& bstrSrc) const throw();
bool operator== (LPCOLESTR pszSrc) const;
bool operator== (LPCSTR pszSrc) const;
bool operator== (int nNull) const throw();

參數

bstrSrc
[in] CComBSTR 物件。

pszSrc
[in]以零結束的字串。

nNull
[in]必須是 NULL

傳回值

如果比較的項目等於 CComBSTR 物件,則傳TRUE回 ,否則傳FALSE回 。

備註

最後一個比較運算符只會比較包含的字串與 NULL

CComBSTR::operator >

CComBSTR比較與字串。

bool operator>(const CComBSTR& bstrSrc) const throw();

傳回值

TRUE如果比較的項目大於物件,CComBSTR則傳回 ,否則傳FALSE回 。

備註

比較是使用使用者的預設地區設定來執行。

CComBSTR::ReadFromStream

m_str 成員設定為 BSTR 包含在指定資料流中的 。

HRESULT ReadFromStream(IStream* pStream) throw();

參數

pStream
[in]數據流上介面的 IStream 指標,其中包含數據。

傳回值

標準 HRESULT 值。

備註

ReadToStream 要求數據流在目前位置的內容與呼叫 WriteToStream所寫出的數據格式相容。

範例

IDataObject* pDataObj;

// Fill in the FORMATETC struct to retrieve desired format 
// from clipboard
FORMATETC formatetcIn = {CF_TEXT, NULL, DVASPECT_CONTENT, -1, TYMED_ISTREAM};
STGMEDIUM medium;
ZeroMemory(&medium, sizeof(STGMEDIUM));

// Get IDataObject from clipboard
HRESULT hr = ::OleGetClipboard(&pDataObj);

// Retrieve data from clipboard
hr = pDataObj->GetData(&formatetcIn, &medium);

if (SUCCEEDED(hr) && medium.tymed == TYMED_ISTREAM)
{
   CComBSTR bstrStr;
   // Get BSTR out of the stream
   hr = bstrStr.ReadFromStream(medium.pstm);

   //release the stream
   ::ReleaseStgMedium(&medium);
}

CComBSTR::ToLower

將包含的字串轉換成小寫。

HRESULT ToLower() throw();

傳回值

標準 HRESULT 值。

備註

如需如何執行轉換的詳細資訊,請參閱 CharLowerBuff

CComBSTR::ToUpper

將包含的字串轉換為大寫。

HRESULT ToUpper() throw();

傳回值

標準 HRESULT 值。

備註

如需如何執行轉換的詳細資訊,請參閱 CharUpperBuff

CComBSTR::WriteToStream

m_str 成員儲存至數據流。

HRESULT WriteToStream(IStream* pStream) throw();

參數

pStream
[in]數據流上介面的 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;
}

另請參閱

類別概觀
ATL 和 MFC 字串轉換宏