Can anyone explain the difference between the types mentioned above and some sample usage to clearly explain the difference between the two?
Any help would be highly appreciated! Note: this question is a spin-off from this other question
BSTR
is the string data type used with COM.
_bstr_t
is a wrapper class that works like a smart pointer, so it will free the allocated memory when the variable is destroyed or goes out of scope.
_bstr_t
also has reference counting, which increases every time you pass the _bstr_t
variable by value (avoiding unnecessary copy) and decrement when it is no longer used. Whenever all references are destroyed, the allocated memory for the string is freed.
An alternative to BSTR
is the CComBSTR
. It also manages the memory for the BSTR
, but has no reference counting.