CComBSTR クラス

このクラスは、BSTR のラッパーです。

構文

class CComBSTR

メンバー

パブリック コンストラクター

名前 説明
CComBSTR::CComBSTR コンストラクター。
CComBSTR::~CComBSTR デストラクター。

パブリック メソッド

名前 説明
CComBSTR::Append m_str に文字列を追加します。
CComBSTR::AppendBSTR BSTRm_str に追加します。
CComBSTR::AppendBytes m_str に指定されたバイト数を追加します。
CComBSTR::ArrayToBSTR safearray 内の各要素の最初の文字から BSTR を作成し、それを CComBSTR オブジェクトにアタッチします。
CComBSTR::AssignBSTR BSTRm_str に代入します。
CComBSTR::Attach BSTRCComBSTR オブジェクトにアタッチします。
CComBSTR::BSTRToArray 0 から始まる 1 次元の safearray を作成します。この場合、配列の各要素は CComBSTR オブジェクトの文字です。
CComBSTR::ByteLength m_str の長さをバイト単位で返します。
CComBSTR::Copy m_str のコピーを返します。
CComBSTR::CopyTo [out] パラメーターを使用して、m_str のコピーを返します
CComBSTR::Detach CComBSTR オブジェクトから m_str をデタッチします。
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 CComBSTR オブジェクトに関連付けられた BSTR を格納します。

解説

CComBSTR クラスは、長さを示すプレフィックスを付けた文字列である BSTR のラッパーです。 長さは、文字列のデータの前のメモリ位置に整数として格納されます。

BSTR は、最後にカウントされた文字の後に null で終わりますが、文字列内に null 文字が埋め込まれている場合もあります。 文字列の長さは、最初の null 文字ではなく、文字数によって決まります。

Note

CComBSTR クラスには、引数として ANSI 文字列または Unicode 文字列を取る多数のメンバー (コンストラクター、代入演算子、および比較演算子) が用意されています。 一時的な Unicode 文字列は多くの場合、内部的に作成されるため、これらの関数の ANSI バージョンは、Unicode の対応する関数よりも効率が悪くなります。 効率を上げるには、可能な限り Unicode バージョンを使用します。

Note

Visual Studio .NET に実装された検索動作が向上しているため、以前のリリースでコンパイルされた可能性のある bstr = L"String2" + bstr; などのコードは、代わりに bstr = CStringW(L"String2") + bstr として実装する必要があります。

CComBSTR を使用する場合の注意事項の一覧については、CComBSTR を使用したプログラミングに関するページを参照してください。

必要条件

ヘッダー:atlbase.h

CComBSTR::Append

lpsz または bstrSrc の BSTR メンバーを 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
[入力] 追加する CComBSTR オブジェクト。

ch
[入力] 追加する文字。

lpsz
[入力] 追加する 0 で終わる文字列。 LPCOLESTR オーバーロードを使用して Unicode 文字列を渡すことも、LPCSTR バージョンを使用して ANSI 文字列を渡すこともできます。

nLen
[入力] 追加する 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

指定された BSTRm_str に追加します。

HRESULT AppendBSTR(BSTR p) throw();

パラメーター

p
[入力] 追加する 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
[入力] 追加するバイト配列へのポインター。

p
[入力] 追加するバイト数。

戻り値

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
[入力] 文字列の作成に使用される要素を含む safearray。

戻り値

S_OK (成功した場合) または標準 HRESULT エラー値。

CComBSTR::AssignBSTR

BSTRm_str に代入します。

HRESULT AssignBSTR(const BSTR bstrSrc) throw();

パラメーター

bstrSrc
[入力] 現在の CComBSTR オブジェクトに代入する BSTR。

戻り値

S_OK (成功した場合) または標準 HRESULT エラー値。

CComBSTR::Attach

m_str メンバーを src に設定して、BSTRCComBSTR オブジェクトにアタッチします。

void Attach(BSTR src) throw();

パラメーター

src
[入力] オブジェクトにアタッチする BSTR

解説

通常のワイド文字列をこのメソッドに渡さないでください。 コンパイラではエラーをキャッチできず、実行時エラーが発生します。

Note

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

0 から始まる 1 次元の safearray を作成します。この場合、配列の各要素は CComBSTR オブジェクトの文字です。

HRESULT BSTRToArray(LPSAFEARRAY* ppArray) throw();

パラメーター

ppArray
[出力] 関数の結果を保持するために使用される safearray へのポインター。

戻り値

S_OK (成功した場合) または標準 HRESULT エラー値。

CComBSTR::ByteLength

終端の null 文字を除き、m_str のバイト数を返します。

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
[入力] sz からコピーする文字数、または CComBSTR の文字の初期サイズ。

sz
[入力] コピーする文字列。 Unicode バージョンでは LPCOLESTR を指定し、ANSI バージョンでは LPCSTR を指定します。

pSrc
[入力] コピーする文字列。 Unicode バージョンでは LPCOLESTR を指定し、ANSI バージョンでは LPCSTR を指定します。

src
[入力] CComBSTR オブジェクト。

guid
[入力] GUID 構造体への参照。

解説

コピー コンストラクターにより、m_strsrc の BSTR メンバーのコピーに設定されます。 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
[出力] このメソッドによって割り当てられた文字列を返す BSTR のアドレス。

pvarDest
[出力] このメソッドによって割り当てられた文字列を返す VARIANT のアドレス。

戻り値

コピーの成功または失敗を示す標準 HRESULT 値。

解説

このメソッドを呼び出した後、pvarDest によって指し示される VARIANTVT_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

CComBSTR オブジェクトから m_str をデタッチし、m_strNULL に設定します。

BSTR Detach() throw();

戻り値

BSTR オブジェクトに関連付けられている CComBSTR です。

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

終端の null 文字を除き、m_str の文字数を返します。

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();

パラメーター

Windows SDK の LoadString に関するページを参照してください。

戻り値

文字列が正常に読み込まれた場合は TRUE を返し、それ以外の場合は FALSE を返します。

解説

最初の関数では、hInst パラメーターを使用して、ユーザーによって識別されたモジュールからリソースを読み込みます。 2 番目の関数では、このプロジェクトで使用される 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

CComBSTR オブジェクトに関連付けられた BSTR を格納します。

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();

解説

[in] BSTR パラメーターを持つ関数に CComBSTR オブジェクトを渡すことができます。

CComBSTR::m_str の例を参照してください。

CComBSTR::operator !

BSTR 文字列が NULL であるかどうかを確認します。

bool operator!() const throw();

戻り値

m_str メンバーが NULL の場合は TRUE、それ以外の場合は FALSE を返します。

解説

この演算子では、空の文字列ではなく、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
[入力] CComBSTR オブジェクト。

pszSrc
[入力] 0 で終わる文字列。

nNull
[入力] NULL である必要があります。

戻り値

比較対象の項目が CComBSTR オブジェクトと等しくない場合は TRUE を返します。それ以外の場合は FALSE を返します。

解説

CComBSTR は、ユーザーの既定のロケールのコンテキストでテキスト形式で比較されます。 最後の比較演算子では単に、含まれている文字列を NULL と比較します。

CComBSTR::operator &

m_str メンバーに格納されている BSTR のアドレスを返します。

BSTR* operator&() throw();

解説

CComBstr operator & には、メモリ リークの識別に役立つように関連付けられた特別なアサーションがあります。 m_str メンバーが初期化されると、プログラムによってアサートされます。 このアサーションは、プログラマが & operator を使用して、m_str の最初の割り当てを解放せずに新しい値を m_str メンバーに代入する状況を識別するために作成されたものです。 m_strNULL に等しい場合、プログラムでは 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
[入力] 追加する CComBSTR オブジェクト。

pszSrc
[入力] 追加する 0 で終わる文字列。

解説

CComBSTR は、ユーザーの既定のロケールのコンテキストでテキスト形式で比較されます。 LPCOLESTR 比較は、各文字列の生データに対して memcmp を使用して行われます。 pszSrc の一時的な Unicode コピーが作成されると、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 のコピー、または srcBSTR メンバーのコピーに設定します。 移動代入演算子では、src をコピーせずに移動します。

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

解説

pSrc パラメーターでは、LPCOLESTR (Unicode バージョンの場合) または LPCSTR (ANSI バージョンの場合) を指定します。

CComBSTR::Copy の例を参照してください。

CComBSTR::operator ==

CComBSTR と文字列を比較します。 CComBSTR は、ユーザーの既定のロケールのコンテキストでテキスト形式で比較されます。

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

パラメーター

bstrSrc
[入力] CComBSTR オブジェクト。

pszSrc
[入力] 0 で終わる文字列。

nNull
[入力] NULL である必要があります。

戻り値

比較対象の項目が CComBSTR オブジェクトと等しい場合は TRUE を返します。それ以外の場合は FALSE を返します。

解説

最後の比較演算子では単に、含まれている文字列を NULL と比較します。

CComBSTR::operator >

CComBSTR と文字列を比較します。

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

戻り値

比較対象の項目が CComBSTR オブジェクトより大きい場合は TRUE を返します。それ以外の場合は FALSE を返します。

解説

比較は、ユーザーの既定のロケールを使用して実行されます。

CComBSTR::ReadFromStream

m_str メンバーを、指定されたストリームに含まれている BSTR に設定します。

HRESULT ReadFromStream(IStream* pStream) throw();

パラメーター

pStream
[入力] データを含むストリームの 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
[入力] ストリームの 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 の文字列変換マクロ