VariantCopy function

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


HRESULT VariantCopy(
  VARIANTARG       *pvargDest,
  const VARIANTARG *pvargSrc



The destination variant.


The source variant.

Return Value

This function can return one of these values.

Return code Description
The variant contains an array that is locked.
The variant type is not a valid type of variant.
One of the arguments is not valid.
Insufficient memory to complete the operation.


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.

Note  The VariantCopy method is not threadsafe.


Target Platform Windows
Header oleauto.h
Library OleAut32.lib
DLL OleAut32.dll

See Also

Variant Manipulation Functions