variant的結構可以參考標頭檔案vc98\include\oaidl.h中關於結構體tagvariant的定義。
對於variant變數的賦值:首先給vt成員賦值,指明資料型別,再對聯合結構中相同資料型別的變數賦值,舉個例子:
variant va;
int a=2001;
va.vt=vt_i4;///指明整型資料
va.lval=a; ///賦值
對於不馬上賦值的variant,最好先用void variantinit(variantarg far* pvarg);進行初始化,其本質是將vt設定為vt_empty,下表我們列舉vt與常用資料的對應關係:
unsigned char bval; vt_ui1
short ival; vt_i2
long lval; vt_i4
float fltval; vt_r4
double dblval; vt_r8
variant_bool boolval; vt_bool
scode scode; vt_error
cy cyval; vt_cy
date date; vt_date
bstr bstrval; vt_bstr
iunknown far* punkval; vt_unknown
idispatch far* pdispval; vt_dispatch
safearray far* parray; vt_array|*
unsigned char far* pbval; vt_byref|vt_ui1
short far* pival; vt_byref|vt_i2
long far* plval; vt_byref|vt_i4
float far* pfltval; vt_byref|vt_r4
double far* pdblval; vt_byref|vt_r8
variant_bool far* pboolval; vt_byref|vt_bool
scode far* pscode; vt_byref|vt_error
cy far* pcyval; vt_byref|vt_cy
date far* pdate; vt_byref|vt_date
bstr far* pbstrval; vt_byref|vt_bstr
iunknown far* far* ppunkval; vt_byref|vt_unknown
idispatch far* far* ppdispval; vt_byref|vt_dispatch
safearray far* far* pparray; vt_array|*
variant far* pvarval; vt_byref|vt_variant
void far* byref; vt_byref
_variant_t是variant的封裝類,其賦值可以使用強制型別轉換,其建構函式會自動處理這些資料型別。
例如:
long l=222;
ing i=100;
_variant_t lval(l);
lval = (long)i;
colevariant v3 = "字串", v4 = (long)1999;
cstring str =(bstr)v3.pbstrval;
long i = v4.lval;
VARIANT 資料型別
variant 資料型別在檔案oaidl.idl中定義如下 vc中資料型別,對應類為 variant t struct tagvariant variant name 4 vt record variant name 3 variant name 2 decimal decval variant n...
VARIANT 資料型別
variant 資料型別在檔案oaidl.idl中定義如下 vc中資料型別,對應類為 variant t struct tagvariant variant name 4 vt record variant name 3 variant name 2 decimal decval variant n...
VARIANT 資料型別
variant 資料型別在檔案oaidl.idl中定義如下 vc中資料型別,對應類為 variant t struct tagvariant variant name 4 vt record variant name 3 variant name 2 decimal decval variant n...