Board index » cppbuilder » Is Widestring exchangeble between objects?

Is Widestring exchangeble between objects?


2007-05-24 03:00:38 AM
cppbuilder112
As I understand, BSTRs must be allocated in system's context via
SysAllocString() so I can pass BSTRs from one component to another (kinda
using a central memory space).
I got a huge project to analyze and the original developer use WideString to
return BSTRs like this:
STDMETHODIMP TMyComp::f(BSTR *pbstr)
{
WideString str;
// some processing...
*pbstr = str.Detach();
return S_OK;
}
Is this safe?
Do I have to use SysAllocString() to allocate the string in the systems
heap?
Regards,
Fred
 
 

Re:Is Widestring exchangeble between objects?

"Fred" < XXXX@XXXXX.COM >wrote in message
Quote
As I understand, BSTRs must be allocated in system's context
via SysAllocString()
Correct.
Quote
I got a huge project to analyze and the original developer use
WideString to return BSTRs like this:
That is perfectly valid and safe to do, and is very common in
VCL-based ActiveX projects. WideString contains a BSTR internally,
and uses the appropriate BSTR functions to manage it. WideString's
destructor calls SysFreeString() if the BSTR is still owned by the
WideString. The WideString::Detach() method releases ownership of the
BSTR. In the code snippet you showed, that ownership is being passed
to the caller, who must then call SysFreeString() when the BSTR is not
longer needed.
WideString is particularly good to use when the ActiveX method has an
exception handler in it, ie:
STDMETHODIMP TMyComp::f(BSTR *pbstr)
{
if( !pbstr )
return E_POINTER;
try
{
*pbstr = NULL;
WideString str;
// some processing...
*pbstr = str.Detach(); // ownership passed to the caller
}
catch(const Exception &)
{
// the BSTR is freed automatically and ownership is not
passed to the caller!
return E_UNEXPECTED;
}
return S_OK;
}
Gambit
 

Re:Is Widestring exchangeble between objects?

Thanks a lot, Gambit! :)
Taking a look at the wstring.cpp in sources directory I figured out
everything you said. I have to say that your confirmation of what I
suspected was a big help!
Thanks, pal
Fred
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Fred" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>As I understand, BSTRs must be allocated in system's context
>via SysAllocString()

Correct.

>I got a huge project to analyze and the original developer use
>WideString to return BSTRs like this:

That is perfectly valid and safe to do, and is very common in
VCL-based ActiveX projects. WideString contains a BSTR internally,
and uses the appropriate BSTR functions to manage it. WideString's
destructor calls SysFreeString() if the BSTR is still owned by the
WideString. The WideString::Detach() method releases ownership of the
BSTR. In the code snippet you showed, that ownership is being passed
to the caller, who must then call SysFreeString() when the BSTR is not
longer needed.

WideString is particularly good to use when the ActiveX method has an
exception handler in it, ie:

STDMETHODIMP TMyComp::f(BSTR *pbstr)
{
if( !pbstr )
return E_POINTER;

try
{
*pbstr = NULL;

WideString str;
// some processing...

*pbstr = str.Detach(); // ownership passed to the caller
}
catch(const Exception &)
{
// the BSTR is freed automatically and ownership is not
passed to the caller!
return E_UNEXPECTED;
}

return S_OK;
}


Gambit


 

{smallsort}

Re:Is Widestring exchangeble between objects?

hello, i'm trying to write a VCL-based ActiveX component with BCB5
i wish to use a pointer to a List of BSTR (or WideString) in my Methods /
Events
or an Array of BSTR
is it possible? is there a specific documentation?
which Type and systax i must use.. ?
STDMETHODIMP TMyComp::f( ??(BSTR?) ???????)
{
}
thanks Davide.
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >ha scritto nel messaggio
Quote

"Fred" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>As I understand, BSTRs must be allocated in system's context
>via SysAllocString()

Correct.

>I got a huge project to analyze and the original developer use
>WideString to return BSTRs like this:

That is perfectly valid and safe to do, and is very common in
VCL-based ActiveX projects. WideString contains a BSTR internally,
and uses the appropriate BSTR functions to manage it. WideString's
destructor calls SysFreeString() if the BSTR is still owned by the
WideString. The WideString::Detach() method releases ownership of the
BSTR. In the code snippet you showed, that ownership is being passed
to the caller, who must then call SysFreeString() when the BSTR is not
longer needed.

WideString is particularly good to use when the ActiveX method has an
exception handler in it, ie:

STDMETHODIMP TMyComp::f(BSTR *pbstr)
{
if( !pbstr )
return E_POINTER;

try
{
*pbstr = NULL;

WideString str;
// some processing...

*pbstr = str.Detach(); // ownership passed to the caller
}
catch(const Exception &)
{
// the BSTR is freed automatically and ownership is not
passed to the caller!
return E_UNEXPECTED;
}

return S_OK;
}


Gambit