Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VARIANT !!! 2

Status
Not open for further replies.

HadiRezaee

Technical User
Mar 9, 2001
165
IR
Hi all,
Please explain to me, what is VARIANT structure and how can i use of it in my programs. (Please write source code)

Thanks.
 
Typical OLE variant you should use like:
VARIANT x;
VariantInit(&x);//standard initialization
V_VT(&x)=VT_BOOL;//assigning some tpes to x, in this
//exapmple will be BOOL
VT_BOOL(&x)=1;//initializing the boolean x
VariantChangeType(&x,&x,0,VT_BSTR);//if you whant you may
//change type
....use variant there
VariantClear(&x);//freeing the x, you must do it

In ATL variant is more simple. Just use _variant_t. John Fill
1c.bmp


ivfmd@mail.md
 
In my opinion the above code unfortunately contains a subtile error and an inexact statement.
1. The BOOL type for VARIANT follows the VB conventions: 0 for false and -1 for true. For our convenience in VC++ there are two macros:

#define VARIANT_TRUE -1
#define VARIANT_FALSE 0

This type is a short, therefore -1 will be 0xffff (two bytes).

This line:
Code:
VT_BOOL(&x)=1;//initializing the boolean x
is not correct (the VT_BOOL is a macro without arguments) and value 1 will NOT set the BOOL as TRUE ! The corect line is:
Code:
x.boolVal = VARIANT_TRUE;//initializing the boolean x

2. The
Code:
_variant_t
is not an ATL class ! Is part of VC++ library. Just
Code:
#include <comdef.h>
and you can use it, as well as
Code:
_bstr_t
and
Code:
_com_error

ATL has another wrapper class for VARIANT:
Code:
CComVariant
. The MFC also has
Code:
COleVariant
. I prefer to use
Code:
CComVariant
and ATL because does not throw exceptions.

Marius Samoila
Brainbench MVP for Visual C++
 
Hi Samoila.
Now, i understand ... :)
Thanks alooooooooot for your excelent reply.

 
MSamoila, you are right, more correct is to put -1 instead of 1. But isn't an error because ProxyStub codes change it to correct value. Correct is to put every values what are not equal with 0 as true and 0 as false. John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top