COleSafeArray::PutElement

Assigns a single element into the array.

void PutElement(
   long* rgIndices,
   void* pvData 
);

Parameters

  • rgIndices
    Pointer to an array of indexes for each dimension of the array.

  • pvData
    Pointer to the data to assign to the array. VT_DISPATCH, VT_UNKNOWN, and VT_BSTR variant types are pointers and do not require another level of indirection.

Remarks

This function automatically calls the Windows functions SafeArrayLock Function and SafeArrayUnlock Function before and after assigning the element. If the data element is a string, object, or variant, the function copies it correctly, and if the existing element is a string, object, or variant, it is cleared correctly.

Note that you can have multiple locks on an array, so you can put elements into an array while the array is locked by other operations.

On error, the function throws a CMemoryException or COleException.

Example

VARIANT retVariantArray() 
{
   COleSafeArray saRet;         
   DWORD numElements[] = {10, 10}; // 10x10

   // Create the 2 dimensional safe-array of type VT_R8 with size 10x10
   saRet.Create(VT_R8, 2, numElements);

   // Initialize safearray  with values...
   long index[2];
   for(index[0] = 0; index[0] < 10; index[0]++)
   {
      for(index[1] = 0; index[1] < 10; index[1]++)
      {
         double val = index[0] + index[1]*10;
         //populate the safearray elements with double values
         saRet.PutElement(index, &val);
      }
   }
   // Return the safe-array encapsulated in a VARIANT...
   return saRet.Detach();
}

Requirements

Header: afxdisp.h

See Also

Reference

COleSafeArray Class

Hierarchy Chart

COleSafeArray::GetElement

SafeArrayPutElement Function

Other Resources

COleSafeArray Members