Re: How to pass a reference to a CComBSTR



"Steve Franks" <please@xxxxxxxxxxxxxxxxx> schrieb im Newsbeitrag
news:laydnTQm3rtDcHHfRVn-hw@xxxxxxxxxxxxxx
>>>> I'm passing in my CComBSTR like this: myfunction(& myCComBSTRvar)
>>>> and myfunction looks like this:
>>>> void myfunction(CComBSTR *myCComBSTRvarToUpdate) {}
>>>
>>> This is not passing by reference - you are passing pointers. To pass by
>>> reference yous should declare myfunction as
>>>
>>> void myfunction(CComBSTR& myModifiableVariable)...
>>>
>>> and call it like
>>>
>>> myfunction(myCComBSTRvar);
>>>
>
> Very interesting. Is there any performance difference (or any practical
> difference for that matter) between passing pointers vs. passing by
> reference?
>
> It looks like I have two approaches that both work. I am wondering which
> one is better (faster performing and safer):
>
> Approach A (passing by reference)
>
> define function to pass by reference:
> void myfunction(CComBSTR& myModifiableVariable)
> and call like this:
> myfunction(myCComBSTRvar);
> and set the "return" (out) result like this at the end of myfunction:
> CComBSTR tempCComBSTR(mySTLString.c_str());
> myModifiableVariable = tempCComBSTR.Detach();

I wouldn't use Detach to assign one CComBSTR to another one. CComBSTR has an
assignment operator and that works fine. And don't argue Detach might be
slightly faster. If you want speed that badly, you have to use plain BSTR.

> Approach B (passing by pointer)
> define function to pass by pointer:
> void myfunction(BSTR* myModifiableVariable) //cannot use CComBSTR in the
> declaration but BSTR works
> and call like this:
> myfunction( & myCComBSTRvar);
> and set the "return" (out) result like this at the end of myfunction:
> CComBSTR tempCComBSTR(mySTLString.c_str());
> *myModifiableVariable = tempCComBSTR.Detach();

This will cause a memory leak. Before you can assign a new string to a BSTR
you must release the destinations previous value using ::SysFreeString

> On a related note, is it perfectly safe to pass a pointer to a CComBSTR
> around as a BSTR, like shown in Approach B?

Approach B shows that it is not safe.

> One final question if you will. What is the best way to convert a string
> contained in an STL "string" variable type to a CComBSTR? For example,
> currently I do this:
> string mySTLString = "Hello";
> CComBSTR tempCComBSTR(mySTLString.c_str());
> myModifiableVariable = tempCComBSTR.Detach();

myModifiableVariable = mySTLString.c_str();

> Is that an efficient way, or is there a better way?

It is very inefficient to assign an std::string to any flavour of BSTR.
std::string uses 8 bit (ANSI) characters whereas BSTR (as well as CComBSTR
and _bstr_t) uses 16 bit (unicode) characters. Assiging one to the other
always needs to convert the entire string. That doesn't only take time, it
can also drop some characters that cannot be represented in the other
character set.

Heinz


.



Relevant Pages

  • Re: How to pass a reference to a CComBSTR
    ... From searching I've read that you cannot pass a CComBSTR by reference because it overloads the & operator. ... Clearly I could pass my CComBSTR by value and return a string, and then once it gets back I could just set the CComBSTR to the returned string value. ... However this is a very critical piece of the code where performance is critical and this code is called intensively so performance optimization matters the most here. ...
    (microsoft.public.vc.atl)
  • Re: BSTR to int
    ... CComBSTR is a class wrapper around BSTR. ... a string class like stl::wstring for internal string manipulation. ...
    (microsoft.public.vc.atl)
  • How to pass a reference to a CComBSTR
    ... I'm passing in my CComBSTR like this: ... >From searching I've read that you cannot pass a CComBSTR by reference ... Clearly I could pass my CComBSTR by value and return a string, ... critical and this code is called intensively so performance optimization ...
    (microsoft.public.vc.atl)
  • Re: How to use BSTR?
    ... string. ... But, by all means, for *get*, you'll want to use CComBSTR. ... > Hi Bob, ... > the BSTR returned by s.AllocSysString? ...
    (microsoft.public.vc.mfc)
  • Re: Pass String by Reference to ATL
    ... If MyMethod takes BSTR* it means that strings will be passed by pointer. ... you mean passing string pointer by reference, ... This is useful when you want to change the string ...
    (microsoft.public.vc.language)

Loading