본문 바로가기
IT-개발,DB

[개발/MFC] VARIANT, BSTR, SAFEARRAY C++ Tutorial

by SB리치퍼슨 2012. 2. 7.

출처 :  http://msdn.microsoft.com/en-us/library/e305240e-9e11-4006-98cc-26f4932d2118(VS.85) 


VARIANT, BSTR & SAFEARRAY C++ Tutorial...

Introduction

Frustrated by the lack of good articles on the VARIANT data type, I decided to write this short introduction.

The VARIANT type is an all purpose data type used by IDispatch::Invoke both to transmit and receive parameters. It can hold numbers, strings, arrays, error values and IDispatch pointers. An XLL developer could consider the variant to be the Automation equivalent of the do everything Excel XLOPER data type.

Structure

Here is a simplified version of the VARIANT definition. For the full definition see this link.

struct tagVARIANT {
    VARTYPE vt; // unsigned short integer type code
    WORD wReserved1;
    WORD wReserved2;
    WORD wReserved3;
    union {
    //  C++ Type      Union Name   Type Tag                Basic Type
    //  --------      ----------   --------                ----------

        long          lVal;        // VT_I4                ByVal Long
        unsigned char bVal;        // VT_UI1               ByVal Byte
        short         iVal;        // VT_I2                ByVal Integer
        double        dblVal;      // VT_R8                ByVal Double
        VARIANT_BOOL  boolVal;     // VT_BOOL              ByVal Boolean
        SCODE         scode;       // VT_ERROR
        DATE          date;        // VT_DATE              ByVal Date
        BSTR          bstrVal;     // VT_BSTR              ByVal String
        IUnknown      *punkVal;    // VT_UNKNOWN 
        IDispatch     *pdispVal;   // VT_DISPATCH          ByVal Object
        SAFEARRAY     *parray;     // VT_ARRAY|*           ByVal array
        // A bunch of other types that don't matter here...
        VARIANT       *pvarVal;    // VT_BYREF|VT_VARIANT  ByRef Variant
        void          * byref;     // Generic ByRef        
    };
};

The variant type is 16 bytes in size.

[출처] variant|작성자 najira00

반응형

댓글