Search code examples
winapiipropertystorage

Initialize PROPVARIANT structure of VT_VARIANT | VT_VECTOR type


I am curious as to how to initialize a PROPVARIANT structure of VT_VARIANT | VT_VECTOR type. An existing model I am aware of is the HeadingPairs property of the DocumentSummaryInformation property set:

The HeadingPairs property is stored as a vector of variants, in repeating pairs of VT_LPSTR (or VT_LPWSTR) and VT_I4 values.

I am aware of the various Init functions, such as the InitPropVariantFromStringAsVector function to create a VT_VECTOR | VT_LPWSTR propvariant, but, as far as I know, there is no function to initialize a PROPVARIANT structure of VT_VARIANT | VT_VECTOR type.

Any help or suggestions would be appreciated.

Thank you.


Solution

  • You are correct that there is no ready-made Win32 function to initialize a PROPVARIANT with a vector of PROPVARIANT values. You are just going to have to initialize it manually, ie by allocating an array of PROPVARIANTs and then assigning a pointer to that array to the PROPVARIANT::capropvar.pElems field, and the array's length to the PROPVARIANT::capropvar.cElems field, eg:

    int count = ...;
    PROPVARIANT *arr = (PROPVARIANT*) CoTaskMemAlloc(count * sizeof(PROPVARIANT));
    
    // initialize array values as needed...
    
    PROPVARIANT pv;
    PropVariantInit(&pv);
          
    pv.vt = VT_VECTOR | VT_VARIANT;
    pv.capropvar.pElems = arr;
    pv.capropvar.cElems = count;
    
    // use pv as needed...
    
    PropVariantClear(&pv);