Partager via


La classe CComBSTR

Cette classe est un wrapper pour BSTRs.

Syntaxe

class CComBSTR

Membres

Constructeurs publics

Nom Description
CComBSTR::CComBSTR Constructeur .
CComBSTR::~CComBSTR Destructeur.

Méthodes publiques

Nom Description
CComBSTR::Append Ajoute une chaîne à m_str.
CComBSTR::AppendBSTR Ajoute un BSTR à m_str.
CComBSTR::AppendBytes Ajoute un nombre spécifié d’octets à m_str.
CComBSTR::ArrayToBSTR Crée un BSTR caractère à partir du premier caractère de chaque élément du safearray et l’attache à l’objet CComBSTR .
CComBSTR::AssignBSTR Affecte un BSTR à m_str.
CComBSTR::Attach Attache un BSTR à l’objet CComBSTR .
CComBSTR::BSTRToArray Crée un safearray unidimensionnel de base zéro, où chaque élément du tableau est un caractère de l’objet CComBSTR .
CComBSTR::ByteLength Retourne la longueur en m_str octets.
CComBSTR::Copy Retourne une copie de m_str.
CComBSTR::CopyTo Retourne une copie de m_str via un [out] paramètre
CComBSTR::Detach m_str Détache de l’objetCComBSTR.
CComBSTR::Empty Frees m_str.
CComBSTR::Length Retourne la longueur de m_str.
CComBSTR::LoadString Charge une ressource de chaîne.
CComBSTR::ReadFromStream Charge un BSTR objet à partir d’un flux.
CComBSTR::ToLower Convertit la chaîne en minuscules.
CComBSTR::ToUpper Convertit la chaîne en majuscules.
CComBSTR::WriteToStream Enregistre dans m_str un flux.

Opérateurs publics

Nom Description
CComBSTR::operator BSTR Convertit un CComBSTR objet en BSTR.
CComBSTR::operator ! Retourne TRUE ou FALSE, selon qu’il m_str s’agit de NULL.
CComBSTR::operator != Compare une CComBSTR chaîne à une chaîne.
CComBSTR::operator & Retourne l’adresse de m_str.
CComBSTR::operator += Ajoute un CComBSTR à l’objet.
CComBSTR::operator < Compare une CComBSTR chaîne à une chaîne.
CComBSTR::operator = Affecte une valeur à m_str.
CComBSTR::operator == Compare une CComBSTR chaîne à une chaîne.
CComBSTR::operator > Compare une CComBSTR chaîne à une chaîne.

Membres de données publics

Nom Description
CComBSTR::m_str Contient l’objet BSTR CComBSTR associé.

Notes

La CComBSTR classe est un wrapper pour BSTRs, qui sont des chaînes préfixées de longueur. La longueur est stockée sous la forme d’un entier à l’emplacement de mémoire précédant les données de la chaîne.

Une BSTR valeur null est terminée après le dernier caractère compté, mais peut également contenir des caractères Null incorporés dans la chaîne. La longueur de la chaîne est déterminée par le nombre de caractères, et non par le premier caractère Null.

Remarque

La CComBSTR classe fournit un certain nombre de membres (constructeurs, opérateurs d’affectation et opérateurs de comparaison) qui prennent des chaînes ANSI ou Unicode en tant qu’arguments. Les versions ANSI de ces fonctions sont moins efficaces que leurs équivalents Unicode, car les chaînes Unicode temporaires sont souvent créées en interne. Pour plus d’efficacité, utilisez les versions Unicode si possible.

Remarque

En raison du comportement de recherche amélioré implémenté dans Visual Studio .NET, le code tel que bstr = L"String2" + bstr;, qui peut avoir été compilé dans les versions précédentes, doit plutôt être implémenté en tant que bstr = CStringW(L"String2") + bstr.

Pour obtenir la liste des avertissements lors de l’utilisation CComBSTR, consultez Programmation avec CComBSTR.

Spécifications

En-tête: atlbase.h

CComBSTR::Append

Ajoute soit lpsz le membre BSTR de 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();

Paramètres

bstrSrc
[in] Objet CComBSTR à ajouter.

ch
[in] Caractère à ajouter.

lpsz
[in] Chaîne de caractères sans fin à ajouter. Vous pouvez passer une chaîne Unicode via la LPCOLESTR surcharge ou une chaîne ANSI via la LPCSTR version.

nLen
[in] Nombre de caractères à partir de lpsz l’ajout.

Valeur de retour

S_OK en cas de réussite ou toute valeur d’erreur standard HRESULT .

Notes

Une chaîne ANSI est convertie en Unicode avant d’être ajoutée.

Exemple

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

Ajoute le paramètre spécifié BSTR à m_str.

HRESULT AppendBSTR(BSTR p) throw();

Paramètres

p
[in] A BSTR à ajouter.

Valeur de retour

S_OK en cas de réussite ou toute valeur d’erreur standard HRESULT .

Notes

Ne passez pas une chaîne de caractères larges ordinaire à cette méthode. Le compilateur ne peut pas intercepter l’erreur et les erreurs d’exécution se produisent.

Exemple

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

Ajoute le nombre spécifié d’octets à m_str sans conversion.

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

Paramètres

lpsz
[in] Pointeur vers un tableau d’octets à ajouter.

p
[in] Nombre d’octets à ajouter.

Valeur de retour

S_OK en cas de réussite ou toute valeur d’erreur standard HRESULT .

Exemple

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

Libère toute chaîne existante contenue dans l’objet CComBSTR , puis crée un BSTR caractère à partir du premier caractère de chaque élément du safearray et l’attache à l’objet CComBSTR .

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

Paramètres

pSrc
[in] Coffre-fort contenant les éléments utilisés pour créer la chaîne.

Valeur de retour

S_OK en cas de réussite ou toute valeur d’erreur standard HRESULT .

CComBSTR::AssignBSTR

Affecte un BSTR à m_str.

HRESULT AssignBSTR(const BSTR bstrSrc) throw();

Paramètres

bstrSrc
[in] BSTR à affecter à l’objet actuel CComBSTR .

Valeur de retour

S_OK en cas de réussite ou toute valeur d’erreur standard HRESULT .

CComBSTR::Attach

Attache un BSTR objet en CComBSTR définissant le m_str membre sur src.

void Attach(BSTR src) throw();

Paramètres

src
[in] À BSTR attacher à l’objet.

Notes

Ne passez pas une chaîne de caractères larges ordinaire à cette méthode. Le compilateur ne peut pas intercepter l’erreur et les erreurs d’exécution se produisent.

Remarque

Cette méthode affirme si m_str elle n’a pas la valeur NULL.

Exemple

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

Crée un safearray unidimensionnel de base zéro, où chaque élément du tableau est un caractère de l’objet CComBSTR .

HRESULT BSTRToArray(LPSAFEARRAY* ppArray) throw();

Paramètres

ppArray
[out] Pointeur vers le safearray utilisé pour contenir les résultats de la fonction.

Valeur de retour

S_OK en cas de réussite ou toute valeur d’erreur standard HRESULT .

CComBSTR::ByteLength

Retourne le nombre d’octets dans m_str, à l’exclusion du caractère null de fin.

unsigned int ByteLength() const throw();

Valeur de retour

Longueur du m_str membre en octets.

Notes

Retourne 0 si m_str c’est NULL.

Exemple

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

unsigned int len = bstrTemp.ByteLength();

ATLASSERT(len == 22);   

CComBSTR::CComBSTR

Constructeur . Le constructeur par défaut définit le m_str membre sur 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)

Paramètres

nSize
[in] Nombre de caractères à copier à partir ou à partir de sz la taille initiale des caractères pour le CComBSTR.

sz
[in] Chaîne à copier. La version Unicode spécifie un LPCOLESTR; la version ANSI spécifie une LPCSTR.

pSrc
[in] Chaîne à copier. La version Unicode spécifie un LPCOLESTR; la version ANSI spécifie une LPCSTR.

src
[in] Objet CComBSTR

guid
[in] Référence à une GUID structure.

Notes

Le constructeur de copie définit m_str une copie du membre BSTR de src. Le REFGUID constructeur convertit le GUID en chaîne à l’aide StringFromGUID2 et stocke le résultat.

Les autres constructeurs affectent à m_str une copie de la chaîne spécifiée. Si vous passez une valeur pour nSize, seuls les nSize caractères seront copiés, suivis d’un caractère null de fin.

CComBSTR prend en charge la sémantique de déplacement. Vous pouvez utiliser le constructeur de déplacement (constructeur qui accepte une référence rvalue (&&)) pour créer un nouvel objet utilisant les mêmes données sous-jacentes que l'ancien objet que vous transmettez comme argument, sans la surcharge propre à la copie de l'objet.

Le destructeur libère la chaîne pointée par m_str.

Exemple

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

Destructeur.

~CComBSTR();

Notes

Le destructeur libère la chaîne pointée par m_str.

CComBSTR::Copy

Alloue et retourne une copie de m_str.

BSTR Copy() const throw();

Valeur de retour

Copie du m_str membre. Si m_str a la valeur NULL, retourne NULL.

Exemple

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

Alloue et retourne une copie du m_str paramètre.

HRESULT CopyTo(BSTR* pbstr) throw();

HRESULT CopyTo(VARIANT* pvarDest) throw();

Paramètres

pbstr
[out] Adresse d’un BSTR élément dans lequel retourner la chaîne allouée par cette méthode.

pvarDest
[out] Adresse d’un VARIANT élément dans lequel retourner la chaîne allouée par cette méthode.

Valeur de retour

Valeur standard HRESULT indiquant la réussite ou l’échec de la copie.

Notes

Après avoir appelé cette méthode, le VARIANT pointé par pvarDest sera de type VT_BSTR.

Exemple

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 Détache de l’objet CComBSTR et définit m_str sur NULL.

BSTR Detach() throw();

Valeur de retour

BSTR associé à l'objet CComBSTR.

Exemple

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

Libère le m_str membre.

void Empty() throw();

Exemple

CComBSTR bstr(OLESTR("abc"));

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

CComBSTR::Length

Retourne le nombre de caractères dans m_str, à l’exclusion du caractère null de fin.

unsigned int Length() const throw();

Valeur de retour

Longueur du m_str membre.

Exemple

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

unsigned int len = bstrTemp.Length();

ATLASSERT(len == 11);

CComBSTR::LoadString

Charge une ressource de chaîne spécifiée par nID et la stocke dans cet objet.

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

Paramètres

Consultez LoadString le Kit de développement logiciel (SDK) Windows.

Valeur de retour

Retourne TRUE si la chaîne est correctement chargée ; sinon, retourne FALSE.

Notes

La première fonction charge la ressource à partir du module identifié par vous via le hInst paramètre. La deuxième fonction charge la ressource à partir du module de ressource associé à l’objet CComModuledérivé utilisé dans ce projet.

Exemple

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

Contient l’objet BSTR CComBSTR associé.

BSTR m_str;

Exemple

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

Convertit un CComBSTR objet en BSTRun .

operator BSTR() const throw();

Notes

Vous permet de passer des objets à des CComBSTR fonctions qui ont des paramètres [in]. BSTR

Exemple

Consultez l’exemple pour CComBSTR::m_str.

CComBSTR::operator !

Vérifie si BSTR la chaîne est NULL.

bool operator!() const throw();

Valeur de retour

Retourne TRUE si le m_str membre est NULL; sinon, FALSE.

Notes

Cet opérateur recherche uniquement une NULL valeur, et non une chaîne vide.

Exemple

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

Retourne l’inverse logique de operator ==.

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

Paramètres

bstrSrc
[in] Objet CComBSTR

pszSrc
[in] Chaîne terminée par zéro.

nNull
[in] Doit être NULL.

Valeur de retour

Retourne TRUE si l’élément comparé n’est pas égal à l’objet CComBSTR ; sinon, retourne FALSE.

Notes

CComBSTRsont comparés textuellement dans le contexte des paramètres régionaux par défaut de l’utilisateur. L’opérateur de comparaison final compare simplement la chaîne contenue par rapport NULLà .

CComBSTR::operator &

Retourne l’adresse du BSTR stocké dans le m_str membre.

BSTR* operator&() throw();

Notes

CComBstr operator & a une assertion spéciale associée pour aider à identifier les fuites de mémoire. Le programme s’affirme lorsque le m_str membre est initialisé. Cette assertion a été créée pour identifier les situations où un programmeur utilise la & operator méthode pour affecter une nouvelle valeur à m_str un membre sans libérer la première allocation de m_str. Si m_str elle est NULLégale, le programme part du principe que m_str n’a pas encore été alloué. Dans ce cas, le programme n’affirme pas.

Cette assertion n’est pas activée par défaut. Définissez ATL_CCOMBSTR_ADDRESS_OF_ASSERT pour activer cette assertion.

Exemple

#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 +=

Ajoute une chaîne à l’objet CComBSTR .

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

Paramètres

bstrSrc
[in] Objet CComBSTR à ajouter.

pszSrc
[in] Chaîne terminée par zéro à ajouter.

Notes

CComBSTRsont comparés textuellement dans le contexte des paramètres régionaux par défaut de l’utilisateur. La LPCOLESTR comparaison est effectuée à l’aide memcmp des données brutes de chaque chaîne. La LPCSTR comparaison est effectuée de la même façon une fois qu’une copie pszSrc Unicode temporaire a été créée. L’opérateur de comparaison final compare simplement la chaîne contenue par rapport NULLà .

Exemple

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 <

Compare une CComBSTR chaîne à une chaîne.

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

Valeur de retour

Retourne TRUE si l’élément comparé est inférieur à l’objet CComBSTR ; sinon, retourne FALSE.

Notes

La comparaison est effectuée à l’aide des paramètres régionaux par défaut de l’utilisateur.

CComBSTR::operator =

Définit le m_str membre sur une copie ou pSrc une copie du BSTR membre de src. L’opérateur d’affectation de déplacement se déplace src sans le copier.

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

Notes

Le pSrc paramètre spécifie une LPCOLESTR version Unicode ou LPCSTR pour les versions ANSI.

Exemple

Consultez l’exemple pour CComBSTR::Copy.

CComBSTR::operator ==

Compare une CComBSTR chaîne à une chaîne. CComBSTRsont comparés textuellement dans le contexte des paramètres régionaux par défaut de l’utilisateur.

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

Paramètres

bstrSrc
[in] Objet CComBSTR

pszSrc
[in] Chaîne terminée par zéro.

nNull
[in] Doit être NULL.

Valeur de retour

Retourne TRUE si l’élément comparé est égal à l’objet CComBSTR ; sinon, retourne FALSE.

Notes

L’opérateur de comparaison final compare simplement la chaîne contenue par rapport NULLà .

CComBSTR::operator >

Compare une CComBSTR chaîne à une chaîne.

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

Valeur de retour

Retourne TRUE si l’élément comparé est supérieur à l’objet CComBSTR ; sinon, retourne FALSE.

Notes

La comparaison est effectuée à l’aide des paramètres régionaux par défaut de l’utilisateur.

CComBSTR::ReadFromStream

Définit le m_str membre sur le BSTR contenu du flux spécifié.

HRESULT ReadFromStream(IStream* pStream) throw();

Paramètres

pStream
[in] Pointeur vers l’interface IStream sur le flux contenant les données.

Valeur de retour

Valeur HRESULT standard.

Notes

ReadToStream nécessite que le contenu du flux à la position actuelle soit compatible avec le format de données écrit par un appel à WriteToStream.

Exemple

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

Convertit la chaîne contenue en minuscules.

HRESULT ToLower() throw();

Valeur de retour

Valeur HRESULT standard.

Notes

Pour CharLowerBuff plus d’informations sur la façon dont la conversion est effectuée.

CComBSTR::ToUpper

Convertit la chaîne contenue en majuscules.

HRESULT ToUpper() throw();

Valeur de retour

Valeur HRESULT standard.

Notes

Pour CharUpperBuff plus d’informations sur la façon dont la conversion est effectuée.

CComBSTR::WriteToStream

Enregistre le m_str membre dans un flux.

HRESULT WriteToStream(IStream* pStream) throw();

Paramètres

pStream
[in] Pointeur vers l’interface IStream sur un flux.

Valeur de retour

Valeur HRESULT standard.

Notes

Vous pouvez recréer un BSTR contenu du flux à l’aide de la ReadFromStream fonction.

Exemple

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

Voir aussi

Vue d’ensemble de la classe
Macros de conversion de chaînes ATL et MFC