Board index » cppbuilder » Returning BSTR*

Returning BSTR*

How do I return a BSTR*?  Is the following correct?

STDMETHODIMP TCOMAppQueryImpl::GetStatus(BSTR* xml)
{
   AnsiString xml = "<p>Hello</p>";
   WideString wsXML( xml );
   BSTR bstr = wsXML.c_bstr();
   xml = &bstr;

Quote
}

 

Re:Returning BSTR*


No, you should do this instead:

    STDMETHODIMP TCOMAppQueryImpl::GetStatus(BSTR* xml)
    {
        *xml = WideString("<p>Hello</p>").Detach();
    }

Gambit

Quote
"Mike King" <emai...@e{*word*277}.com> wrote in message news:3ca8db21$1_2@dnews...
> How do I return a BSTR*?  Is the following correct?

Re:Returning BSTR*


This will cause problems, because you have not transferred OWNERSHIP to the
calling program. Therefore, when you return the address of the LOCAL bstr,
it could get overwritten by that local component (possibly causing the
crash).

The solution posted by Remy transfers ownership (using the Detach()) method,
giving that memory space over to the calling method. It is now that calling
method's responsibility to free that memory when it's done with the BSTR.

John

Quote
"Mike King" <emai...@e{*word*277}.com> wrote in message news:3ca8db21$1_2@dnews...
> How do I return a BSTR*?  Is the following correct?

> STDMETHODIMP TCOMAppQueryImpl::GetStatus(BSTR* xml)
> {
>    AnsiString xml = "<p>Hello</p>";
>    WideString wsXML( xml );
>    BSTR bstr = wsXML.c_bstr();
>    xml = &bstr;
> }

Other Threads