Re: How to pass a reference to a CComBSTR
- From: "Heinz Ozwirk" <hozwirk.SPAM@xxxxxxxx>
- Date: Sun, 31 Jul 2005 18:53:25 +0200
"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
.
- References:
- How to pass a reference to a CComBSTR
- From: Steve Franks
- Re: How to pass a reference to a CComBSTR
- From: Heinz Ozwirk
- Re: How to pass a reference to a CComBSTR
- From: Egbert Nierop \(MVP for IIS\)
- Re: How to pass a reference to a CComBSTR
- From: Steve Franks
- How to pass a reference to a CComBSTR
- Prev by Date: Re: How to pass a reference to a CComBSTR
- Next by Date: Re: How to pass a reference to a CComBSTR
- Previous by thread: Re: How to pass a reference to a CComBSTR
- Next by thread: Re: How to pass a reference to a CComBSTR
- Index(es):
Relevant Pages
|
Loading