VARIANT型变量转换

1、 在VC++编程中,我们常常会遇到这样的函数,如:::SetValue(const VARIANTpropVal); 我们怎么把一个整形或double型等其它的类型赋值给VARIANT型变量呢,或者是VARIANT型变量怎么赋值给int等其它类型呢。

2、 VARIANT是一个结构体,它的结构体如下:

struct tagVARIANT {

    union {

        struct __tagVARIANT {

            VARTYPE vt;

            WORD    wReserved1;

            WORD    wReserved2;

            WORD    wReserved3;

            union {

                ULONGLONG     ullVal;       /* VT_UI8               */

                LONGLONG      llVal;        /* VT_I8                */

                LONG          lVal;         /* VT_I4                */

                BYTE          bVal;         /* VT_UI1               */

                SHORT         iVal;         /* VT_I2                */

                FLOAT         fltVal;       /* VT_R4                */

                DOUBLE        dblVal;       /* VT_R8                */

                VARIANT_BOOL boolVal;      /* VT_BOOL              */

                _VARIANT_BOOL bool;         /* (obsolete)           */

                SCODE         scode;        /* VT_ERROR             */

                CY            cyVal;        /* VT_CY                */

                DATE          date;         /* VT_DATE              */

                BSTR          bstrVal;      /* VT_BSTR              */

                IUnknown *    punkVal;      /* VT_UNKNOWN           */

                IDispatch *   pdispVal;     /* VT_DISPATCH          */

                SAFEARRAY *   parray;       /* VT_ARRAY             */

                BYTE *        pbVal;        /* VT_BYREF|VT_UI1      */

                SHORT *       piVal;        /* VT_BYREF|VT_I2       */

                LONG *        plVal;        /* VT_BYREF|VT_I4       */

                LONGLONG *    pllVal;       /* VT_BYREF|VT_I8       */

                FLOAT *       pfltVal;      /* VT_BYREF|VT_R4       */

                DOUBLE *      pdblVal;      /* VT_BYREF|VT_R8       */

                VARIANT_BOOL *pboolVal;     /* VT_BYREF|VT_BOOL     */

                _VARIANT_BOOL *pbool;       /* (obsolete)           */

                SCODE *       pscode;       /* VT_BYREF|VT_ERROR    */

                CY *          pcyVal;       /* VT_BYREF|VT_CY       */

                DATE *        pdate;        /* VT_BYREF|VT_DATE     */

                BSTR *        pbstrVal;     /* VT_BYREF|VT_BSTR     */

                IUnknown **   ppunkVal;     /* VT_BYREF|VT_UNKNOWN */

                IDispatch ** ppdispVal;    /* VT_BYREF|VT_DISPATCH */

                SAFEARRAY ** pparray;      /* VT_BYREF|VT_ARRAY    */

                VARIANT *     pvarVal;      /* VT_BYREF|VT_VARIANT */

                PVOID         byref;        /* Generic ByRef        */

                CHAR          cVal;         /* VT_I1                */

                USHORT        uiVal;        /* VT_UI2               */

                ULONG         ulVal;        /* VT_UI4               */

                INT           intVal;       /* VT_INT               */

                UINT          uintVal;      /* VT_UINT              */

                DECIMAL *     pdecVal;      /* VT_BYREF|VT_DECIMAL */

                CHAR *        pcVal;        /* VT_BYREF|VT_I1       */

                USHORT *      puiVal;       /* VT_BYREF|VT_UI2      */

                ULONG *       pulVal;       /* VT_BYREF|VT_UI4      */

                ULONGLONG *   pullVal;      /* VT_BYREF|VT_UI8      */

                INT *         pintVal;      /* VT_BYREF|VT_INT      */

                UINT *        puintVal;     /* VT_BYREF|VT_UINT     */

                struct __tagBRECORD {

                    PVOID         pvRecord;

                    IRecordInfo * pRecInfo;

                } __VARIANT_NAME_4;         /* VT_RECORD            */

            } __VARIANT_NAME_3;

        } __VARIANT_NAME_2;

        DECIMAL decVal;

    } __VARIANT_NAME_1;

};

3、 Float型转化为VARIANT

//float型转化为VARIANT

VARIANT va1;

float a1=m_Fre1_AD9852;

va1.vt=VT_R4;///指明float数据,VT_I4是整型,VT_R8double

//va1.lVal=a1; 

va1.fltVal=a1; //指明fltVal是float型赋值,intVal是整型,dblVal是double

m_Fre1_wknob.SetValue(va1);

4、 VARIANT型转化为double型

VARIANT var;

var=m_Power_wknob.GetValue();//获取值

value_power=var.dblVal; ///指明fltVal是float型赋值,intVal是整型,dblVal是double

AMP_dBm_Reg=value_power;//赋值给double型

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页