Search code examples
c++bstr

Using _bstr_t to pass parameter of type BSTR* in function


What is the correct way of doing this:

_bstr_t description;
errorInfo->GetDescription( &description.GetBSTR() );

or:

_bstr_t description;
errorInfo->GetDescription( description.GetAddress() );

Where IError:GetDescription is defined as:

HRESULT GetDescription (BSTR *pbstrDescription);

I know I could easily do this:

BSTR description= SysAllocString (L"Whateva"));
errorInfo->GetDescription (&description);
SysFreeString (description);

Thanks


Solution

  • The BSTR is reference counted, I seriously doubt that will work right if you use GetAddress(). Sadly the source code isn't available to double-check that. I've always done it like this:

    BSTR temp = 0;
    HRESULT hr = p->GetDescription(&temp);
    if (SUCCEEDED(hr)) {
        _bstr_t wrap(temp, FALSE);
        // etc..
    }