Windows apps
Collapse the table of content
Expand the table of content

VariantCopy Function

Frees the destination variant and makes a copy of the source variant.

HRESULT VariantCopy(
  VARIANTARG   *pvargDest,
  VARIANTARG   *pvargSrc


Pointer to the VARIANTARG to receive the copy.


Pointer to the VARIANTARG to be copied.

The return value obtained from the returned HRESULT is one of the following.






The variant contains an array that is locked.


The source and destination have an invalid variant type (usually uninitialized).


Memory could not be allocated for the copy.


One of the arguments is invalid.

First, free any memory that is owned by pvargDest, such as VariantClear (pvargDest must point to a valid initialized variant, and not simply to an uninitialized memory location). Then pvargDest receives an exact copy of the contents of pvargSrc.

If pvargSrc is a VT_BSTR, a copy of the string is made. If pvargSrcis a VT_ARRAY, the entire array is copied. If pvargSrc is a VT_DISPATCH or VT_UNKNOWN, AddRef is called to increment the object's reference count.

If the variant to be copied is a COM object that is passed by reference, the vtfield of the pvargSrcparameter is VT_DISPATCH | VT_BYREF or VT_UNKNOWN | VT_BYREF. In this case, VariantCopy does not increment the reference count on the referenced object. Because the variant being copied is a pointer to a reference to an object, VariantCopy has no way to determine if it is necessary to increment the reference count of the object. It is therefore the responsibility of the caller to call IUnknown::AddRef on the object or not, as appropriate.


The VariantCopy method is not threadsafe.

© 2017 Microsoft