[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to determine the type of string in a VARIANT?

Posted on 1998-10-13
6
Medium Priority
?
1,019 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 …
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.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

656 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