PropVariantToStringVector function

Extracts a vector of strings from a PROPVARIANT structure.

Syntax


HRESULT PropVariantToStringVector(
  _In_  REFPROPVARIANT propvar,
  _Out_ PWSTR          *prgsz,
  _In_  ULONG          crgsz,
  _Out_ ULONG          *pcElem
);

Parameters

propvar [in]

Type: REFPROPVARIANT

Reference to a source PROPVARIANT structure.

prgsz [out]

Type: PWSTR*

Pointer to a vector of string pointers. When this function returns, the buffer has been initialized with pcElem elements pointing to newly allocated strings containing the data extracted from the source PROPVARIANT.

crgsz [in]

Type: ULONG

Size of the buffer pointed to by prgsz, in elements.

pcElem [out]

Type: ULONG*

When this function returns, contains the count of strings extracted from source PROPVARIANT structure.

Return value

Type: HRESULT

This function can return one of these values.

Return codeDescription
S_OK

Returns S_OK if successful, or an error value otherwise.

TYPE_E_BUFFERTOOSMALL

The sourcePROPVARIANTcontained more than crgsz values. The buffer pointed to by prgsz.

E_INVALIDARG

ThePROPVARIANTwas not of the appropriate type.

 

Remarks

This helper function is used in places where the calling application expects a PROPVARIANT to hold an vector of string values with a fixed number of elements.

This function works for the following PROPVARIANT types:

  • VT_VECTOR | VT_LPWSTR
  • VT_VECTOR | VT_BSTR
  • VT_ARRAY | VT_BSTR

If the source PROPVARIANT has a supported type, this helper function extracts up to crgsz string values and places an allocated copy of each into the buffer pointed to by prgsz. If thePROPVARIANTcontains more elements than will fit into the prgsz buffer, this function returns an error and sets pcElem to 0.

Since each string in pointed to by the output buffer has been newly allocated, the calling application is responsible for using CoTaskMemFree to free each string in the output buffer when they are no longer needed.

If a BSTR in the source PROPVARIANT is NULL, it is converted to a newly allocated string containing "" in the output.

Examples


// PROPVARIANT propvar;
// Assume the variable propvar is initialized and valid
LPWSTR rgszStrings; // The application is expecting propvar to hold 4 strings in a vector
ULONG cElems;
HRESULT hr = PropVariantToStringVector(propvar, rgszStrings, ARRAYSIZE(rgszStrings), &cElems);
if (SUCCEEDED(hr))
{
     if (cElems == ARRAYSIZE(rgszStrings))
     {
         // The application got 4 string which are now stored in rgszStrings
     }
     else
     {
         // The application got cElems which are stored in the first cElems elements of rgLongs
     }
 
    // Free the cElems strings that PropVariantToStringVector allocated
    for (ULONG i = 0; i < cElems; i++)
    {
        CoTaskMemFree(rgszStrings[i]);
    }
}

Requirements

Minimum supported client

Windows XP with SP2, Windows Vista [desktop apps only]

Minimum supported server

Windows Server 2003 with SP1 [desktop apps only]

Redistributable

Windows Desktop Search (WDS) 3.0

Header

Propvarutil.h

Library

Propsys.lib

DLL

Propsys.dll (version 6.0 or later)

See also

InitPropVariantFromStringVector
PropVariantToString
PropVariantGetStringElem
PropVariantToStringVectorAlloc
VariantToStringArray

 

 

Show: