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?
hshliangAsked:
Who is Participating?
 
chensuConnect With a Mentor Commented:
In most cases, using VT_BSTR and V_BSTR() is sufficient because COM/OLE usually use Unicode. You should use VT_BSTRT and V_BSTRT() when you use ANSI BSTRs, as in DAO. You'll understand it better after looking into the following preprocessor macro definitions in the AfxDisp.h.

#if defined(_UNICODE) || defined(OLE2ANSI)
      #define VTS_BSTR            VTS_WBSTR// an 'LPCOLESTR'
      #define VT_BSTRT            VT_BSTR
#else
      #define VTS_BSTR            "\x0E"  // an 'LPCSTR'
      #define VT_BSTRA            14
      #define VT_BSTRT            VT_BSTRA
#endif


// variant handling (use V_BSTRT when you have ANSI BSTRs, as in DAO)
#ifndef _UNICODE
      #define V_BSTRT(b)  (LPSTR)V_BSTR(b)
#else
      #define V_BSTRT(b)  V_BSTR(b)
#endif

0
 
hshliangAuthor Commented:
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.
0
 
chensuCommented:
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.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
hshliangAuthor Commented:
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
0
 
chensuCommented:
>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.
0
 
hshliangAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.