Re: Comparing a variant string with char*
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Tue, 19 Dec 2006 01:32:48 -0500
Note that 'char' is a rather quaint and obsolete data type, to be used only in rare and
exotic conditions.
A VARIANT type is not a string, and is not comparable to a string; furthermore, in the
extremely unlikely situation where you would have a pointer to an 8-bit character string
that contains "Y", it will not compare as equal to the string you coded, "Y", because ==
compares pointers, not values.
There is no VARIANT type that can represent a char string, for example, although there is
a type, VT_BYREF | VT_UI1, that, in the very unlikely situation where the string returned
was really a 8-bit character string, might be used.
VARIANT is a union, which has many members. You have to first ascertain that the type of
variant you have indeed is a pointer to 8-bit characters (most VARIANT types will be
pointers to Unicode (BSTR), because most VARIANT types are used to communicate to systems
such as VB that are implicitly Unicode), and if it is, you have to use (because you are
assuming 8-bit characters), strcmp. You have to allow for the fact that the VARIANT might
be Unicode characters. strcmp/wcscmp/tcscmp compare the contents of strings.
So you might write something like
VARIANT * v = _getVariant();
if(v != NULL && v->vt == VT_BSTR)
{ /* it is a string */
if(wcscmp(v->bstrVal, L"Y") == 0)
{ /* "Y" */
... code here
This should all be obvious if you actually had read the documentation about the VARIANT
type. Note that you can't write the code you did because (a) it might return NULL (b) it
might return a VARIANT which is not VT_BSTR. You have to assume that both of these are
possible.
joe
On 18 Dec 2006 21:39:12 -0800, "Aviraj" <aviraj.singh@xxxxxxxxx> wrote:
I am trying to compare a variant string with a char* constant. The codeJoseph M. Newcomer [MVP]
compiles on VS2002, but VS2005 gives the following compilation error,
Error 1 error C2678: binary '==' : no operator found which takes a
left-hand operand of type '_variant_t' (or there is no acceptable
conversion) c:\documents and settings\projects\test\test\test.cpp 25
The code is as follows,
static _variant_t _getVARIANTTfromRs();
int _tmain(int argc, _TCHAR* argv[])
{
if(_getVARIANTT() == "Y")
{
printf("Inside if\n");
}
else
{
printf("Inside else\n");
}
return 0;
}
_variant_t _getVARIANTT()
{
_variant_t value;
value.bstrVal = _bstr_t("");
return value;
}
How do I get this code to compile on VS2005 and make sure that the
comparison succeeds in case _getVARIANT returns "Y"
Thanks.
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: Comparing a variant string with char*
- From: Aviraj
- Re: Comparing a variant string with char*
- References:
- Comparing a variant string with char*
- From: Aviraj
- Comparing a variant string with char*
- Prev by Date: Re: shellexecute not opening opera (default browser)
- Next by Date: Re: Unicode Question
- Previous by thread: Comparing a variant string with char*
- Next by thread: Re: Comparing a variant string with char*
- Index(es):
Relevant Pages
|