Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How check validity of BSTR?

647 views
Skip to first unread message

Ramesh

unread,
Feb 28, 2004, 7:42:57 PM2/28/04
to
Hi,
When a BSTR is passed is as a function parameter, how to test the
validity of BSTR?

In case of char* pointer we check whether it is NULL before using it.
Is there a similar error checking mechanism for BSTR?

I tried checking the lenght, but this is not the rightway, it hangs.

I am using ATL, my function get called by ExOLEDB.

Thanks
Ramesh

David Lowndes

unread,
Feb 29, 2004, 8:28:05 AM2/29/04
to
> When a BSTR is passed is as a function parameter, how to test the
>validity of BSTR?
>
>In case of char* pointer we check whether it is NULL before using it.
>Is there a similar error checking mechanism for BSTR?

Ramesh,

You can certainly check the BSTR for NULL. Other than that, it really
depends on what your function expects. For instance, you might not
allow an empty BSTR, so you could check the length using the
SysStringLen API.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq

Simon Trew

unread,
Mar 1, 2004, 3:39:58 AM3/1/04
to
By convention (perhaps even by definition) a NULL BSTR and a zero-length
BSTR are considered to be identical. So it's not usually wise to check for a
NULL BSTR. I believe the types _bstr_t and CComBSTR treat a NULL BSTR in
this way, so it shouldn't give you any problems if you are wrapping your
BSTR with one of those.

There's possibly an API to check whether the pointer passed in does point to
the start of a validly allocated COM memory block (one allocated, directly
or indirectly, with CoTaskMemAlloc), but I don't know of it. The API
IsValidPtrIn would seem like a good candidate, but is just marked as being
obsolete (and if it's available at all on your platform then it probably
just always returns true).

S.

"Ramesh" <asno...@hotmail.com> wrote in message
news:45ad1436.0402...@posting.google.com...

Ramesh

unread,
Mar 1, 2004, 11:05:38 AM3/1/04
to
Thanks for the responses. Checking first for NULL and then for length
seems to be the way to go.

Regards,
Ramesh

"Simon Trew" <ten.egnaro@werts> wrote in message news:<e9CIJj2$DHA....@TK2MSFTNGP11.phx.gbl>...

0 new messages