Solved

How to determine the type of string in a VARIANT?

Posted on 1998-10-13
6
978 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:hshliang
  • 3
  • 3
6 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1323278
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
 

Author Comment

by:hshliang
ID: 1323279
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
 
LVL 23

Expert Comment

by:chensu
ID: 1323280
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:hshliang
ID: 1323281
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
 
LVL 23

Expert Comment

by:chensu
ID: 1323282
>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
 

Author Comment

by:hshliang
ID: 1323283
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now