Solved

How to determine the type of string in a VARIANT?

Posted on 1998-10-13
6
992 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.

730 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