Link to home
Start Free TrialLog in
Avatar of hshliang
hshliang

asked on

How to determine the type of string in a VARIANT?

How do I check a Variant whether the text(string) in it is a VT_BSTR or VT_BSTRT?
I find that the vt variable is always 0x0008 i.e. VT_BSTR,
but if you use V_BSTR() or V_BSTRT() to convert it to a CString you get very different results.

Can any one tell me how to determine which V_XXX should I use each time?
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hshliang
hshliang

ASKER

Thank you, but is it true that it is always UNICODE? as I assign Variant myself for use in DAO, but I find it hard to check what actually is in the Variant when I need to read it again. I understand your explanation, but you did not explain how to check inside the Variant. I cannot think of a way to do it, can you?

Henry.
I think you misunderstood the functionality of VT_BSTRT. VT_BSTRT is VT_BSTR or VT_BSTRA depending on whether _UNICODE is defined. It is just like the relationship between LPTSTR and LPSTR. LPTSTR is LPSTR or LPWSTR depending on whether _UNICODE is defined. You cannot say it is VT_BSTRT or VT_BSTR just like that you cannot say it is LPTSTR or LPSTR. To check what is in the Variant, check the vt to see if it is VT_BSTR or VT_BSTRA.
Thank you, I think I must have been confused about BSTR and BSTRT, let me explain, I have done a simple experiment:
      CString s1,s2,s3,s4;
      COleVariant va1,va2;

      va1.SetString("BSTR",VT_BSTR);
      va2.SetString("BSTRT",VT_BSTRT);

      s1=V_BSTR(&va1);
      s2=V_BSTRT(&va1);
      s3=V_BSTR(&va2);
      s4=V_BSTRT(&va2);

the result:
      s1="BSTR"      //correct
      s2="B"            //incorrect
      s3="??T"                   //incorrect
      s4="BSTRT"      //correct

Let say, I am writing a ole control, and receive a VARIANT from the client. Now, how am I going to tell whether the client is giving me a string variant that is set by BSTR or BSTRT? If the client may do either, I will not be able to convert it back to the right string. That is why I ask if I can tell what is in the Variant.
I use VC++ 4,2, under Window 95/98, and do DAO with Jet 3 database. I use the DAO sdk for assess of the database. Hence, I will have to able to handle any kind of strings in variant forms. Can you help to solve this problem?

Hope you can help. Thanks.
Henry
>Let say, I am writing a ole control, and receive a VARIANT from the client. Now, how am I going to tell whether the client is giving me a string variant that is set by BSTR or BSTRT?

In this case, they should always use VT_BSTR (UNICODE). It is the client's responsibility to set the correct VT_BSTR (UNICODE) strings.

>I use VC++ 4,2, under Window 95/98, and do DAO with Jet 3 database. I use the DAO sdk for assess of the database. Hence, I will have to able to handle any kind of strings in variant forms.

A DAO recordset in a non-UNICODE build expects strings to be ANSI. In this case, you should use VT_BSTRT to make ANSI strings.


Regarding your simple experiment, you are not able to know whether va1 or va2 are VT_BSTR or VT_BSTRT if you do not see the SetString statements because the vt member is always VT_BSTR in both cases.
Thank you, you did answer my question this time, i.e. There is No way to tell if a variant is BSTR or BSTRT. I think microsoft should make some changes in the structure of VARIANT to address this deficiency, as being a responsible server programmer, we must expect the client to give us any thing and make sure the variant we receive is compatible. Right?
Thank you any ways.
Henry