Greedo
Greedo

Reputation: 5543

Understanding the tagVARIANT struct

The COM VARIANT type is defined using the tagVARIANT structure like this:

typedef struct tagVARIANT {
  union {
    struct {
      VARTYPE vt;
      WORD    wReserved1;
      WORD    wReserved2;
      WORD    wReserved3;
      union {
        LONGLONG     llVal;
        LONG         lVal;
        BYTE         bVal;
        SHORT        iVal;
        FLOAT        fltVal;
        DOUBLE       dblVal;
        VARIANT_BOOL boolVal;
        VARIANT_BOOL __OBSOLETE__VARIANT_BOOL;
        SCODE        scode;
        CY           cyVal;
        DATE         date;
        BSTR         bstrVal;
        IUnknown     *punkVal;
        IDispatch    *pdispVal;
        SAFEARRAY    *parray;
        BYTE         *pbVal;
        SHORT        *piVal;
        LONG         *plVal;
        LONGLONG     *pllVal;
        FLOAT        *pfltVal;
        DOUBLE       *pdblVal;
        VARIANT_BOOL *pboolVal;
        VARIANT_BOOL *__OBSOLETE__VARIANT_PBOOL;
        SCODE        *pscode;
        CY           *pcyVal;
        DATE         *pdate;
        BSTR         *pbstrVal;
        IUnknown     **ppunkVal;
        IDispatch    **ppdispVal;
        SAFEARRAY    **pparray;
        VARIANT      *pvarVal;
        PVOID        byref;
        CHAR         cVal;
        USHORT       uiVal;
        ULONG        ulVal;
        ULONGLONG    ullVal;
        INT          intVal;
        UINT         uintVal;
        DECIMAL      *pdecVal;
        CHAR         *pcVal;
        USHORT       *puiVal;
        ULONG        *pulVal;
        ULONGLONG    *pullVal;
        INT          *pintVal;
        UINT         *puintVal;
        struct {
          PVOID       pvRecord;
          IRecordInfo *pRecInfo;
        } __VARIANT_NAME_4;
      } __VARIANT_NAME_3;
    } __VARIANT_NAME_2;
    DECIMAL decVal;
  } __VARIANT_NAME_1;
} VARIANT;

I'm just wondering: what do all those __VARIANT_NAME_... labels mean?

I'm trying to construct one of these manually in VBA, and would like to understand what all the members mean

Upvotes: 2

Views: 2951

Answers (2)

Lewis Miller
Lewis Miller

Reputation: 105

if NONAMELESSUNION is defined then __VARIANT_NAME_1-2-3-4 evaluate to n1,n2,n3,n4 respectively, otherwise they will be blank, making them nameless unions.

#ifdef NONAMELESSUNION
#define __VARIANT_NAME_1 n1
#define __VARIANT_NAME_2 n2
#define __VARIANT_NAME_3 n3
#define __VARIANT_NAME_4 n4

#else

#define __tagVARIANT
#define __VARIANT_NAME_1
#define __VARIANT_NAME_2
#define __VARIANT_NAME_3
#define __VARIANT_NAME_4
#endif

VBA doesnt have "unions" so you need to make a structure something like this (vba does have structures if i remember right, its been a while)

Public Type MyVariant
   vt As VarType ' or Integer
   Value As String * (Longest size of a VBA (or Variant) type Possible, probably 16)
   Dec As Decimal
End Type

and then handle copying the data to and from Value using RtlMoveMemory() and using the vt field to determine size and type of the data in 'Value'. Im guessing Currency or Long64 will be your biggest type other than Decimal. Strings and Arrays will be pointers.

Upvotes: 2

eerorika
eerorika

Reputation: 238401

What does __VARIANT_NAME_1 mean in a union struct?

typedef struct tagVARIANT {
  union {
    ....
  } __VARIANT_NAME_1;
} VARIANT;

It is the name of a variable of that unnamed union type. In this context, the variable is a member of tagVARIANT.

P.S. __VARIANT_NAME_1 and several other names in that definition are names reserved to the language implementation.

Upvotes: 3

Related Questions