Board index » cppbuilder » Returning BSTR for an automation server dispinterface function

Returning BSTR for an automation server dispinterface function

I have an automation server dispinterface ( not a dual interface ) and I
want to return a BSTR. My current method is to return a WideString,
since this has an operator BSTR. Is this acceptable and will this work
properly ? Is it the automation controller's responsibility to free the
BSTR after it is finished with it ?

 

Re:Returning BSTR for an automation server dispinterface function


Edward,

The WideString owns the underlying BSTR and it will destroy it when it is
destroyed. If the BSTR you're returning is an [out] or [out, retval]
parameter, you'll want to return either WideStringInstance.Detach() or
WideStringInstance.Copy(). In those cases, the Controller is indeed
responsible for freeing the BSTR.

Thanks,

Bruneau.

Quote
Edward Diener wrote:
> I have an automation server dispinterface ( not a dual interface ) and I
> want to return a BSTR. My current method is to return a WideString,
> since this has an operator BSTR. Is this acceptable and will this work
> properly ? Is it the automation controller's responsibility to free the
> BSTR after it is finished with it ?

Re:Returning BSTR for an automation server dispinterface function


I'm just returning a WideString which is local to my function in a
dispinterface method, ie.

BSTR MyAutomationFunction()
{

WideString ret;

...

return(ret);

Quote
}
Bruneau Babet wrote:
> Edward,

> The WideString owns the underlying BSTR and it will destroy it when it is
> destroyed. If the BSTR you're returning is an [out] or [out, retval]
> parameter, you'll want to return either WideStringInstance.Detach() or
> WideStringInstance.Copy(). In those cases, the Controller is indeed
> responsible for freeing the BSTR.

> Thanks,

> Bruneau.

> Edward Diener wrote:

> > I have an automation server dispinterface ( not a dual interface ) and I
> > want to return a BSTR. My current method is to return a WideString,
> > since this has an operator BSTR. Is this acceptable and will this work
> > properly ? Is it the automation controller's responsibility to free the
> > BSTR after it is finished with it ?

Re:Returning BSTR for an automation server dispinterface function


Edward,

I would recommend the following instead:

BSTR Method()
{
  WideString ret;

   return ret.Detach();

Quote
}

The BSTR is going back in a VARIANT. When the latter is cleared, the BSTR will
be freed. So, you don't want the WideString's destructor to free the BSTR that
you're returning [and it will unless you call Detach()].

Thanks,

Bruneau.

Quote
Edward Diener wrote:
> I'm just returning a WideString which is local to my function in a
> dispinterface method, ie.

> BSTR MyAutomationFunction()
> {

> WideString ret;

> ...

> return(ret);

> }

> Bruneau Babet wrote:

> > Edward,

> > The WideString owns the underlying BSTR and it will destroy it when it is
> > destroyed. If the BSTR you're returning is an [out] or [out, retval]
> > parameter, you'll want to return either WideStringInstance.Detach() or
> > WideStringInstance.Copy(). In those cases, the Controller is indeed
> > responsible for freeing the BSTR.

> > Thanks,

> > Bruneau.

> > Edward Diener wrote:

> > > I have an automation server dispinterface ( not a dual interface ) and I
> > > want to return a BSTR. My current method is to return a WideString,
> > > since this has an operator BSTR. Is this acceptable and will this work
> > > properly ? Is it the automation controller's responsibility to free the
> > > BSTR after it is finished with it ?

Re:Returning BSTR for an automation server dispinterface function


Thanks ! Much appreciated.

Quote
Bruneau Babet wrote:
> Edward,

> I would recommend the following instead:

> BSTR Method()
> {
>   WideString ret;

>    return ret.Detach();
> }

> The BSTR is going back in a VARIANT. When the latter is cleared, the BSTR will
> be freed. So, you don't want the WideString's destructor to free the BSTR that
> you're returning [and it will unless you call Detach()].

> Thanks,

> Bruneau.

> Edward Diener wrote:

> > I'm just returning a WideString which is local to my function in a
> > dispinterface method, ie.

> > BSTR MyAutomationFunction()
> > {

> > WideString ret;

> > ...

> > return(ret);

> > }

> > Bruneau Babet wrote:

> > > Edward,

> > > The WideString owns the underlying BSTR and it will destroy it when it is
> > > destroyed. If the BSTR you're returning is an [out] or [out, retval]
> > > parameter, you'll want to return either WideStringInstance.Detach() or
> > > WideStringInstance.Copy(). In those cases, the Controller is indeed
> > > responsible for freeing the BSTR.

> > > Thanks,

> > > Bruneau.

> > > Edward Diener wrote:

> > > > I have an automation server dispinterface ( not a dual interface ) and I
> > > > want to return a BSTR. My current method is to return a WideString,
> > > > since this has an operator BSTR. Is this acceptable and will this work
> > > > properly ? Is it the automation controller's responsibility to free the
> > > > BSTR after it is finished with it ?

Other Threads