Reputation: 13882
I'm trying to convert a _bstr_t object to a char array. More specifically I've got a preallocated char array, which size I know and I want to copy the _bstr_t in there.
For a while my code looked like this:
bool ReadCharPtr(_bstr_t const& input, char* output, unsigned int max_len)
{
// THIS CODE IS WRONG !
const char * temp (input );
return ::strcpy_s(output, max_len, temp) == 0;
}
But... I'm not sure and it's not clear in the windows documentation, what happens when I call operator char*() const
. Is something allocated in the heap, that I have to deallocate later ?
Is there a way to avoid this step, and actually convert the _bstr_t in character into the preallocated array ? That would avoid allocating chars on the heap for nothing...
Upvotes: 0
Views: 1827
Reputation: 5331
No. This is not required. The documentation says that " The operators return the pointer to the actual internal buffer, so the resulting string cannot be modified." which would imply that _bstr_t takes care of the deletion.
Additionally you could check the implementation of _bstr_t :
In MSVC 2010 :
_bstr_t uses _com_util::ConvertBSTRToString() to get the character pointer from the BSTR. _com_util::ConvertBSTRToString() documentation says that this needs to be deleted by the callers. However, _bstr_t caches this pointer and deletes in the destructor.
Upvotes: 1